Skip to content

Commit

Permalink
client: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
julie-ng committed Sep 15, 2018
1 parent d8dda3f commit b93708e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
13 changes: 13 additions & 0 deletions client/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const https = require('https');
const fs = require('fs');
const path = require('path');

module.exports = function (name) {
const certFile = path.resolve(__dirname, `${name}_cert.pem`);
const keyFile = path.resolve(__dirname, `${name}_key.pem`);
return new https.Agent({
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
rejectUnauthorized: false
});
};
17 changes: 2 additions & 15 deletions client/invalid-app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
const axios = require('axios');
const https = require('https');
const fs = require('fs');
const path = require('path');
const agent = require('./agent');

const serverUrl = 'https://localhost:4433/authenticate';
let opts = { httpsAgent: agent('bob') };

// Configure certifcate options
const certFile = path.resolve(__dirname, 'bob_cert.pem');
const keyFile = path.resolve(__dirname, 'bob_key.pem');
let agent = new https.Agent({
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
rejectUnauthorized: false
});
let opts = { httpsAgent: agent };

// Test with Promises
axios.get(serverUrl, opts)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.error(err.response.data);
});

29 changes: 3 additions & 26 deletions client/valid-app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
const axios = require('axios');
const https = require('https');
const fs = require('fs');
const path = require('path');
const agent = require('./agent');

const serverUrl = 'https://localhost:4433/authenticate';
let opts = { httpsAgent: agent('alice') };

// Configure certifcate options
const certFile = path.resolve(__dirname, 'alice_cert.pem');
const keyFile = path.resolve(__dirname, 'alice_key.pem');
let agent = new https.Agent({
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
rejectUnauthorized: false
});
let opts = { httpsAgent: agent };

// Test with Promises
axios.get(serverUrl, opts)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.error(err);
console.error(err.response.data);
});

// Test with async/away
async function testClient() {
try {
const res = await axios.get(serverUrl, opts);
console.log(res.data);
} catch (err) {
console.error(err);
}
}
// testClient();

0 comments on commit b93708e

Please sign in to comment.