Refactor all auth adapters to reduce duplications (#4954)

* Refactor all auth adapters to reduce duplications

* Adds mocking and proper testing for all auth adapters

* Proper testing of the google auth adapter

* noit
This commit is contained in:
Florent Vilmart
2018-08-12 11:05:28 -04:00
committed by GitHub
parent f1b008388c
commit b9673da07b
16 changed files with 214 additions and 320 deletions

View File

@@ -0,0 +1,41 @@
const https = require('https');
function makeCallback(resolve, reject, noJSON) {
return function(res) {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (noJSON) {
return resolve(data);
}
try {
data = JSON.parse(data);
} catch(e) {
return reject(e);
}
resolve(data);
});
res.on('error', reject);
};
}
function get(options, noJSON = false) {
return new Promise((resolve, reject) => {
https
.get(options, makeCallback(resolve, reject, noJSON))
.on('error', reject);
});
}
function request(options, postData) {
return new Promise((resolve, reject) => {
const req = https.request(options, makeCallback(resolve, reject));
req.on('error', reject);
req.write(postData);
req.end();
});
}
module.exports = { get, request };