@everymundo/promise-data-to
Advanced tools
Comparing version 1.2.1 to 1.2.2
13
index.js
@@ -1,6 +0,5 @@ | ||
const | ||
{ promiseGet } = require('./lib/promise-get'), | ||
{ promiseDataTo } = require('./lib/promise-data-to'), | ||
{ urlToEndpoint } = require('./lib/url-to-endpoint'), | ||
{ parseEndpoints } = require('./lib/parse-endpoints'); | ||
const { promiseGet } = require('./lib/promise-get') | ||
const { promiseDataTo } = require('./lib/promise-data-to') | ||
const { urlToEndpoint } = require('./lib/url-to-endpoint') | ||
const { parseEndpoints } = require('./lib/parse-endpoints') | ||
@@ -11,3 +10,3 @@ module.exports = { | ||
urlToEndpoint, | ||
promiseGet, | ||
}; | ||
promiseGet | ||
} |
@@ -1,23 +0,23 @@ | ||
'use strict'; | ||
'use strict' | ||
const querystring = require('querystring'); | ||
const querystring = require('querystring') | ||
const addQueryToPath = (inputQuery, path) => { | ||
if (!inputQuery) return path; | ||
if (!inputQuery) return path | ||
let query = inputQuery; | ||
let query = inputQuery | ||
if (typeof inputQuery !== 'string') { | ||
query = querystring.stringify(inputQuery); | ||
query = querystring.stringify(inputQuery) | ||
} | ||
if (query.indexOf('?') === 0) { | ||
query = query.substr(1); | ||
query = query.substr(1) | ||
} | ||
const joinChar = path.includes('?') ? '&' : '?'; | ||
const joinChar = path.includes('?') ? '&' : '?' | ||
return `${path}${joinChar}${query}`; | ||
}; | ||
return `${path}${joinChar}${query}` | ||
} | ||
module.exports = { addQueryToPath }; | ||
module.exports = { addQueryToPath } |
@@ -1,25 +0,25 @@ | ||
const zlib = require('zlib'); | ||
const zlib = require('zlib') | ||
const compression = { | ||
deflate(data) { | ||
return zlib.deflateSync(data); | ||
deflate (data) { | ||
return zlib.deflateSync(data) | ||
}, | ||
gzip(data) { | ||
return zlib.gzipSync(data); | ||
}, | ||
}; | ||
gzip (data) { | ||
return zlib.gzipSync(data) | ||
} | ||
} | ||
const getDataFromXData = (xData, compressMethod) => { | ||
const data = (xData && [Object, Array].includes(xData.constructor)) ? JSON.stringify(xData) : xData; | ||
const data = (xData && [Object, Array].includes(xData.constructor)) ? JSON.stringify(xData) : xData | ||
if (compressMethod && compression[compressMethod]) { | ||
return compression[compressMethod](data); | ||
return compression[compressMethod](data) | ||
} | ||
return data; | ||
}; | ||
return data | ||
} | ||
module.exports = { | ||
getDataFromXData, | ||
compression, | ||
}; | ||
compression | ||
} |
@@ -1,15 +0,15 @@ | ||
'use strict'; | ||
'use strict' | ||
/* eslint-disable no-console */ | ||
const protos = { 'http:': require('http'), 'https:': require('https') }; | ||
const { parseJson } = require('@everymundo/json-utils'); | ||
const protos = { 'http:': require('http'), 'https:': require('https') } | ||
const { parseJson } = require('@everymundo/json-utils') | ||
const validateEndpointProtocol = endpointKey => (endpoint) => { | ||
if (/^https?:/.test(endpoint)) return true; | ||
if (/^https?:/.test(endpoint)) return true | ||
throw new Error(`endpointKey: [${endpointKey}] has an invalid endpoint [${endpoint}]`); | ||
}; | ||
throw new Error(`endpointKey: [${endpointKey}] has an invalid endpoint [${endpoint}]`) | ||
} | ||
const parseEndpoints = (pattern = /^(MAIN)_ENDPOINT$/) => { | ||
const parsedEndpoints = {}; | ||
const parsedEndpoints = {} | ||
Object | ||
@@ -21,7 +21,8 @@ .keys(process.env) | ||
const | ||
match = envVar.match(pattern), | ||
endpointKey = match[1] || match[0]; | ||
match = envVar.match(pattern) | ||
const endpointKey = match[1] || match[0] | ||
if (parsedEndpoints[endpointKey]) { | ||
throw new Error(`${endpointKey} present twice`); | ||
throw new Error(`${endpointKey} present twice`) | ||
} | ||
@@ -33,11 +34,14 @@ | ||
.split(/[|,;\s]+/) | ||
.filter(validateEndpointProtocol(endpointKey)), | ||
headers_env = `${envVar}_HEADERS`, | ||
raw_headers = process.env[headers_env] ? process.env[headers_env].split('|||') : [], | ||
headers = raw_headers.map(parseJson); | ||
.filter(validateEndpointProtocol(endpointKey)) | ||
parsedEndpoints[endpointKey] = new Array(endpoints.length); | ||
const headersEnv = `${envVar}_HEADERS` | ||
const rawHeaders = process.env[headersEnv] ? process.env[headersEnv].split('|||') : [] | ||
const headers = rawHeaders.map(parseJson) | ||
parsedEndpoints[endpointKey] = new Array(endpoints.length) | ||
endpoints.forEach((endpoint, ix) => { | ||
const { protocol, hostname, port, path, auth } = require('url').parse(endpoint); | ||
const { protocol, hostname, port, path, auth } = require('url').parse(endpoint) | ||
const configs = { | ||
@@ -50,13 +54,13 @@ http: protos[protocol], | ||
endpoint, | ||
compress: undefined, | ||
}; | ||
parsedEndpoints[endpointKey][ix] = configs; | ||
compress: undefined | ||
} | ||
parsedEndpoints[endpointKey][ix] = configs | ||
if (headers[ix] instanceof Error) { | ||
// console.error(headers_env, 'error for', { raw: raw_headers[ix], headers_env, raw_headers }, { error: headers[ix] }); | ||
throw headers[ix]; | ||
throw headers[ix] | ||
} | ||
if (headers[ix]) { | ||
Object.keys(headers[ix]).forEach(key => configs.headers[key.toLowerCase()] = headers[ix][key]); | ||
Object.keys(headers[ix]).forEach(key => { configs.headers[key.toLowerCase()] = headers[ix][key] }) | ||
} | ||
@@ -66,13 +70,13 @@ | ||
if (auth) { | ||
configs.headers.Authorization = auth.replace(':', ' '); | ||
configs.endpoint = configs.endpoint.replace(`${auth}@`, ''); | ||
configs.headers.Authorization = auth.replace(':', ' ') | ||
configs.endpoint = configs.endpoint.replace(`${auth}@`, '') | ||
} | ||
configs.compress = configs.headers['content-encoding']; | ||
}); | ||
}); | ||
configs.compress = configs.headers['content-encoding'] | ||
}) | ||
}) | ||
return parsedEndpoints; | ||
}; | ||
return parsedEndpoints | ||
} | ||
module.exports = { parseEndpoints }; | ||
module.exports = { parseEndpoints } |
@@ -1,14 +0,13 @@ | ||
'use strict'; | ||
'use strict' | ||
const | ||
logr = require('@everymundo/simple-logr'), | ||
{ addQueryToPath } = require('../lib/add-query-to-path'), | ||
{ simulatedResponse } = require('../lib/simulate-response'), | ||
{ setResTxt } = require('../lib/set-response-text'), | ||
{ getDataFromXData } = require('../lib/get-data-from-xdata'), | ||
{ setHeaders } = require('../lib/set-headers'), | ||
protocols = {'http:': require('http'), 'https:': require('https')}, | ||
SIMULATE = +process.env.SIMULATE, | ||
MAX_RETRY_ATTEMPTS = Math.abs(process.env.MAX_RETRY_ATTEMPTS) || 3, | ||
RETRY_TIMEOUT_MS = Math.abs(process.env.RETRY_TIMEOUT_MS) || 500; | ||
const logr = require('@everymundo/simple-logr') | ||
const { addQueryToPath } = require('../lib/add-query-to-path') | ||
const { simulatedResponse } = require('../lib/simulate-response') | ||
const { setResTxt } = require('../lib/set-response-text') | ||
const { getDataFromXData } = require('../lib/get-data-from-xdata') | ||
const { setHeaders } = require('../lib/set-headers') | ||
const protocols = { 'http:': require('http'), 'https:': require('https') } | ||
const SIMULATE = +process.env.SIMULATE | ||
const MAX_RETRY_ATTEMPTS = Math.abs(process.env.MAX_RETRY_ATTEMPTS) || 3 | ||
const RETRY_TIMEOUT_MS = Math.abs(process.env.RETRY_TIMEOUT_MS) || 500 | ||
@@ -29,23 +28,23 @@ const promiseDataTo = ({ | ||
}, xData) => new Promise((resolve, reject) => { | ||
if (!http) http = protocols[protocol]; | ||
if (!http) http = protocols[protocol] | ||
// An object of options to indicate where to post to | ||
const | ||
// data = typeof xData === 'string' ? xData : JSON.stringify(xData), | ||
data = getDataFromXData(xData, compress), | ||
start = new Date(), | ||
post_options = { | ||
host, | ||
port, | ||
query, | ||
path: query ? addQueryToPath(query, path) : path, | ||
method, | ||
headers, | ||
agent, | ||
}; | ||
// const data = typeof xData === 'string' ? xData : JSON.stringify(xData), | ||
const data = getDataFromXData(xData, compress) | ||
const start = new Date() | ||
let attempt = 1; | ||
const requestOptions = { | ||
host, | ||
port, | ||
query, | ||
path: query ? addQueryToPath(query, path) : path, | ||
method, | ||
headers, | ||
agent | ||
} | ||
if (SIMULATE) return resolve(simulatedResponse(start, xData)); | ||
let attempt = 1 | ||
setHeaders(headers, data, compress); | ||
if (SIMULATE) return resolve(simulatedResponse(start, xData)) | ||
setHeaders(headers, data, compress) | ||
// if (!headers['Content-type']) headers['Content-type'] = 'application/json'; | ||
@@ -61,10 +60,10 @@ | ||
// Set up the request | ||
const post_req = http.request(post_options, (res) => { | ||
const request = http.request(requestOptions, (res) => { | ||
// res.setEncoding('utf8'); | ||
const buffers = []; | ||
const buffers = [] | ||
res | ||
.on('data', (chunk) => { buffers.push(chunk); }) | ||
.on('data', (chunk) => { buffers.push(chunk) }) | ||
.on('end', () => { | ||
const buffer = Buffer.concat(buffers); | ||
const buffer = Buffer.concat(buffers) | ||
@@ -84,33 +83,33 @@ setResTxt(res, buffer).then((resTxt) => { | ||
requestHeaders: headers, | ||
responseHeaders: res.headers, | ||
}; | ||
responseHeaders: res.headers | ||
} | ||
if (res.statusCode === 400) { | ||
logr.error({ resTxt }); | ||
const err = new Error('400 Status'); | ||
err.stats = stats; | ||
logr.error({ resTxt }) | ||
const err = new Error('400 Status') | ||
err.stats = stats | ||
return reject(err); | ||
return reject(err) | ||
} | ||
if (res.statusCode > 400) { | ||
logr.error({ resTxt }); | ||
return tryAgain(`Status Code: ${res.statusCode}`, stats); | ||
logr.error({ resTxt }) | ||
return tryAgain(`Status Code: ${res.statusCode}`, stats) | ||
} | ||
if (res.statusCode > 299) { | ||
stats.err = new Error(`{"Response": ${stats.resTxt}, "statusCode": ${res.statusCode}, "data": ${data}}`); | ||
return resolve(stats); | ||
stats.err = new Error(`{"Response": ${stats.resTxt}, "statusCode": ${res.statusCode}, "data": ${data}}`) | ||
return resolve(stats) | ||
} | ||
resolve(stats); | ||
resolve(stats) | ||
}) | ||
.catch((err) => { | ||
logr.error(err); | ||
reject(err); | ||
}); | ||
}); | ||
logr.error(err) | ||
reject(err) | ||
}) | ||
}) | ||
}) | ||
.on('error', (err) => { | ||
logr.error('http.request', err); | ||
logr.error('http.request', err) | ||
const stats = { | ||
@@ -124,29 +123,28 @@ code: 599, | ||
compress, | ||
requestHeaders: headers, | ||
}; | ||
tryAgain(err, stats); | ||
}); | ||
requestHeaders: headers | ||
} | ||
tryAgain(err, stats) | ||
}) | ||
// post the data | ||
if (['PUT', 'PATCH', 'POST'].includes(method.toUpperCase())) post_req.write(data); | ||
post_req.end(); | ||
}; | ||
if (['PUT', 'PATCH', 'POST'].includes(method.toUpperCase())) request.write(data) | ||
request.end() | ||
} | ||
function tryAgain(error, stats) { | ||
logr.error('tryAgain: attempt', attempt, 'has failed.', host + path, error); | ||
function tryAgain (error, stats) { | ||
logr.error('tryAgain: attempt', attempt, 'has failed.', host + path, error) | ||
if (attempt < maxRetry) { | ||
return setTimeout(() => { post(++attempt); }, 500); | ||
return setTimeout(() => { post(++attempt) }, 500) | ||
} | ||
const err = error instanceof Error ? error : new Error(error); | ||
err.message = `tryAgain has exceeded max delivery attempts (${attempt}):${err.message}`; | ||
logr.error(err.message); | ||
stats.err = err; | ||
reject(stats); | ||
const err = error instanceof Error ? error : new Error(error) | ||
err.message = `tryAgain has exceeded max delivery attempts (${attempt}):${err.message}` | ||
logr.error(err.message) | ||
stats.err = err | ||
reject(stats) | ||
} | ||
post(); | ||
}); | ||
post() | ||
}) | ||
module.exports = { | ||
@@ -157,3 +155,3 @@ promiseDataTo, | ||
loadedAt: new Date(), | ||
getDataFromXData, | ||
}; | ||
getDataFromXData | ||
} |
@@ -1,2 +0,2 @@ | ||
const { promiseDataTo } = require('../lib/promise-data-to'); | ||
const { promiseDataTo } = require('../lib/promise-data-to') | ||
@@ -13,3 +13,3 @@ const promiseGet = ({ | ||
agent, | ||
maxRetry, | ||
maxRetry | ||
}) => { | ||
@@ -27,8 +27,8 @@ const config = { | ||
maxRetry, | ||
method: 'GET', | ||
}; | ||
method: 'GET' | ||
} | ||
return promiseDataTo(config); | ||
}; | ||
return promiseDataTo(config) | ||
} | ||
module.exports = { promiseGet }; | ||
module.exports = { promiseGet } |
const setHeaders = (headers, data, compress) => { | ||
const headerKeys = Object.keys(headers); | ||
const headerKeys = Object.keys(headers) | ||
const contentType = headerKeys.find(key => key.toLowerCase() === 'content-type'); | ||
if (!headers[contentType]) headers['content-type'] = 'application/json'; | ||
const contentType = headerKeys.find(key => key.toLowerCase() === 'content-type') | ||
if (!headers[contentType]) headers['content-type'] = 'application/json' | ||
if (data) headers['content-length'] = Buffer.byteLength(data); | ||
if (data) headers['content-length'] = Buffer.byteLength(data) | ||
if (compress && ['deflate', 'gzip'].includes(compress)) { | ||
headers['content-encoding'] = compress; | ||
headers['content-encoding'] = compress | ||
} | ||
}; | ||
} | ||
module.exports = { setHeaders }; | ||
module.exports = { setHeaders } |
@@ -1,15 +0,15 @@ | ||
const zlib = require('zlib'); | ||
const zlib = require('zlib') | ||
const setResTxt = (res, buffer) => new Promise((resolve, reject) => { | ||
if (res.headers['content-encoding'] !== 'gzip') { | ||
return resolve(buffer.toString('utf-8')); | ||
return resolve(buffer.toString('utf-8')) | ||
} | ||
zlib.gunzip(buffer, (err, buff) => { | ||
if (err) return reject(err); | ||
if (err) return reject(err) | ||
resolve(Buffer.from(buff).toString('utf-8')); | ||
}); | ||
}); | ||
resolve(Buffer.from(buff).toString('utf-8')) | ||
}) | ||
}) | ||
module.exports = { setResTxt }; | ||
module.exports = { setResTxt } |
@@ -1,4 +0,4 @@ | ||
const logr = require('@everymundo/simple-logr'); | ||
const logr = require('@everymundo/simple-logr') | ||
function simulatedResponse(start, endpoint, xData) { | ||
function simulatedResponse (start, endpoint, xData) { | ||
const | ||
@@ -12,9 +12,10 @@ stats = { | ||
dataType: xData && xData.constructor.name, | ||
dataLen: Array.isArray(xData) && xData.length, | ||
}; | ||
dataLen: Array.isArray(xData) && xData.length | ||
} | ||
logr.info(`process.env.SIMULATE=${process.env.SIMULATE}`); | ||
return stats; | ||
logr.info(`process.env.SIMULATE=${process.env.SIMULATE}`) | ||
return stats | ||
} | ||
module.exports = { simulatedResponse }; | ||
module.exports = { simulatedResponse } |
@@ -1,13 +0,13 @@ | ||
'use strict'; | ||
'use strict' | ||
/* eslint-disable no-console */ | ||
const protos = { 'http:': require('http'), 'https:': require('https') }; | ||
const parseUrl = require('url').parse; | ||
const protos = { 'http:': require('http'), 'https:': require('https') } | ||
const parseUrl = require('url').parse | ||
const urlToEndpoint = (url, headers = {}) => { | ||
if (!/^https?:/.test(url)) { | ||
throw new Error(`Invalid endpoint url [${url}]`); | ||
throw new Error(`Invalid endpoint url [${url}]`) | ||
} | ||
const { protocol, hostname, port, path, auth, search } = parseUrl(url); | ||
const { protocol, hostname, port, path, auth, search } = parseUrl(url) | ||
const configs = { | ||
@@ -20,16 +20,16 @@ http: protos[protocol], | ||
path, | ||
endpoint: url, | ||
}; | ||
endpoint: url | ||
} | ||
if (!(headers instanceof Object)) configs.headers = {}; | ||
if (!(headers instanceof Object)) configs.headers = {} | ||
// auth | ||
if (auth) { | ||
configs.headers.Authorization = auth.replace(':', ' '); | ||
configs.endpoint = configs.endpoint.replace(`${auth}@`, ''); | ||
configs.headers.Authorization = auth.replace(':', ' ') | ||
configs.endpoint = configs.endpoint.replace(`${auth}@`, '') | ||
} | ||
return configs; | ||
}; | ||
return configs | ||
} | ||
module.exports = { urlToEndpoint }; | ||
module.exports = { urlToEndpoint } |
{ | ||
"name": "@everymundo/promise-data-to", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "This is a helper to perform POST requests using promises and no external dependencies", | ||
@@ -12,4 +12,4 @@ "main": "index.js", | ||
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"check-lint": "eslint *.js lib/*.js test/*.js", | ||
"fix-lint": "eslint --fix *.js lib/*.js test/*.js", | ||
"check-lint": "standard *.js lib/*.js test/*.js", | ||
"fix-lint": "standard --fix *.js lib/*.js test/*.js", | ||
"test": "RETRY_TIMEOUT_MS= MAX_RETRY_ATTEMPTS= SIMULATE= mocha" | ||
@@ -41,14 +41,15 @@ }, | ||
"devDependencies": { | ||
"@everymundo/cleanrequire": "^1.1.1", | ||
"@everymundo/cleanrequire": "^1.2.1", | ||
"@everymundo/simple-clone": "^1.1.0", | ||
"chai": "^4.1.2", | ||
"chai": "^4.2.0", | ||
"ghooks": "^2.0.4", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.2.0", | ||
"sinon": "^4.5.0" | ||
"mocha": "^6.1.4", | ||
"sinon": "^7.3.2", | ||
"standard": "^12.0.1" | ||
}, | ||
"dependencies": { | ||
"@everymundo/json-utils": "^1.0.0", | ||
"@everymundo/simple-logr": "^1.1.1" | ||
"@everymundo/simple-logr": "^1.2.2" | ||
} | ||
} |
# promise-data-to | ||
This is a helper to perform POST requests using promises. | ||
## Install | ||
```sh | ||
npm install @everymundo/promise-data-to | ||
``` |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -8,44 +8,48 @@ /* eslint-env mocha */ | ||
const | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
cleanrequire = require('@everymundo/cleanrequire'), | ||
loadLib = () => cleanrequire('../index.js'), | ||
noop = () => { }; | ||
{ sandbox } = require('sinon') | ||
const { expect } = require('chai') | ||
const cleanrequire = require('@everymundo/cleanrequire') | ||
const loadLib = () => cleanrequire('../index.js') | ||
const noop = () => { } | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
const logr = require('@everymundo/simple-logr'); | ||
const logr = require('@everymundo/simple-logr') | ||
beforeEach(() => { | ||
box = sandbox.create(); | ||
['log', 'info', /* 'debug', */'error'] | ||
.forEach(method => box.stub(logr, method).callsFake(noop)); | ||
}); | ||
.forEach(method => box.stub(logr, method).callsFake(noop)) | ||
}) | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
context('exported keys and types', () => { | ||
const expected = { | ||
promiseDataTo: Function, | ||
promiseDataTo: Function, | ||
parseEndpoints: Function, | ||
urlToEndpoint: Function, | ||
promiseGet: Function, | ||
}; | ||
urlToEndpoint: Function, | ||
promiseGet: Function | ||
} | ||
it('should export the expected keys', () => { | ||
const lib = loadLib(); | ||
const lib = loadLib() | ||
const libKeys = Object.keys(lib); | ||
const libKeys = Object.keys(lib) | ||
const expectedKeys = Object.keys(expected); | ||
const expectedKeys = Object.keys(expected) | ||
expect(libKeys).to.deep.equal(expectedKeys); | ||
}); | ||
expect(libKeys).to.deep.equal(expectedKeys) | ||
}) | ||
it('should export the expected keys', () => { | ||
const lib = loadLib(); | ||
const lib = loadLib() | ||
Object.keys(expected).forEach(key => expect(lib[key]).to.be.instanceof(expected[key])); | ||
}); | ||
}); | ||
}); | ||
Object.keys(expected).forEach(key => expect(lib[key]).to.be.instanceof(expected[key])) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,84 +7,77 @@ /* eslint-env mocha */ | ||
describe('lib/addQueryToPath.js', () => { | ||
const | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
// noop = () => { }, | ||
loadLib = () => require('../lib/add-query-to-path'); | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
// noop = () => { }, | ||
const loadLib = () => require('../lib/add-query-to-path') | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
beforeEach(() => { box = sandbox.create(); }); | ||
afterEach(() => { box.restore(); }); | ||
beforeEach(() => { box = sinon.createSandbox() }) | ||
afterEach(() => { box.restore() }) | ||
describe('#addQueryToPath', () => { | ||
const { addQueryToPath } = loadLib(); | ||
const { addQueryToPath } = loadLib() | ||
context('when query is empty', () => { | ||
it('should return inputPath', () => { | ||
const query = undefined; | ||
const path = '/something'; | ||
const query = undefined | ||
const path = '/something' | ||
const result = addQueryToPath(query, path); | ||
const result = addQueryToPath(query, path) | ||
expect(result).to.equal(path); | ||
}); | ||
}); | ||
expect(result).to.equal(path) | ||
}) | ||
}) | ||
context('when query is an object', () => { | ||
it('should return the correct concatenated string', () => { | ||
const | ||
query = {var1: 'value1', var2: 'v2'}, | ||
path = '/something', | ||
expected = `${path}?var1=value1&var2=v2`; | ||
const query = { var1: 'value1', var2: 'v2' } | ||
const path = '/something' | ||
const expected = `${path}?var1=value1&var2=v2` | ||
const result = addQueryToPath(query, path) | ||
const result = addQueryToPath(query, path); | ||
expect(result).to.equal(expected) | ||
}) | ||
}) | ||
expect(result).to.equal(expected); | ||
}); | ||
}); | ||
context('when query is a string', () => { | ||
context('that already includes ?', () => { | ||
it('should return the correct concatenated string', () => { | ||
const | ||
query = '?name=daniel&descr=awesome', | ||
path = '/something', | ||
expected = '/something?name=daniel&descr=awesome'; | ||
const query = '?name=daniel&descr=awesome' | ||
const path = '/something' | ||
const expected = '/something?name=daniel&descr=awesome' | ||
const result = addQueryToPath(query, path) | ||
const result = addQueryToPath(query, path); | ||
expect(result).to.equal(expected) | ||
}) | ||
}) | ||
expect(result).to.equal(expected); | ||
}); | ||
}); | ||
context('that DOES NOT include ?', () => { | ||
it('should return the correct concatenated string', () => { | ||
const | ||
query = 'name=daniel&descr=awesome', | ||
path = '/something', | ||
expected = '/something?name=daniel&descr=awesome'; | ||
const query = 'name=daniel&descr=awesome' | ||
const path = '/something' | ||
const expected = '/something?name=daniel&descr=awesome' | ||
const result = addQueryToPath(query, path) | ||
const result = addQueryToPath(query, path); | ||
expect(result).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
expect(result).to.equal(expected); | ||
}); | ||
}); | ||
}); | ||
context('when path already includes the ?', () => { | ||
it('should return the correct concatenated string', () => { | ||
const | ||
query = '?name=daniel&descr=awesome', | ||
path = '/something?crazy=true', | ||
expected = '/something?crazy=true&name=daniel&descr=awesome'; | ||
const query = '?name=daniel&descr=awesome' | ||
const path = '/something?crazy=true' | ||
const expected = '/something?crazy=true&name=daniel&descr=awesome' | ||
const result = addQueryToPath(query, path) | ||
const result = addQueryToPath(query, path); | ||
expect(result).to.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(result).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,19 +7,20 @@ /* eslint-env mocha */ | ||
describe('lib/get-data-from-xdata.js', () => { | ||
const | ||
zlib = require('zlib'), | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
// noop = () => { }, | ||
loadLib = () => require('../lib/get-data-from-xdata'); | ||
const zlib = require('zlib') | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
// noop = () => { }, | ||
const loadLib = () => require('../lib/get-data-from-xdata') | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
beforeEach(() => { box = sandbox.create(); }); | ||
afterEach(() => { box.restore(); }); | ||
beforeEach(() => { box = sinon.createSandbox() }) | ||
afterEach(() => { box.restore() }) | ||
describe('#getDataFromXData', () => { | ||
const { getDataFromXData, validCompressionTypes } = loadLib(); | ||
const { getDataFromXData, validCompressionTypes } = loadLib() | ||
@@ -29,38 +30,38 @@ context('Without Compression', () => { | ||
it('should return the exact same string', () => { | ||
const input = 'simple string'; | ||
const input = 'simple string' | ||
const result = getDataFromXData(input); | ||
const result = getDataFromXData(input) | ||
expect(result).to.equal(input); | ||
}); | ||
}); | ||
expect(result).to.equal(input) | ||
}) | ||
}) | ||
context('when the input data is an Array', () => { | ||
it('should return a json representation of it', () => { | ||
const input = [1, 2, 3, 4]; | ||
const expected = JSON.stringify(input); | ||
const result = getDataFromXData(input); | ||
const input = [1, 2, 3, 4] | ||
const expected = JSON.stringify(input) | ||
const result = getDataFromXData(input) | ||
expect(result).to.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.equal(expected) | ||
}) | ||
}) | ||
context('when the input data is a plain Object', () => { | ||
it('should return a json representation of it', () => { | ||
const input = { a: 1, b: 2, c: 3, d: 4 }; | ||
const expected = JSON.stringify(input); | ||
const result = getDataFromXData(input); | ||
const input = { a: 1, b: 2, c: 3, d: 4 } | ||
const expected = JSON.stringify(input) | ||
const result = getDataFromXData(input) | ||
expect(result).to.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.equal(expected) | ||
}) | ||
}) | ||
context('when the input data is a Buffer', () => { | ||
it('should return the very same Buffer', () => { | ||
const input = Buffer.from('{ a: 1, b: 2, c: 3, d: 4 }'); | ||
const result = getDataFromXData(input); | ||
const input = Buffer.from('{ a: 1, b: 2, c: 3, d: 4 }') | ||
const result = getDataFromXData(input) | ||
expect(result).to.equal(input); | ||
}); | ||
}); | ||
expect(result).to.equal(input) | ||
}) | ||
}) | ||
}); | ||
@@ -72,50 +73,50 @@ | ||
it('should return the GZIPPED version of the given string', () => { | ||
const input = 'simple string'; | ||
const expected = zlib[`${compress}Sync`](input); | ||
const input = 'simple string' | ||
const expected = zlib[`${compress}Sync`](input) | ||
const result = getDataFromXData(input, compress); | ||
const result = getDataFromXData(input, compress) | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`); | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match'); | ||
}); | ||
}); | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`) | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match') | ||
}) | ||
}) | ||
context('when the input data is an Array', () => { | ||
it('should return the GZIPPED version of the json representation of it', () => { | ||
const input = [1, 2, 3, 4]; | ||
const expected = zlib[`${compress}Sync`](JSON.stringify(input)); | ||
const input = [1, 2, 3, 4] | ||
const expected = zlib[`${compress}Sync`](JSON.stringify(input)) | ||
const result = getDataFromXData(input, compress); | ||
const result = getDataFromXData(input, compress) | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`); | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match'); | ||
}); | ||
}); | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`) | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match') | ||
}) | ||
}) | ||
context('when the input data is a plain Object', () => { | ||
it('should return the GZIPPED version of the json representation of it', () => { | ||
const input = { a: 1, b: 2, c: 3, d: 4 }; | ||
const expected = zlib[`${compress}Sync`](JSON.stringify(input)); | ||
const input = { a: 1, b: 2, c: 3, d: 4 } | ||
const expected = zlib[`${compress}Sync`](JSON.stringify(input)) | ||
const result = getDataFromXData(input, compress); | ||
const result = getDataFromXData(input, compress) | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`); | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match'); | ||
}); | ||
}); | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`) | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match') | ||
}) | ||
}) | ||
context('when the input data is a Buffer', () => { | ||
it('should return the very same Buffer', () => { | ||
const input = Buffer.from('{ a: 1, b: 2, c: 3, d: 4 }'); | ||
const expected = zlib[`${compress}Sync`](input); | ||
const input = Buffer.from('{ a: 1, b: 2, c: 3, d: 4 }') | ||
const expected = zlib[`${compress}Sync`](input) | ||
const result = getDataFromXData(input, compress); | ||
const result = getDataFromXData(input, compress) | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`); | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(result).to.be.instanceof(Buffer, `=> ${result} ${validCompressionTypes}`) | ||
expect(Buffer.compare(result, expected)).to.equal(0, 'Buffers do not match') | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,28 +7,29 @@ /* eslint-env mocha */ | ||
describe('lib/parse-endpoints.js', () => { | ||
const | ||
http = require('http'), | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
// noop = () => { }, | ||
loadLib = () => require('../lib/parse-endpoints.js'); | ||
const http = require('http') | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
// noop = () => { }, | ||
const loadLib = () => require('../lib/parse-endpoints.js') | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
// const logr = require('@everymundo/simple-logr'); | ||
if (!('MAIN_ENDPOINT' in process.env)) process.env.MAIN_ENDPOINT = ''; | ||
if (!('MAIN_ENDPOINT_HEADERS' in process.env)) process.env.MAIN_ENDPOINT_HEADERS = ''; | ||
if (!('MAIN_ENDPOINT' in process.env)) process.env.MAIN_ENDPOINT = '' | ||
if (!('MAIN_ENDPOINT_HEADERS' in process.env)) process.env.MAIN_ENDPOINT_HEADERS = '' | ||
beforeEach(() => { | ||
box = sandbox.create(); | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path'); | ||
}); | ||
box = sinon.createSandbox() | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path') | ||
}) | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
context('parseEndpoints', () => { | ||
const { parseEndpoints } = loadLib(); | ||
const { parseEndpoints } = loadLib() | ||
@@ -39,20 +40,20 @@ context('when receiving and INVALID url =>', () => { | ||
box.stub(process.env, 'MAIN_ENDPOINT') | ||
.value('tcp://test.com/some/path'); | ||
}); | ||
.value('tcp://test.com/some/path') | ||
}) | ||
it('show throw an error', () => { | ||
const caller = () => parseEndpoints(); | ||
const caller = () => parseEndpoints() | ||
expect(caller).to.throw(Error, 'has an invalid endpoint'); | ||
}); | ||
}); | ||
}); | ||
expect(caller).to.throw(Error, 'has an invalid endpoint') | ||
}) | ||
}) | ||
}) | ||
context('when not receiving any argument =>', () => { | ||
beforeEach(() => { | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path'); | ||
}); | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path') | ||
}) | ||
it('should return an object with the expected properties', () => { | ||
const result = parseEndpoints(); | ||
const result = parseEndpoints() | ||
@@ -67,19 +68,18 @@ const expected = { | ||
port: null, | ||
compress: undefined, | ||
}], | ||
}; | ||
compress: undefined | ||
}] | ||
} | ||
expect(result).to.deep.equal(expected); | ||
expect(Object.keys(result)).to.deep.equal(Object.keys(expected)); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
expect(Object.keys(result)).to.deep.equal(Object.keys(expected)) | ||
}) | ||
}) | ||
context('when receiving a regexp without grouping', () => { | ||
beforeEach(() => { | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path'); | ||
}); | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path') | ||
}) | ||
it('should return an object with the key being the whole variable name', () => { | ||
const result = parseEndpoints(/^MAIN_ENDPOINT$/); | ||
const result = parseEndpoints(/^MAIN_ENDPOINT$/) | ||
@@ -94,25 +94,25 @@ const expected = { | ||
port: null, | ||
compress: undefined, | ||
}], | ||
}; | ||
compress: undefined | ||
}] | ||
} | ||
expect(result).to.deep.equal(expected); | ||
expect(Object.keys(result)).to.deep.equal(Object.keys(expected)); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
expect(Object.keys(result)).to.deep.equal(Object.keys(expected)) | ||
}) | ||
}) | ||
context('http url with credentials and no headers', () => { | ||
beforeEach(() => { | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://Bearer:token@test.com/some/path'); | ||
}); | ||
box.stub(process.env, 'MAIN_ENDPOINT').value('http://Bearer:token@test.com/some/path') | ||
}) | ||
it('should return an object with the expected properties', () => { | ||
const result = parseEndpoints(); | ||
const result = parseEndpoints() | ||
const expected = { | ||
MAIN:[{ | ||
MAIN: [{ | ||
http, | ||
endpoint: 'http://test.com/some/path', | ||
headers: { | ||
Authorization: 'Bearer token', | ||
Authorization: 'Bearer token' | ||
}, | ||
@@ -122,9 +122,9 @@ host: 'test.com', | ||
port: null, | ||
compress: undefined, | ||
}], | ||
}; | ||
compress: undefined | ||
}] | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
@@ -134,9 +134,9 @@ context('http url with credentials AND with headers', () => { | ||
box.stub(process.env, 'MAIN_ENDPOINT') | ||
.value('http://Bearer:token@test.com/some/path'); | ||
.value('http://Bearer:token@test.com/some/path') | ||
box.stub(process.env, 'MAIN_ENDPOINT_HEADERS') | ||
.value('{"content-type":"application/xml","content-encoding":"gzip"}'); | ||
}); | ||
.value('{"content-type":"application/xml","content-encoding":"gzip"}') | ||
}) | ||
it('should return an object with the expected properties', () => { | ||
const result = parseEndpoints(); | ||
const result = parseEndpoints() | ||
@@ -150,3 +150,3 @@ const expected = { | ||
'content-type': 'application/xml', | ||
'content-encoding': 'gzip', | ||
'content-encoding': 'gzip' | ||
}, | ||
@@ -156,9 +156,9 @@ host: 'test.com', | ||
port: null, | ||
compress: 'gzip', | ||
}], | ||
}; | ||
compress: 'gzip' | ||
}] | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
@@ -168,9 +168,9 @@ context('http simple url with headers', () => { | ||
box.stub(process.env, 'MAIN_ENDPOINT') | ||
.value('http://test.com/some/path'); | ||
.value('http://test.com/some/path') | ||
box.stub(process.env, 'MAIN_ENDPOINT_HEADERS') | ||
.value('{"content-type":"application/xml"}'); | ||
}); | ||
.value('{"content-type":"application/xml"}') | ||
}) | ||
it('should return an object with the expected properties', () => { | ||
const result = parseEndpoints(); | ||
const result = parseEndpoints() | ||
@@ -182,3 +182,3 @@ const expected = { | ||
headers: { | ||
'content-type': 'application/xml', | ||
'content-type': 'application/xml' | ||
}, | ||
@@ -188,9 +188,9 @@ host: 'test.com', | ||
port: null, | ||
compress: undefined, | ||
}], | ||
}; | ||
compress: undefined | ||
}] | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
@@ -200,13 +200,13 @@ context('http simple url but INVALID headers', () => { | ||
box.stub(process.env, 'MAIN_ENDPOINT') | ||
.value('http://test.com/some/path'); | ||
.value('http://test.com/some/path') | ||
box.stub(process.env, 'MAIN_ENDPOINT_HEADERS') | ||
.value('{not-a-valid-json-key:"application/xml"}'); | ||
}); | ||
.value('{not-a-valid-json-key:"application/xml"}') | ||
}) | ||
it('should return an object with the expected properties', () => { | ||
const caller = () => parseEndpoints(); | ||
const caller = () => parseEndpoints() | ||
expect(caller).to.throw(Error); | ||
}); | ||
}); | ||
expect(caller).to.throw(Error) | ||
}) | ||
}) | ||
@@ -216,14 +216,14 @@ context('Regexp too generic and matches multiple ENV Vars', () => { | ||
box.stub(process.env, 'MAIN_ENDPOINT') | ||
.value('http://test.com/some/path'); | ||
.value('http://test.com/some/path') | ||
box.stub(process.env, 'MAIN_ENDPOINT_HEADERS') | ||
.value('{"content-type":"application/xml"}'); | ||
}); | ||
.value('{"content-type":"application/xml"}') | ||
}) | ||
it('should return an object with the expected properties', () => { | ||
const caller = () => parseEndpoints(/MAIN/); | ||
const caller = () => parseEndpoints(/MAIN/) | ||
expect(caller).to.throw(Error, 'present twice'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(caller).to.throw(Error, 'present twice') | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,54 +7,55 @@ /* eslint-env mocha */ | ||
describe('promise-data-to', () => { | ||
const | ||
sinon = require('sinon'), | ||
{ sandbox, spy } = require('sinon'), | ||
{ expect } = require('chai'), | ||
{ clone } = require('@everymundo/simple-clone'), | ||
cleanrequire = require('@everymundo/cleanrequire'), | ||
loadLib = () => cleanrequire('../lib/promise-data-to'), | ||
noop = () => { }; | ||
const sinon = require('sinon') | ||
const { spy } = require('sinon') | ||
const { expect } = require('chai') | ||
const { clone } = require('@everymundo/simple-clone') | ||
const cleanrequire = require('@everymundo/cleanrequire') | ||
const loadLib = () => cleanrequire('../lib/promise-data-to') | ||
const noop = () => { } | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
const logr = require('@everymundo/simple-logr'); | ||
const zlib = require('zlib'); | ||
const logr = require('@everymundo/simple-logr') | ||
const zlib = require('zlib') | ||
beforeEach(() => { | ||
box = sandbox.create(); | ||
box = sinon.createSandbox(); | ||
['log', 'info', /* 'debug', */'error'] | ||
.forEach(method => box.stub(logr, method).callsFake(noop)); | ||
}); | ||
.forEach(method => box.stub(logr, method).callsFake(noop)) | ||
}) | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
describe('#promiseDataTo', () => { | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let httpRequest, httpEmitter, httpResponse; | ||
let httpRequest, httpEmitter, httpResponse | ||
const | ||
http = require('http'), | ||
{ EventEmitter } = require('events'), | ||
newEmitter = () => { | ||
const emitter = new EventEmitter(); | ||
emitter.headers = {}; | ||
emitter.setEncoding = noop; | ||
const http = require('http') | ||
const { EventEmitter } = require('events') | ||
return emitter; | ||
}, | ||
newHttpEmitter = (response) => { | ||
const emitter = newEmitter(); | ||
const newEmitter = () => { | ||
const emitter = new EventEmitter() | ||
emitter.headers = {} | ||
emitter.setEncoding = noop | ||
emitter.response = response; | ||
emitter.write = data => response.emit('data', Buffer.from(data)); | ||
emitter.end = () => response.emit('end'); | ||
return emitter | ||
} | ||
return emitter; | ||
}; | ||
const newHttpEmitter = (response) => { | ||
const emitter = newEmitter() | ||
emitter.response = response | ||
emitter.write = data => response.emit('data', Buffer.from(data)) | ||
emitter.end = () => response.emit('end') | ||
return emitter | ||
} | ||
beforeEach(() => { | ||
httpRequest = box.stub(http, 'request'); | ||
httpResponse = newEmitter(); | ||
httpEmitter = newHttpEmitter(httpResponse); | ||
}); | ||
httpRequest = box.stub(http, 'request') | ||
httpResponse = newEmitter() | ||
httpEmitter = newHttpEmitter(httpResponse) | ||
}) | ||
@@ -67,238 +68,245 @@ const config = { | ||
endpoint: 'http://localhost:80/path', | ||
headers: { Authorization: 'Authorization' }, | ||
}; | ||
headers: { Authorization: 'Authorization' } | ||
} | ||
context('Calling setHeaders', () => { | ||
const libSetHeaders = cleanrequire('../lib/set-headers.js'); | ||
const libSetHeaders = cleanrequire('../lib/set-headers.js') | ||
beforeEach(() => { | ||
box.stub(libSetHeaders, 'setHeaders'); | ||
}); | ||
box.stub(libSetHeaders, 'setHeaders') | ||
}) | ||
it('should call setHeaders passing the correct arguments', () => { | ||
const { setHeaders } = libSetHeaders; | ||
const data = { a: 1, b: 2, c: 3 }; | ||
const expectedData = require('zlib').gzipSync(JSON.stringify(data)); | ||
const { setHeaders } = libSetHeaders | ||
const data = { a: 1, b: 2, c: 3 } | ||
const expectedData = require('zlib').gzipSync(JSON.stringify(data)) | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const customConfig = clone(config); | ||
const { headers } = customConfig; | ||
const { promiseDataTo } = loadLib() | ||
const customConfig = clone(config) | ||
const { headers } = customConfig | ||
customConfig.http = http; | ||
customConfig.compress = 'gzip'; | ||
customConfig.http = http | ||
customConfig.compress = 'gzip' | ||
return promiseDataTo(customConfig, data) | ||
.then(() => { | ||
expect(setHeaders).to.have.property('calledOnce', true); | ||
expect(setHeaders).to.have.property('calledOnce', true) | ||
// expect(libSetHeaders.setHeaders.calledWith(headers, expectedData, 'gzip')).to.be.true; | ||
sinon.assert.calledWith(setHeaders, headers, expectedData, 'gzip'); | ||
sinon.assert.calledWith(setHeaders, headers, expectedData, 'gzip') | ||
// expect(.calledWith(headers, expectedData, 'gzip')).to.be.true; | ||
}); | ||
}); | ||
}); | ||
}) | ||
}) | ||
}) | ||
it('should success when status is between 200 and 299 using http', () => { | ||
const | ||
data = { a: 1, b: 2, c: 3 }, | ||
expectedData = JSON.stringify(data); | ||
data = { a: 1, b: 2, c: 3 } | ||
const expectedData = JSON.stringify(data) | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
return promiseDataTo(config, data) | ||
.then((stats) => { | ||
expect(stats.code).to.equal(200); | ||
expect(stats).to.have.property('resTxt', expectedData); | ||
expect(stats).to.not.have.property('err'); | ||
}); | ||
}); | ||
expect(stats.code).to.equal(200) | ||
expect(stats).to.have.property('resTxt', expectedData) | ||
expect(stats).to.not.have.property('err') | ||
}) | ||
}) | ||
it('should success when status is between 200 and 299 using protocol', () => { | ||
const | ||
data = { a: 1, b: 2, c: 3 }, | ||
expectedData = JSON.stringify(data); | ||
data = { a: 1, b: 2, c: 3 } | ||
const expectedData = JSON.stringify(data) | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const protoConfig = clone(config); | ||
delete protoConfig.http; | ||
protoConfig.protocol = 'http:'; | ||
const { promiseDataTo } = loadLib() | ||
const protoConfig = clone(config) | ||
delete protoConfig.http | ||
protoConfig.protocol = 'http:' | ||
return promiseDataTo(protoConfig, data) | ||
.then((stats) => { | ||
expect(stats.code).to.equal(200); | ||
expect(stats).to.have.property('resTxt', expectedData); | ||
expect(stats).to.not.have.property('err'); | ||
}); | ||
}); | ||
expect(stats.code).to.equal(200) | ||
expect(stats).to.have.property('resTxt', expectedData) | ||
expect(stats).to.not.have.property('err') | ||
}) | ||
}) | ||
it('should success when status is between 200 and 299 using protocol and query', () => { | ||
const | ||
data = { a: 1, b: 2, c: 3 }, | ||
expectedData = JSON.stringify(data); | ||
data = { a: 1, b: 2, c: 3 } | ||
const expectedData = JSON.stringify(data) | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const protoConfig = clone(config); | ||
delete protoConfig.http; | ||
protoConfig.protocol = 'http:'; | ||
protoConfig.query = {name:'Daniel', features:['awesome', 'handsome']}; | ||
const { promiseDataTo } = loadLib() | ||
const protoConfig = clone(config) | ||
delete protoConfig.http | ||
protoConfig.protocol = 'http:' | ||
protoConfig.query = { name: 'Daniel', features: ['awesome', 'handsome'] } | ||
return promiseDataTo(protoConfig, data) | ||
.then((stats) => { | ||
expect(stats.code).to.equal(200); | ||
expect(stats).to.have.property('resTxt', expectedData); | ||
expect(stats).to.not.have.property('err'); | ||
}); | ||
}); | ||
expect(stats.code).to.equal(200) | ||
expect(stats).to.have.property('resTxt', expectedData) | ||
expect(stats).to.not.have.property('err') | ||
}) | ||
}) | ||
it('should success when status is between 200 and 299 using protocol and method=GET', () => { | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const protoConfig = clone(config); | ||
delete protoConfig.http; | ||
protoConfig.protocol = 'http:'; | ||
protoConfig.method = 'GET'; | ||
const { promiseDataTo } = loadLib() | ||
const protoConfig = clone(config) | ||
delete protoConfig.http | ||
protoConfig.protocol = 'http:' | ||
protoConfig.method = 'GET' | ||
return promiseDataTo(protoConfig) | ||
.then((stats) => { | ||
expect(stats.code).to.equal(200); | ||
expect(stats).to.have.property('resTxt', ''); | ||
expect(stats).to.not.have.property('err'); | ||
}); | ||
}); | ||
expect(stats.code).to.equal(200) | ||
expect(stats).to.have.property('resTxt', '') | ||
expect(stats).to.not.have.property('err') | ||
}) | ||
}) | ||
it('should success when status is between 200 and 299 and response is GZIP', () => { | ||
const expected = JSON.stringify({name: 'Daniel', awesome: true}); | ||
const expected = JSON.stringify({ name: 'Daniel', awesome: true }) | ||
httpEmitter.write = function write() { | ||
this.response.emit('data', zlib.gzipSync(expected)); | ||
}; | ||
httpEmitter.write = function write () { | ||
this.response.emit('data', zlib.gzipSync(expected)) | ||
} | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
httpResponse.headers = {'content-encoding': 'gzip'}; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
httpResponse.headers = { 'content-encoding': 'gzip' } | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
return promiseDataTo(config, {}) | ||
.then((stats) => { | ||
expect(stats.code).to.equal(200); | ||
expect(stats).to.have.property('resTxt', expected); | ||
expect(stats).to.not.have.property('err'); | ||
}); | ||
}); | ||
expect(stats.code).to.equal(200) | ||
expect(stats).to.have.property('resTxt', expected) | ||
expect(stats).to.not.have.property('err') | ||
}) | ||
}) | ||
it('should REJECT when status 2** and response GZIP throws and error', () => { | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
httpResponse.headers = {'content-encoding': 'gzip'}; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 200 | ||
httpResponse.headers = { 'content-encoding': 'gzip' } | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
const secret = Math.random(); | ||
const secret = Math.random() | ||
return promiseDataTo(config, 'something') | ||
.catch((error) => { | ||
expect(error).to.be.instanceof(Error); | ||
expect(error).to.have.property('message', 'incorrect header check'); | ||
expect(error).to.be.instanceof(Error) | ||
expect(error).to.have.property('message', 'incorrect header check') | ||
return secret; | ||
return secret | ||
}) | ||
.then(res => expect(res).to.equal(secret, 'It did not reject')); | ||
}); | ||
.then(res => expect(res).to.equal(secret, 'It did not reject')) | ||
}) | ||
it('should reject when status is between 400', () => { | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 400; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 400 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
const secret = Math.random(); | ||
const secret = Math.random() | ||
return promiseDataTo(config, '') | ||
.catch((error) => { | ||
expect(error).to.be.instanceof(Error); | ||
expect(error).to.have.property('message', '400 Status'); | ||
expect(error).to.be.instanceof(Error) | ||
expect(error).to.have.property('message', '400 Status') | ||
return secret; | ||
return secret | ||
}) | ||
.then(res => expect(res).to.equal(secret, 'It did not reject')); | ||
}); | ||
.then(res => expect(res).to.equal(secret, 'It did not reject')) | ||
}) | ||
context('when process.env.SIMULATE is set', () => { | ||
const simulateLib = require('../lib/simulate-response'); | ||
const simulateLib = require('../lib/simulate-response') | ||
beforeEach(() => { | ||
box.stub(process.env, 'SIMULATE').value('1'); | ||
spy(simulateLib, 'simulatedResponse'); | ||
}); | ||
box.stub(process.env, 'SIMULATE').value('1') | ||
spy(simulateLib, 'simulatedResponse') | ||
}) | ||
afterEach(() => { }); | ||
afterEach(() => { }) | ||
it('should succeed', () => { | ||
const | ||
dataObject = { a: 1, b: 2, c: 3 }, | ||
dataArray = [dataObject], | ||
dataString = JSON.stringify(dataArray), | ||
expectedData = '{"success":true,"data":[]}', | ||
invalidAttempt = NaN; | ||
dataObject = { a: 1, b: 2, c: 3 } | ||
const dataArray = [dataObject] | ||
const dataString = JSON.stringify(dataArray) | ||
const expectedData = '{"success":true,"data":[]}' | ||
const invalidAttempt = NaN | ||
httpRequest.callsFake((options, callback) => { | ||
callback(httpResponse); | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const thenFunction = spy((stats) => { | ||
expect(stats.code).to.equal(222); | ||
expect(stats).to.have.property('resTxt', expectedData); | ||
expect(stats).to.have.property('start'); | ||
expect(stats).to.have.property('end'); | ||
expect(stats).to.not.have.property('err'); | ||
}); | ||
expect(stats.code).to.equal(222) | ||
expect(stats).to.have.property('resTxt', expectedData) | ||
expect(stats).to.have.property('start') | ||
expect(stats).to.have.property('end') | ||
expect(stats).to.not.have.property('err') | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
@@ -312,109 +320,112 @@ return promiseDataTo(config, dataObject) | ||
.then(() => { | ||
expect(thenFunction).to.have.property('calledThrice', true); | ||
expect(simulateLib.simulatedResponse).to.have.property('calledThrice', true); | ||
}); | ||
}); | ||
}); | ||
expect(thenFunction).to.have.property('calledThrice', true) | ||
expect(simulateLib.simulatedResponse).to.have.property('calledThrice', true) | ||
}) | ||
}) | ||
}) | ||
context('when status is is between 300 and 399', () => { | ||
beforeEach(() => { | ||
}); | ||
}) | ||
it('should fail', () => { | ||
const | ||
data = [{ a: 1, b: 2, c: 3 }], | ||
expectedData = JSON.stringify(data); | ||
data = [{ a: 1, b: 2, c: 3 }] | ||
const expectedData = JSON.stringify(data) | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 302; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 302 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
return promiseDataTo(config, data) | ||
.then((stats) => { | ||
expect(stats.code).to.equal(302); | ||
expect(stats).to.have.property('err'); | ||
expect(stats).to.have.property('resTxt', expectedData); | ||
}); | ||
}); | ||
}); | ||
expect(stats.code).to.equal(302) | ||
expect(stats).to.have.property('err') | ||
expect(stats).to.have.property('resTxt', expectedData) | ||
}) | ||
}) | ||
}) | ||
context('when status >= 400', () => { | ||
beforeEach(() => { | ||
box.stub(process.env, 'MAX_RETRY_ATTEMPTS').value('1'); | ||
box.stub(process.env, 'RETRY_TIMEOUT_MS').value('0'); | ||
}); | ||
box.stub(process.env, 'MAX_RETRY_ATTEMPTS').value('1') | ||
box.stub(process.env, 'RETRY_TIMEOUT_MS').value('0') | ||
}) | ||
it('should fail', (done) => { | ||
const | ||
data = { a: 1, b: 2 }, | ||
expectedData = JSON.stringify(data); | ||
data = { a: 1, b: 2 } | ||
const expectedData = JSON.stringify(data) | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 404; | ||
callback(httpResponse); | ||
httpResponse.statusCode = 404 | ||
callback(httpResponse) | ||
return httpEmitter; | ||
}); | ||
return httpEmitter | ||
}) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
promiseDataTo(config, data) | ||
.catch((stats) => { | ||
expect(stats).to.have.property('err'); | ||
expect(stats).to.have.property('resTxt', expectedData); | ||
expect(stats.code).to.equal(404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
expect(stats).to.have.property('err') | ||
expect(stats).to.have.property('resTxt', expectedData) | ||
expect(stats.code).to.equal(404) | ||
done() | ||
}) | ||
}) | ||
}) | ||
context('when there is connection error', () => { | ||
beforeEach(() => { | ||
box.stub(process.env, 'MAX_RETRY_ATTEMPTS').value('2'); | ||
box.stub(process.env, 'RETRY_TIMEOUT_MS').value('1'); | ||
}); | ||
box.stub(process.env, 'MAX_RETRY_ATTEMPTS').value('2') | ||
box.stub(process.env, 'RETRY_TIMEOUT_MS').value('1') | ||
}) | ||
it('Errors with a connection error', (done) => { | ||
const | ||
data = { a: 1, b: 2, c: 3 }, | ||
expectedData = JSON.stringify(data); | ||
data = { a: 1, b: 2, c: 3 } | ||
const expectedData = JSON.stringify(data) | ||
httpRequest.callsFake((options, callback) => { | ||
callback(httpResponse); | ||
httpEmitter = newHttpEmitter(httpResponse); | ||
httpEmitter.end = () => httpEmitter.emit('error', new Error('FakeError')); | ||
return httpEmitter; | ||
}); | ||
callback(httpResponse) | ||
httpEmitter = newHttpEmitter(httpResponse) | ||
httpEmitter.end = () => httpEmitter.emit('error', new Error('FakeError')) | ||
return httpEmitter | ||
}) | ||
const lib = cleanrequire('../lib/promise-data-to'); | ||
expect(lib).to.have.property('MAX_RETRY_ATTEMPTS', 2); | ||
expect(lib).to.have.property('RETRY_TIMEOUT_MS', 1); | ||
const lib = cleanrequire('../lib/promise-data-to') | ||
expect(lib).to.have.property('MAX_RETRY_ATTEMPTS', 2) | ||
expect(lib).to.have.property('RETRY_TIMEOUT_MS', 1) | ||
const { promiseDataTo } = loadLib(); | ||
const { promiseDataTo } = loadLib() | ||
promiseDataTo(config, data) | ||
.catch((stats) => { | ||
expect(stats).to.have.property('err'); | ||
expect(stats).to.not.have.property('resTxt', expectedData); | ||
expect(stats.code).to.equal(599); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(stats).to.have.property('err') | ||
expect(stats).to.not.have.property('resTxt', expectedData) | ||
expect(stats.code).to.equal(599) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
context('when env vars don\'t have value', () => { | ||
beforeEach(() => { | ||
box.stub(process.env, 'MAX_RETRY_ATTEMPTS').value(''); | ||
box.stub(process.env, 'RETRY_TIMEOUT_MS').value(''); | ||
}); | ||
box.stub(process.env, 'MAX_RETRY_ATTEMPTS').value('') | ||
box.stub(process.env, 'RETRY_TIMEOUT_MS').value('') | ||
}) | ||
it('should set the default values', () => { | ||
const lib = cleanrequire('../lib/promise-data-to'); | ||
expect(lib).to.have.property('MAX_RETRY_ATTEMPTS', 3); | ||
expect(lib).to.have.property('RETRY_TIMEOUT_MS', 500); | ||
}); | ||
}); | ||
}); | ||
const lib = cleanrequire('../lib/promise-data-to') | ||
expect(lib).to.have.property('MAX_RETRY_ATTEMPTS', 3) | ||
expect(lib).to.have.property('RETRY_TIMEOUT_MS', 500) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,63 +7,63 @@ /* eslint-env mocha */ | ||
describe('index.js', () => { | ||
const | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
// { clone } = require('@everymundo/simple-clone'), | ||
cleanrequire = require('@everymundo/cleanrequire'), | ||
loadLib = () => cleanrequire('../lib/promise-get'), | ||
noop = () => { }; | ||
const url = require('url') | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
// const { clone } = require('@everymundo/simple-clone') | ||
const cleanrequire = require('@everymundo/cleanrequire') | ||
const logr = require('@everymundo/simple-logr') | ||
const loadLib = () => cleanrequire('../lib/promise-get') | ||
const noop = () => { } | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
const url = require('url'); | ||
const logr = require('@everymundo/simple-logr'); | ||
beforeEach(() => { | ||
box = sandbox.create(); | ||
box = sinon.createSandbox(); | ||
['log', 'info', /* 'debug', */'error'] | ||
.forEach(method => box.stub(logr, method).callsFake(noop)); | ||
}); | ||
.forEach(method => box.stub(logr, method).callsFake(noop)) | ||
}) | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
context('promiseGet', () => { | ||
let promiseDataToLib; | ||
let promiseDataToLib | ||
beforeEach(() => { | ||
promiseDataToLib = cleanrequire('../lib/promise-data-to'); | ||
box.stub(promiseDataToLib, 'promiseDataTo').callsFake(arg => Promise.resolve(arg)); | ||
}); | ||
promiseDataToLib = cleanrequire('../lib/promise-data-to') | ||
box.stub(promiseDataToLib, 'promiseDataTo').callsFake(arg => Promise.resolve(arg)) | ||
}) | ||
it('should call promiseDataTo', () => { | ||
const { promiseGet } = loadLib(); | ||
const { promiseGet } = loadLib() | ||
const config = url.parse('http://test.com/somepath'); | ||
const config = url.parse('http://test.com/somepath') | ||
return promiseGet(config) | ||
.then(() => { | ||
expect(promiseDataToLib.promiseDataTo).to.have.property('calledOnce', true); | ||
}); | ||
}); | ||
expect(promiseDataToLib.promiseDataTo).to.have.property('calledOnce', true) | ||
}) | ||
}) | ||
it('should call promiseDataTo', () => { | ||
const { promiseGet } = loadLib(); | ||
const { promiseGet } = loadLib() | ||
const config = url.parse('http://test.com/somepath'); | ||
const config = url.parse('http://test.com/somepath') | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let protocol, http, host, port, query, path, endpoint, headers, agent, maxRetry; | ||
const expected = { protocol, http, host, port, query, path, endpoint, headers, agent, maxRetry, method: 'GET'}; | ||
let protocol, http, host, port, query, path, endpoint, headers, agent, maxRetry | ||
const expected = { protocol, http, host, port, query, path, endpoint, headers, agent, maxRetry, method: 'GET' } | ||
Object.keys(config).forEach((key) => { | ||
if (key in expected) expected[key] = config[key]; | ||
}); | ||
if (key in expected) expected[key] = config[key] | ||
}) | ||
return promiseGet(config) | ||
.then((arg) => { | ||
expected.method = 'GET'; | ||
expected.method = 'GET' | ||
expect(arg).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(arg).to.deep.equal(expected) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,77 +7,76 @@ /* eslint-env mocha */ | ||
describe('lib/set-headers.js', () => { | ||
const | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'); | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
beforeEach(() => { box = sandbox.create(); }); | ||
afterEach(() => { box.restore(); }); | ||
beforeEach(() => { box = sinon.createSandbox() }) | ||
afterEach(() => { box.restore() }) | ||
describe('#setHeaders', () => { | ||
const { setHeaders } = require('../lib/set-headers'); | ||
const { setHeaders } = require('../lib/set-headers') | ||
context('When content-type is not set', () => { | ||
it('should set content-type to JSON', () => { | ||
const headers = {}; | ||
const headers = {} | ||
setHeaders(headers); | ||
setHeaders(headers) | ||
expect(headers).to.have.property('content-type', 'application/json'); | ||
}); | ||
}); | ||
expect(headers).to.have.property('content-type', 'application/json') | ||
}) | ||
}) | ||
context('When content-type is preset', () => { | ||
it('should not change the content-type sent', () => { | ||
const headers = { 'content-type': 'application/something-else' }; | ||
const headers = { 'content-type': 'application/something-else' } | ||
setHeaders(headers); | ||
setHeaders(headers) | ||
expect(headers).to.have.property('content-type', 'application/something-else'); | ||
}); | ||
}); | ||
expect(headers).to.have.property('content-type', 'application/something-else') | ||
}) | ||
}) | ||
context('When data is not passed', () => { | ||
it('should not set content-length', () => { | ||
const headers = { 'content-type': 'application/something-else' }; | ||
const headers = { 'content-type': 'application/something-else' } | ||
setHeaders(headers); | ||
setHeaders(headers) | ||
expect(headers).to.not.have.property('content-length'); | ||
}); | ||
}); | ||
expect(headers).to.not.have.property('content-length') | ||
}) | ||
}) | ||
context('When data is passed', () => { | ||
it('should set content-length with its byte length', () => { | ||
const headers = {}; | ||
const data = '{"name":"Daniel San","code":"ç"}'; | ||
const headers = {} | ||
const data = '{"name":"Daniel San","code":"ç"}' | ||
setHeaders(headers, data); | ||
setHeaders(headers, data) | ||
expect(headers).to.have.property('content-length', 33); | ||
}); | ||
}); | ||
expect(headers).to.have.property('content-length', 33) | ||
}) | ||
}) | ||
context('When compress is NOT present', () => { | ||
it('should not set content-encoding', () => { | ||
const headers = {}; | ||
const headers = {} | ||
setHeaders(headers); | ||
setHeaders(headers) | ||
expect(headers).to.not.have.property('content-encoding'); | ||
}); | ||
}); | ||
expect(headers).to.not.have.property('content-encoding') | ||
}) | ||
}) | ||
context('When compress is present', () => { | ||
it('should set content-encoding its value', () => { | ||
const headers = {}; | ||
const compress = 'gzip'; | ||
const headers = {} | ||
const compress = 'gzip' | ||
setHeaders(headers, null, compress); | ||
setHeaders(headers, null, compress) | ||
expect(headers).to.have.property('content-encoding', compress); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(headers).to.have.property('content-encoding', compress) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,27 +7,31 @@ /* eslint-env mocha */ | ||
describe('lib/simulatedResponse.js', () => { | ||
const | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
logr = require('@everymundo/simple-logr'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
noop = () => { }, | ||
loadLib = () => require('../lib/simulate-response'); | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
const logr = require('@everymundo/simple-logr') | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
const noop = () => { } | ||
const loadLib = () => require('../lib/simulate-response') | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
beforeEach(() => { | ||
box = sandbox.create(); | ||
box.stub(logr, 'info').callsFake(noop); | ||
}); | ||
box = sinon.createSandbox() | ||
box.stub(logr, 'info').callsFake(noop) | ||
}) | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
describe('#simulatedResponse', () => { | ||
const { simulatedResponse } = loadLib(); | ||
const { simulatedResponse } = loadLib() | ||
context('when calle with an array', () => { | ||
it('should return the expected object with code 222', () => { | ||
const start = new Date(); | ||
const endpoint = 'http://test.com'; | ||
const data = [{name: 'Daniel'}, {name: 'Ragnar'}]; | ||
const start = new Date() | ||
const endpoint = 'http://test.com' | ||
const data = [{ name: 'Daniel' }, { name: 'Ragnar' }] | ||
@@ -41,11 +45,11 @@ const expected = { | ||
dataLen: 2, | ||
end: Date.now(), | ||
}; | ||
end: Date.now() | ||
} | ||
const result = simulatedResponse(start, endpoint, data); | ||
const result = simulatedResponse(start, endpoint, data) | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
'require strict'; | ||
'require strict' | ||
@@ -7,25 +7,25 @@ /* eslint-env mocha */ | ||
describe('index.js', () => { | ||
const | ||
{ sandbox } = require('sinon'), | ||
{ expect } = require('chai'), | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
loadLib = () => require('../lib/url-to-endpoint'), | ||
noop = () => { }; | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
const logr = require('@everymundo/simple-logr') | ||
// { clone } = require('@everymundo/simple-clone'), | ||
// cleanrequire = require('@everymundo/cleanrequire'), | ||
const loadLib = () => require('../lib/url-to-endpoint') | ||
const noop = () => { } | ||
// eslint-disable-next-line one-var-declaration-per-line | ||
let box; | ||
let box | ||
const logr = require('@everymundo/simple-logr'); | ||
beforeEach(() => { | ||
box = sandbox.create(); | ||
box = sinon.createSandbox(); | ||
['log', 'info', /* 'debug', */'error'] | ||
.forEach(method => box.stub(logr, method).callsFake(noop)); | ||
}); | ||
.forEach(method => box.stub(logr, method).callsFake(noop)) | ||
}) | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
context('urlToEndpoint', () => { | ||
const { urlToEndpoint } = loadLib(); | ||
const { urlToEndpoint } = loadLib() | ||
@@ -35,23 +35,23 @@ context('when receiving and INVALID url =>', () => { | ||
it('show throw an error', () => { | ||
const caller = () => urlToEndpoint(); | ||
const caller = () => urlToEndpoint() | ||
expect(caller).to.throw(Error); | ||
}); | ||
}); | ||
expect(caller).to.throw(Error) | ||
}) | ||
}) | ||
context('invalid protocol', () => { | ||
it('show throw an error', () => { | ||
const caller = () => urlToEndpoint('tcp://test.com'); | ||
const caller = () => urlToEndpoint('tcp://test.com') | ||
expect(caller).to.throw(Error); | ||
}); | ||
}); | ||
}); | ||
expect(caller).to.throw(Error) | ||
}) | ||
}) | ||
}) | ||
context('when receiving valid url =>', () => { | ||
const http = require('http'); | ||
const http = require('http') | ||
context('valid simple http url and no headers', () => { | ||
it('should return an object with the expected properties', () => { | ||
const result = urlToEndpoint('http://test.com/some/path'); | ||
const result = urlToEndpoint('http://test.com/some/path') | ||
@@ -65,12 +65,12 @@ const expected = { | ||
port: null, | ||
search: null, | ||
}; | ||
search: null | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
context('http url with credentials and no headers', () => { | ||
it('should return an object with the expected properties', () => { | ||
const result = urlToEndpoint('http://Bearer:token@test.com/some/path'); | ||
const result = urlToEndpoint('http://Bearer:token@test.com/some/path') | ||
@@ -81,3 +81,3 @@ const expected = { | ||
headers: { | ||
Authorization: 'Bearer token', | ||
Authorization: 'Bearer token' | ||
}, | ||
@@ -87,14 +87,14 @@ host: 'test.com', | ||
port: null, | ||
search: null, | ||
}; | ||
search: null | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
context('http url with credentials AND with headers', () => { | ||
it('should return an object with the expected properties', () => { | ||
const headers = {'content-type': 'application/xml'}; | ||
const endpointUrl = 'http://Bearer:token@test.com/some/path'; | ||
const result = urlToEndpoint(endpointUrl, headers); | ||
const headers = { 'content-type': 'application/xml' } | ||
const endpointUrl = 'http://Bearer:token@test.com/some/path' | ||
const result = urlToEndpoint(endpointUrl, headers) | ||
@@ -106,3 +106,3 @@ const expected = { | ||
Authorization: 'Bearer token', | ||
'content-type': 'application/xml', | ||
'content-type': 'application/xml' | ||
}, | ||
@@ -112,14 +112,14 @@ host: 'test.com', | ||
port: null, | ||
search: null, | ||
}; | ||
search: null | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
context('http simple url with headers', () => { | ||
it('should return an object with the expected properties', () => { | ||
const headers = {'content-type': 'application/xml'}; | ||
const endpointUrl = 'http://test.com/some/path'; | ||
const result = urlToEndpoint(endpointUrl, headers); | ||
const headers = { 'content-type': 'application/xml' } | ||
const endpointUrl = 'http://test.com/some/path' | ||
const result = urlToEndpoint(endpointUrl, headers) | ||
@@ -130,3 +130,3 @@ const expected = { | ||
headers: { | ||
'content-type': 'application/xml', | ||
'content-type': 'application/xml' | ||
}, | ||
@@ -136,14 +136,14 @@ host: 'test.com', | ||
port: null, | ||
search: null, | ||
}; | ||
search: null | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
context('http simple url but INVALID headers', () => { | ||
it('should return an object with the expected properties', () => { | ||
const headers = ''; | ||
const endpointUrl = 'http://test.com/some/path'; | ||
const result = urlToEndpoint(endpointUrl, headers); | ||
const headers = '' | ||
const endpointUrl = 'http://test.com/some/path' | ||
const result = urlToEndpoint(endpointUrl, headers) | ||
@@ -157,10 +157,10 @@ const expected = { | ||
port: null, | ||
search: null, | ||
}; | ||
search: null | ||
} | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
8
50173
8
22
1307
10