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:
41
src/Adapters/Auth/httpsRequest.js
Normal file
41
src/Adapters/Auth/httpsRequest.js
Normal 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 };
|
||||
Reference in New Issue
Block a user