Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #100 from adisingh007/hapi#bassmaster#99
Browse files Browse the repository at this point in the history
Fixes bug where #99, where in path parameters of pipelined requests, `$n` doesn't necessarily have to have a reachable path. This is helpful in cases when the response of a request on which the current request is dependent on, doesn't return a JSON object but just empty strings
  • Loading branch information
Christopher DeCairos authored Sep 20, 2018
2 parents 031a769 + 8f6e352 commit 5e3063b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ module.exports.config = function (settings) {
request.payload.requests.every((req, idx) => {

const requestParts = [];
const result = req.path.replace(internals.requestRegex, (match, p1, p2) => {
const result = req.path.replace(internals.requestRegex, (match, p1, p2, p3) => {

if (!p1) {
requestParts.push({ type: 'text', value: p2 });
requestParts.push({ type: 'text', value: p3 });
return '';
}

if (p1 < idx) {
requestParts.push({ type: 'ref', index: p1, value: p2 });
requestParts.push({ type: 'ref', index: p1, value: p3 });
return '';
}

Expand Down Expand Up @@ -153,7 +153,7 @@ internals.buildPath = function (resultsData, pos, parts) {
break;
}

value = Hoek.reach(ref, part.value);
value = part.value ? Hoek.reach(ref, part.value) : ref;

if (value === null || value === undefined) {
error = new Error('Reference not found');
Expand All @@ -174,7 +174,7 @@ internals.buildPath = function (resultsData, pos, parts) {

internals.payloadRegex = /^\$(\d+)(?:\.([^\s\$]*))?/;

internals.requestRegex = /(?:\/)(?:\$(\d+)\.)?([^\/\$]*)/g;
internals.requestRegex = /(?:\/)(?:\$(\d+))?(\.)?([^\/\$]*)/g;

internals.parsePayload = function (obj) {

Expand Down
22 changes: 22 additions & 0 deletions test/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,26 @@ describe('Batch', () => {
expect(res[11].id).to.equal('10');
expect(res[11].name).to.equal('Item');
});

it('substitutes index in url without any resultPath in url path parameters', async () => {

const res = await Internals.makeRequest(server, JSON.stringify({
requests: [
{
method: 'POST',
path: '/returnInputtedInteger',
payload: {
id: 10041995
}
},
{
method: 'GET',
path: '/returnPathParamInteger/$0'
}
]
}));

expect(res[0]).to.equal(10041995);
expect(res[1]).to.equal(10041995);
});
});
6 changes: 6 additions & 0 deletions test/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ const returnInputtedBooleanHandler = function (request, h) {
return request.payload.bool;
};

const returnPathParamHandler = function (request, h) {

return request.params.pathParamInteger;
};

module.exports.setupServer = async function () {

const server = new Hapi.Server();
Expand Down Expand Up @@ -222,6 +227,7 @@ module.exports.setupServer = async function () {
},
{ method: 'GET', path: '/redirect', handler: redirectHandler },
{ method: 'POST', path: '/returnInputtedInteger', handler: returnInputtedIntegerHandler },
{ method: 'GET', path: '/returnPathParamInteger/{pathParamInteger}', handler: returnPathParamHandler },
{ method: 'GET', path: '/getFalse', handler: getFalseHandler },
{ method: 'POST', path: '/returnInputtedBoolean', handler: returnInputtedBooleanHandler }
]);
Expand Down

0 comments on commit 5e3063b

Please sign in to comment.