bassmaster
Advanced tools
Comparing version 2.0.3 to 3.0.0
103
lib/batch.js
@@ -11,3 +11,7 @@ 'use strict'; | ||
const Joi = require('joi'); | ||
const { promisify } = require('util'); | ||
const asyncSeries = promisify(Async.series); | ||
const asyncParallel = promisify(Async.parallel); | ||
// Declare internals | ||
@@ -20,3 +24,3 @@ | ||
return { | ||
handler: function (request, reply) { | ||
handler: async function (request, h) { | ||
@@ -47,3 +51,3 @@ const requests = []; | ||
errorMessage = 'Request reference is beyond array size: ' + idx; | ||
errorMessage = `Request reference is beyond array size: ${idx}`; | ||
return match; | ||
@@ -58,3 +62,3 @@ }); | ||
else { | ||
errorMessage = errorMessage || 'Invalid request format in item: ' + idx; | ||
errorMessage = errorMessage || `Invalid request format in item: ${idx}`; | ||
return false; | ||
@@ -70,6 +74,14 @@ } | ||
if (errorMessage !== null) { | ||
return reply(Boom.badRequest(errorMessage)); | ||
throw Boom.badRequest(errorMessage); | ||
} | ||
internals.process(request, requests, payloads, resultsData, reply); | ||
try { | ||
await internals.process(request, requests, payloads, resultsData); | ||
} | ||
catch (err) { | ||
// console.log("ERROR ", err); | ||
throw Boom.badRequest(err); | ||
} | ||
return h.response(resultsData.results).code(200); | ||
}, | ||
@@ -93,3 +105,3 @@ notes: settings.notes, | ||
internals.process = function (request, requests, payloads, resultsData, reply) { | ||
internals.process = async function (request, requests, payloads, resultsData) { | ||
@@ -103,35 +115,19 @@ const fnsParallel = []; | ||
if (internals.hasRefPart(requestParts) || payloadParts.length) { | ||
return fnsSerial.push((callback) => { | ||
internals.batch(request, resultsData, idx, requestParts, payloadParts, callback); | ||
}); | ||
return fnsSerial.push( | ||
async () => await internals.batch(request, resultsData, idx, requestParts, payloadParts) | ||
); | ||
} | ||
fnsParallel.push((callback) => { | ||
internals.batch(request, resultsData, idx, requestParts, undefined, callback); | ||
}); | ||
fnsParallel.push( | ||
async () => await internals.batch(request, resultsData, idx, requestParts) | ||
); | ||
}); | ||
Async.series([ | ||
(callback) => { | ||
Async.parallel(fnsParallel, callback); | ||
}, | ||
(callback) => { | ||
Async.series(fnsSerial, callback); | ||
} | ||
], (err) => { | ||
if (err) { | ||
reply(err); | ||
} | ||
else { | ||
reply(resultsData.results); | ||
} | ||
}); | ||
return await asyncSeries([ | ||
async () => await asyncParallel(fnsParallel), | ||
async () => await asyncSeries(fnsSerial) | ||
]); | ||
}; | ||
internals.hasRefPart = function (parts) { | ||
@@ -252,3 +248,3 @@ | ||
internals.batch = function (batchRequest, resultsData, pos, requestParts, payloadParts, callback) { | ||
internals.batch = async function (batchRequest, resultsData, pos, requestParts, payloadParts) { | ||
@@ -259,3 +255,3 @@ const path = internals.buildPath(resultsData, pos, requestParts); | ||
resultsData.results[pos] = path; | ||
return callback(path); | ||
throw path; | ||
} | ||
@@ -278,23 +274,14 @@ | ||
internals.dispatch(batchRequest, request, (data) => { | ||
let data = await internals.dispatch(batchRequest, request); | ||
// If redirection | ||
if (('' + data.statusCode).indexOf('3') === 0) { | ||
request.path = data.headers.location; | ||
internals.dispatch(batchRequest, request, (batchData) => { | ||
// If redirection | ||
if (data.statusCode >= 300 && data.statusCode < 400) { | ||
request.path = data.headers.location; | ||
data = await internals.dispatch(batchRequest, request); | ||
} | ||
const batchResult = internals.parseResult(batchData.result); | ||
resultsData.results[pos] = batchResult; | ||
resultsData.resultsMap[pos] = batchResult; | ||
callback(null, batchResult); | ||
}); | ||
return; | ||
} | ||
const result = internals.parseResult(data.result); | ||
resultsData.results[pos] = result; | ||
resultsData.resultsMap[pos] = result; | ||
callback(null, result); | ||
}); | ||
const result = internals.parseResult(data.result); | ||
resultsData.results[pos] = result; | ||
resultsData.resultsMap[pos] = result; | ||
return result; | ||
}; | ||
@@ -317,3 +304,3 @@ | ||
internals.dispatch = function (batchRequest, request, callback) { | ||
internals.dispatch = async function (batchRequest, request) { | ||
@@ -339,8 +326,4 @@ let path; | ||
}; | ||
if (batchRequest.server.connections.length === 1) { | ||
batchRequest.server.inject(injectOptions, callback); | ||
} | ||
else { | ||
batchRequest.connection.inject(injectOptions, callback); | ||
} | ||
return await batchRequest.server.inject(injectOptions); | ||
}; |
@@ -7,4 +7,5 @@ 'use strict'; | ||
const Batch = require('./batch'); | ||
const Pkg = require('../package.json'); | ||
const { name } = Pkg; | ||
// Declare internals | ||
@@ -21,5 +22,4 @@ | ||
const register = function (server, options) { | ||
exports.register = function (server, options, next) { | ||
const settings = Hoek.applyToDefaults(internals.defaults, options); | ||
@@ -32,8 +32,4 @@ | ||
}); | ||
return next(); | ||
}; | ||
exports.register.attributes = { | ||
pkg: require('../package.json') | ||
}; | ||
exports.plugin = { register, name, Pkg }; |
@@ -5,3 +5,3 @@ { | ||
"repository": "git://github.com/hapijs/bassmaster", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"main": "lib/index.js", | ||
@@ -17,13 +17,12 @@ "keywords": [ | ||
"dependencies": { | ||
"async": "1.x.x", | ||
"boom": "3.x.x", | ||
"hoek": "4.x.x", | ||
"joi": "8.x.x", | ||
"async": "2.x.x", | ||
"boom": "7.x.x", | ||
"hoek": "5.x.x", | ||
"joi": "13.x.x", | ||
"traverse": "0.6.x" | ||
}, | ||
"devDependencies": { | ||
"code": "2.x.x", | ||
"hapi": "13.x.x", | ||
"lab": "10.x.x", | ||
"sinon": "1.x.x" | ||
"code": "5.x.x", | ||
"hapi": "17.x.x", | ||
"lab": "15.x.x" | ||
}, | ||
@@ -30,0 +29,0 @@ "scripts": { |
@@ -5,6 +5,4 @@ 'use strict'; | ||
const Async = require('async'); | ||
const Code = require('code'); | ||
const Lab = require('lab'); | ||
const Sinon = require('sinon'); | ||
const Internals = require('./internals.js'); | ||
@@ -15,6 +13,4 @@ | ||
const lab = exports.lab = Lab.script(); | ||
const describe = lab.describe; | ||
const it = lab.it; | ||
const before = lab.before; | ||
const expect = Code.expect; | ||
const { describe, it, before } = lab; | ||
const { expect } = Code; | ||
@@ -25,76 +21,63 @@ let server = null; | ||
before((done) => { | ||
before( async () => { | ||
server = Internals.setupServer(done); | ||
server = await Internals.setupServer(); | ||
}); | ||
it('shows single response when making request for single endpoint', (done) => { | ||
it('shows single response when making request for single endpoint', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile" }] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile" }] }'); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
}); | ||
it('supports redirect', (done) => { | ||
it('supports redirect', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/redirect" }] }', (res) => { | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
done(); | ||
}); | ||
const res = await Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/redirect" }] }'); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
}); | ||
it('supports query string in the request', (done) => { | ||
it('supports query string in the request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile?id=someid" }] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile?id=someid" }] }'); | ||
expect(res[0].id).to.equal('someid'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('someid'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
}); | ||
it('supports non alphanum characters in the request', (done) => { | ||
it('supports non alphanum characters in the request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/item/item-_^~&-end" }] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/item/item-_^~&-end" }] }'); | ||
expect(res[0].id).to.equal('item-_^~&-end'); | ||
expect(res[0].name).to.equal('Item'); | ||
expect(res.length).to.equal(1); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('item-_^~&-end'); | ||
expect(res[0].name).to.equal('Item'); | ||
expect(res.length).to.equal(1); | ||
}); | ||
it('shows two ordered responses when requesting two endpoints', (done) => { | ||
it('shows two ordered responses when requesting two endpoints', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/profile"}, {"method": "get", "path": "/item"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/profile"}, {"method": "get", "path": "/item"}] }'); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(2); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Active Item'); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(2); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Active Item'); | ||
}); | ||
it('shows two ordered responses when requesting two endpoints (with optional path param)', (done) => { | ||
it('shows two ordered responses when requesting two endpoints (with optional path param)', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item2/john"}, {"method": "get", "path": "/item2/"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item2/john"}, {"method": "get", "path": "/item2/"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('john'); | ||
expect(res[1].id).to.equal('mystery-guest'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('john'); | ||
expect(res[1].id).to.equal('mystery-guest'); | ||
}); | ||
it('handles a large number of batch requests in parallel', (done) => { | ||
it('handles a large number of batch requests in parallel', async () => { | ||
@@ -183,432 +166,340 @@ const requestBody = '{ "requests": [{"method": "get", "path": "/profile"},' + | ||
const asyncSpy = Sinon.spy(Async, 'parallel'); | ||
Internals.makeRequest(server, requestBody, (res) => { | ||
const res = await Internals.makeRequest(server, requestBody); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(80); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Active Item'); | ||
expect(asyncSpy.args[0][0].length).to.equal(80); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('fa0dbda9b1b'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(80); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Active Item'); | ||
}); | ||
it('supports piping a response into the next request', (done) => { | ||
it('supports piping a response into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.id"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
}); | ||
it('supports piping Id\'s with "-" (like a uuid) into the next request', (done) => { | ||
it('supports piping Id\'s with "-" (like a uuid) into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/interestingIds"}, {"method": "get", "path": "/item/$0.idWithDash"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/interestingIds"}, {"method": "get", "path": "/item/$0.idWithDash"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].idWithDash).to.equal('55cf-687663-55cf687663'); | ||
expect(res[1].id).to.equal('55cf-687663-55cf687663'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].idWithDash).to.equal('55cf-687663-55cf687663'); | ||
expect(res[1].id).to.equal('55cf-687663-55cf687663'); | ||
}); | ||
it('supports piping interesting Ids with "." (like a filename) into the next request', (done) => { | ||
it('supports piping interesting Ids with "." (like a filename) into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/interestingIds"}, {"method": "get", "path": "/item/$0.idLikeFilename"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/interestingIds"}, {"method": "get", "path": "/item/$0.idLikeFilename"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].idLikeFilename).to.equal('55cf687663.png'); | ||
expect(res[1].id).to.equal('55cf687663.png'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].idLikeFilename).to.equal('55cf687663.png'); | ||
expect(res[1].id).to.equal('55cf687663.png'); | ||
}); | ||
it('supports piping interesting Ids with "-" and "." (like a filename) into the next request', (done) => { | ||
it('supports piping interesting Ids with "-" and "." (like a filename) into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/interestingIds"}, {"method": "get", "path": "/item/$0.idLikeFileNameWithDash"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/interestingIds"}, {"method": "get", "path": "/item/$0.idLikeFileNameWithDash"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].idLikeFileNameWithDash).to.equal('55cf-687663-55cf687663.png'); | ||
expect(res[1].id).to.equal('55cf-687663-55cf687663.png'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].idLikeFileNameWithDash).to.equal('55cf-687663-55cf687663.png'); | ||
expect(res[1].id).to.equal('55cf-687663-55cf687663.png'); | ||
}); | ||
it('supports piping a deep response into the next request', (done) => { | ||
it('supports piping a deep response into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/deepItem"}, {"method": "post", "path": "/echo", "payload": "$0.inner.name"}, {"method": "post", "path": "/echo", "payload": "$0.inner.inner.name"}, {"method": "post", "path": "/echo", "payload": "$0.inner.inner.inner.name"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/deepItem"}, {"method": "post", "path": "/echo", "payload": "$0.inner.name"}, {"method": "post", "path": "/echo", "payload": "$0.inner.inner.name"}, {"method": "post", "path": "/echo", "payload": "$0.inner.inner.inner.name"}] }'); | ||
expect(res.length).to.equal(4); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Deep Item'); | ||
expect(res[1]).to.equal('Level 1'); | ||
expect(res[2]).to.equal('Level 2'); | ||
expect(res[3]).to.equal('Level 3'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(4); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Deep Item'); | ||
expect(res[1]).to.equal('Level 1'); | ||
expect(res[2]).to.equal('Level 2'); | ||
expect(res[3]).to.equal('Level 3'); | ||
}); | ||
it('supports piping a deep response into an array in the next request', (done) => { | ||
it('supports piping a deep response into an array in the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/deepItem"}, {"method": "post", "path": "/echo", "payload": "$0.inner.inner.inner.array.0.name"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/deepItem"}, {"method": "post", "path": "/echo", "payload": "$0.inner.inner.inner.array.0.name"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Deep Item'); | ||
expect(res[1]).to.equal('Array Item 0'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Deep Item'); | ||
expect(res[1]).to.equal('Array Item 0'); | ||
}); | ||
it('supports piping integer response into the next request', (done) => { | ||
it('supports piping integer response into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/int"}, {"method": "get", "path": "/int/$0.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/int"}, {"method": "get", "path": "/int/$0.id"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal(123); | ||
expect(res[0].name).to.equal('Integer Item'); | ||
expect(res[1].id).to.equal('123'); | ||
expect(res[1].name).to.equal('Integer'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal(123); | ||
expect(res[0].name).to.equal('Integer Item'); | ||
expect(res[1].id).to.equal('123'); | ||
expect(res[1].name).to.equal('Integer'); | ||
}); | ||
it('supports the return of strings instead of json', (done) => { | ||
it('supports the return of strings instead of json', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/string"}, {"method": "get", "path": "/item/$0.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/string"}, {"method": "get", "path": "/item/$0.id"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('String Item'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('String Item'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
}); | ||
it('supports piping a zero integer response into the next request', (done) => { | ||
it('supports piping a zero integer response into the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/zero"}, {"method": "get", "path": "/int/$0.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/zero"}, {"method": "get", "path": "/int/$0.id"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal(0); | ||
expect(res[0].name).to.equal('Zero Item'); | ||
expect(res[1].id).to.equal('0'); | ||
expect(res[1].name).to.equal('Integer'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal(0); | ||
expect(res[0].name).to.equal('Zero Item'); | ||
expect(res[1].id).to.equal('0'); | ||
expect(res[1].name).to.equal('Integer'); | ||
}); | ||
it('supports posting multiple requests', (done) => { | ||
it('supports posting multiple requests', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":{"a":2}}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":{"a":2}}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0]).to.deep.equal({ a: 1 }); | ||
expect(res[1]).to.deep.equal({ a: 2 }); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0]).to.equal({ a: 1 }); | ||
expect(res[1]).to.equal({ a: 2 }); | ||
}); | ||
it('supports sending multiple PUTs requests', (done) => { | ||
it('supports sending multiple PUTs requests', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "put", "path": "/echo", "payload":{"a":1}}, {"method": "put", "path": "/echo", "payload":{"a":2}}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "put", "path": "/echo", "payload":{"a":1}}, {"method": "put", "path": "/echo", "payload":{"a":2}}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0]).to.deep.equal({ a: 1 }); | ||
expect(res[1]).to.deep.equal({ a: 2 }); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0]).to.equal({ a: 1 }); | ||
expect(res[1]).to.equal({ a: 2 }); | ||
}); | ||
it('supports piping a response from post into the next get request', (done) => { | ||
it('supports piping a response from post into the next get request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload": {"id":"55cf687663"}}, {"method": "get", "path": "/item/$0.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload": {"id":"55cf687663"}}, {"method": "get", "path": "/item/$0.id"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
}); | ||
it('supports piping a nested response value from post into the next get request', (done) => { | ||
it('supports piping a nested response value from post into the next get request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.id"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].data.id).to.equal('44cf687663'); | ||
expect(res[1].id).to.equal('44cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].data.id).to.equal('44cf687663'); | ||
expect(res[1].id).to.equal('44cf687663'); | ||
expect(res[1].name).to.equal('Item'); | ||
}); | ||
it('handles null payloads gracefully', (done) => { | ||
it('handles null payloads gracefully', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":null}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":null}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0]).to.deep.equal({ a: 1 }); | ||
expect(res[1]).to.deep.equal(null); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0]).to.equal({ a: 1 }); | ||
expect(res[1]).to.equal(null); | ||
}); | ||
it('includes errors when they occur in the request', (done) => { | ||
it('includes errors when they occur in the request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/nothere"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/nothere"}] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].error).to.exist(); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].error).to.exist(); | ||
}); | ||
it('bad requests return the correct error', (done) => { | ||
it('bad requests return the correct error', async () => { | ||
Internals.makeRequest(server, '{ "blah": "test" }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "blah": "test" }'); | ||
expect(res.statusCode).to.equal(400); | ||
done(); | ||
}); | ||
expect(res.statusCode).to.equal(400); | ||
}); | ||
it('handles empty payload', (done) => { | ||
it('handles empty payload', async () => { | ||
Internals.makeRequest(server, null, (res) => { | ||
const res = await Internals.makeRequest(server, null); | ||
expect(res.statusCode).to.equal(400); | ||
done(); | ||
}); | ||
expect(res.statusCode).to.equal(400); | ||
}); | ||
it('handles payload request not array', (done) => { | ||
it('handles payload request not array', async () => { | ||
Internals.makeRequest(server, '{ "requests": {"method": "get", "path": "/$1"} }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": {"method": "get", "path": "/$1"} }'); | ||
expect(res.statusCode).to.equal(400); | ||
done(); | ||
}); | ||
expect(res.statusCode).to.equal(400); | ||
}); | ||
it('handles bad paths in requests array', (done) => { | ||
it('handles bad paths in requests array', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/$1"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/$1"}] }'); | ||
expect(res.statusCode).to.equal(400); | ||
done(); | ||
}); | ||
expect(res.statusCode).to.equal(400); | ||
}); | ||
it('handles errors in the requested handlers', (done) => { | ||
it('handles errors in the requested handlers', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/error"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/error"}] }'); | ||
expect(res[0].statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res[0].statusCode).to.equal(500); | ||
}); | ||
it('an out of bounds reference returns an error', (done) => { | ||
it('an out of bounds reference returns an error', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$1.id"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$1.id"}] }'); | ||
expect(res.error).to.equal('Bad Request'); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('a non-existant reference returns an internal error', (done) => { | ||
it('a non-existant reference returns an error', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.nothere"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.nothere"}] }'); | ||
expect(res.statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('a non-existant & nested reference returns an internal error', (done) => { | ||
it('a non-existant & nested reference returns an error', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.not.here"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.not.here"}] }'); | ||
expect(res.statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('handles a bad character in the reference value', (done) => { | ||
it('handles a bad character in the reference value', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.invalidChar"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.invalidChar"}] }'); | ||
expect(res.statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('handles a null value in the reference value', (done) => { | ||
it('handles a null value in the reference value', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.null"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.null"}] }'); | ||
expect(res.statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('cannot use invalid character to request reference', (done) => { | ||
it('cannot use invalid character to request reference', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$:.name"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$:.name"}] }'); | ||
expect(res.statusCode).to.equal(400); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('handles missing reference', (done) => { | ||
it('handles missing reference', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$0.name"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$0.name"}] }'); | ||
expect(res.statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('handles error when getting reference value', (done) => { | ||
it('handles error when getting reference value', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.1"}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.1"}] }'); | ||
expect(res.statusCode).to.equal(500); | ||
done(); | ||
}); | ||
expect(res.error).to.equal('Bad Request'); | ||
}); | ||
it('supports an optional query object', (done) => { | ||
it('supports an optional query object', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "someid" } }] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "someid" } }] }'); | ||
expect(res[0].id).to.equal('someid'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('someid'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
}); | ||
it('supports alphanum characters in the query', (done) => { | ||
it('supports alphanum characters in the query', async () => { | ||
Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "item-_^~&-end" } }] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "item-_^~&-end" } }] }'); | ||
expect(res[0].id).to.equal('item-_^~&-end'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
done(); | ||
}); | ||
expect(res[0].id).to.equal('item-_^~&-end'); | ||
expect(res[0].name).to.equal('John Doe'); | ||
expect(res.length).to.equal(1); | ||
}); | ||
it('handles null queries gracefully', (done) => { | ||
it('handles null queries gracefully', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }'); | ||
expect(res.length).to.equal(1); | ||
expect(res[0]).to.deep.equal(null); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(1); | ||
expect(res[0]).to.equal(null); | ||
}); | ||
it('supports piping a whole payload to the next request', (done) => { | ||
it('supports piping a whole payload to the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0"} ] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0"} ] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Active Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].id).to.equal('55cf687663'); | ||
expect(res[1].name).to.equal('Active Item'); | ||
}); | ||
it('supports piping a partial payload to the next request', (done) => { | ||
it('supports piping a partial payload to the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.name"} ] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.name"} ] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1]).to.equal('Active Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1]).to.equal('Active Item'); | ||
}); | ||
it('supports piping a partial payload from a nested array to the next request', (done) => { | ||
it('supports piping a partial payload from a nested array to the next request', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/array"}, {"method": "post", "path": "/echo", "payload":"$0.items.1"} ] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/array"}, {"method": "post", "path": "/echo", "payload":"$0.items.1"} ] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Dress'); | ||
expect(res[1].color).to.equal('whiteandgold'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Dress'); | ||
expect(res[1].color).to.equal('whiteandgold'); | ||
}); | ||
it('returns an empty object when a non-existent path is set at the root of the payload', (done) => { | ||
it('returns an empty object when a non-existent path is set at the root of the payload', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.foo"} ] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.foo"} ] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1]).to.be.empty(); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1]).to.be.empty(); | ||
}); | ||
it('sets a nested reference in the payload', (done) => { | ||
it('sets a nested reference in the payload', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"name2": "$0.name"}} ] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"name2": "$0.name"}} ] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].name2).to.equal('Active Item'); | ||
done(); | ||
}); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].name2).to.equal('Active Item'); | ||
}); | ||
it('returns an empty object when a nonexistent path is set in the payload', (done) => { | ||
it('returns an empty object when a nonexistent path is set in the payload', async () => { | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"foo": "$0.foo"}} ] }', (res) => { | ||
const res = await Internals.makeRequest(server, '{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"foo": "$0.foo"}} ] }'); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].foo).to.be.empty(); | ||
expect(res.length).to.equal(2); | ||
expect(res[0].id).to.equal('55cf687663'); | ||
expect(res[0].name).to.equal('Active Item'); | ||
expect(res[1].foo).to.be.empty(); | ||
done(); | ||
}); | ||
}); | ||
it('works with multiple connections', (done) => { | ||
// Add a connection to the server | ||
server.connection({ port: 8000, host: 'localhost', labels: ['test'] }); | ||
Internals.makeRequest(server, '{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', (res) => { | ||
expect(res.length).to.equal(1); | ||
expect(res[0]).to.deep.equal(null); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -6,8 +6,8 @@ 'use strict'; | ||
const profileHandler = function (request, reply) { | ||
const profileHandler = function (request, h) { | ||
const id = request.query.id || 'fa0dbda9b1b'; | ||
return reply({ | ||
'id': id, | ||
return h.response({ | ||
id, | ||
'name': 'John Doe' | ||
@@ -17,5 +17,5 @@ }); | ||
const activeItemHandler = function (request, reply) { | ||
const activeItemHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': '55cf687663', | ||
@@ -26,5 +26,5 @@ 'name': 'Active Item' | ||
const itemHandler = function (request, reply) { | ||
const itemHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': request.params.id, | ||
@@ -35,5 +35,5 @@ 'name': 'Item' | ||
const deepItemHandler = function (request, reply) { | ||
const deepItemHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': '55cf687663', | ||
@@ -61,5 +61,5 @@ 'name': 'Deep Item', | ||
const item2Handler = function (request, reply) { | ||
const item2Handler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': request.params.id || 'mystery-guest', | ||
@@ -70,5 +70,5 @@ 'name': 'Item' | ||
const arrayHandler = function (request, reply) { | ||
const arrayHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': '55cf687663', | ||
@@ -80,5 +80,5 @@ 'name': 'Dress', | ||
const zeroIntegerHandler = function (request, reply) { | ||
const zeroIntegerHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': 0, | ||
@@ -89,5 +89,5 @@ 'name': 'Zero Item' | ||
const integerHandler = function (request, reply) { | ||
const integerHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': 123, | ||
@@ -98,5 +98,5 @@ 'name': 'Integer Item' | ||
const integerItemHandler = function (request, reply) { | ||
const integerItemHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': request.params.id, | ||
@@ -107,5 +107,5 @@ 'name': 'Integer' | ||
const stringItemHandler = function (request, reply) { | ||
const stringItemHandler = function (request, h) { | ||
return reply('{' + | ||
return h.response('{' + | ||
'"id": "55cf687663",' + | ||
@@ -116,5 +116,5 @@ '"name": "String Item"' + | ||
const badCharHandler = function (request, reply) { | ||
const badCharHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'id': 'test', | ||
@@ -126,15 +126,15 @@ 'null': null, | ||
const badValueHandler = function (request, reply) { | ||
const badValueHandler = function (request, h) { | ||
return reply(null); | ||
return h.response(null); | ||
}; | ||
const redirectHandler = function (request, reply) { | ||
const redirectHandler = function (request, h) { | ||
return reply().redirect('/profile'); | ||
return h.redirect('/profile'); | ||
}; | ||
const interestingIdsHandler = function (request, reply) { | ||
const interestingIdsHandler = function (request, h) { | ||
return reply({ | ||
return h.response({ | ||
'idWithDash': '55cf-687663-55cf687663', | ||
@@ -146,49 +146,45 @@ 'idLikeFilename': '55cf687663.png', | ||
const fetch1 = function (request, next) { | ||
const fetch1 = function (request, h) { | ||
next('Hello'); | ||
return 'Hello'; | ||
}; | ||
const fetch2 = function (request, next) { | ||
const fetch2 = function (request, h) { | ||
next(request.pre.m1 + request.pre.m3 + request.pre.m4); | ||
return request.pre.m1 + request.pre.m3 + request.pre.m4; | ||
}; | ||
const fetch3 = function (request, next) { | ||
const fetch3 = function (request, h) { | ||
process.nextTick(() => { | ||
next(' '); | ||
}); | ||
return ' '; | ||
}; | ||
const fetch4 = function (request, next) { | ||
const fetch4 = function (request, h) { | ||
next('World'); | ||
return 'World'; | ||
}; | ||
const fetch5 = function (request, next) { | ||
const fetch5 = function (request, h) { | ||
next(request.pre.m2 + '!'); | ||
return `${request.pre.m2}!`; | ||
}; | ||
const getFetch = function (request, reply) { | ||
const getFetch = function (request, h) { | ||
return reply(request.pre.m5 + '\n'); | ||
return `${request.pre.m5}\n`; | ||
}; | ||
const errorHandler = function (request, reply) { | ||
const errorHandler = function (request, h) { | ||
return reply(new Error('myerror')); | ||
return new Error('myerror'); | ||
}; | ||
const echoHandler = function (request, reply) { | ||
const echoHandler = function (request, h) { | ||
return reply(request.payload); | ||
return request.payload; | ||
}; | ||
module.exports.setupServer = function (done) { | ||
module.exports.setupServer = async function () { | ||
const server = new Hapi.Server(); | ||
server.connection(); | ||
server.route([ | ||
@@ -228,13 +224,14 @@ { method: 'POST', path: '/echo', handler: echoHandler }, | ||
server.register(Bassmaster, done); | ||
await server.register(Bassmaster); | ||
return server; | ||
}; | ||
module.exports.makeRequest = function (server, payload, callback) { | ||
module.exports.makeRequest = async function (server, payload) { | ||
server.connections[0].inject({ | ||
return (await server.inject({ | ||
method: 'post', | ||
url: '/batch', | ||
payload: payload | ||
}, (res) => callback(res.result)); | ||
payload | ||
})).result; | ||
}; |
@@ -21,3 +21,3 @@ 'use strict'; | ||
const it = lab.it; | ||
const expect = Code.expect; | ||
const { expect, fail } = Code; | ||
@@ -27,43 +27,48 @@ | ||
it('can be added as a plugin to hapi', (done) => { | ||
it('can be added as a plugin to hapi', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection(); | ||
server.register({ register: Bassmaster }, (err) => { | ||
expect(err).to.not.exist(); | ||
done(); | ||
}); | ||
try { | ||
await server.register(Bassmaster); | ||
} | ||
catch (e) { | ||
fail('Plugin failed to register'); | ||
} | ||
expect(true).to.be.true(); | ||
}); | ||
it('can be given a custom route url', (done) => { | ||
it('can be given a custom route url', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection(); | ||
server.register({ register: Bassmaster, options: { batchEndpoint: '/custom' } }, (err) => { | ||
expect(err).to.not.exist(); | ||
const path = server.connections[0].table()[0].path; | ||
expect(path).to.equal('/custom'); | ||
done(); | ||
}); | ||
try { | ||
await server.register({ plugin: Bassmaster, options: { batchEndpoint: '/custom' } }); | ||
} | ||
catch (e) { | ||
fail('Plugin failed to register'); | ||
} | ||
const path = server.table()[0].path; | ||
expect(path).to.equal('/custom'); | ||
}); | ||
it('can be given a custom description', (done) => { | ||
it('can be given a custom description', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection(); | ||
server.register({ register: Bassmaster, options: { description: 'customDescription' } }, (err) => { | ||
try { | ||
await server.register({ plugin: Bassmaster, options: { description: 'customDescription' } }); | ||
} | ||
catch (e) { | ||
fail('Plugin failed to register'); | ||
} | ||
expect(err).to.not.exist(); | ||
const description = server.connections[0].table()[0].settings.description; | ||
expect(description).to.equal('customDescription'); | ||
done(); | ||
}); | ||
const description = server.table()[0].settings.description; | ||
expect(description).to.equal('customDescription'); | ||
}); | ||
it('can be given an authentication strategy', (done) => { | ||
it('can be given an authentication strategy', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection(); | ||
const mockScheme = { | ||
@@ -88,23 +93,27 @@ authenticate: () => { | ||
server.auth.strategy('mockStrategy', 'mockScheme'); | ||
server.register({ register: Bassmaster, options: { auth: 'mockStrategy' } }, (err) => { | ||
expect(err).to.not.exist(); | ||
const auth = server.connections[0].table()[0].settings.auth.strategies[0]; | ||
expect(auth).to.equal('mockStrategy'); | ||
done(); | ||
}); | ||
try { | ||
await server.register({ plugin: Bassmaster, options: { auth: 'mockStrategy' } }); | ||
} | ||
catch (e) { | ||
fail('Plugin failed to register'); | ||
} | ||
const auth = server.table()[0].settings.auth.strategies[0]; | ||
expect(auth).to.equal('mockStrategy'); | ||
}); | ||
it('can be given custom tags', (done) => { | ||
it('can be given custom tags', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection(); | ||
server.register({ register: Bassmaster, options: { tags: ['custom', 'tags'] } }, (err) => { | ||
try { | ||
await server.register({ plugin: Bassmaster, options: { tags: ['custom', 'tags'] } }); | ||
} | ||
catch (e) { | ||
fail('Plugin failed to register'); | ||
} | ||
expect(err).to.not.exist(); | ||
const tags = server.connections[0].table()[0].settings.tags; | ||
expect(tags).to.deep.equal(['custom', 'tags']); | ||
done(); | ||
}); | ||
const tags = server.table()[0].settings.tags; | ||
expect(tags).to.equal(['custom', 'tags']); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
3
82512
12
938
+ Addedasync@2.6.4(transitive)
+ Addedboom@7.3.0(transitive)
+ Addedhoek@5.0.46.1.3(transitive)
+ Addedisemail@3.2.0(transitive)
+ Addedjoi@13.7.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedtopo@3.0.3(transitive)
- Removedasync@1.5.2(transitive)
- Removedboom@3.2.2(transitive)
- Removedhoek@4.3.1(transitive)
- Removedisemail@2.2.1(transitive)
- Removedjoi@8.4.2(transitive)
- Removedmoment@2.30.1(transitive)
- Removedtopo@2.1.1(transitive)
Updatedasync@2.x.x
Updatedboom@7.x.x
Updatedhoek@5.x.x
Updatedjoi@13.x.x