Skip to content

Commit

Permalink
Allow global multipart. Closes #4027
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Feb 13, 2020
1 parent 8656725 commit 85d7801
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ internals.routeBase = Joi.object({
output: Joi.valid('data', 'stream', 'file', 'annotated').required()
})
.default(false)
.allow(true, false)
.when('.', { is: true, then: Joi.object().strip() }),
.allow(true, false),
allow: Joi.array().items(Joi.string()).single(),
override: Joi.string(),
protoAction: Joi.valid('error', 'remove', 'ignore').default('error'),
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"web"
],
"dependencies": {
"@hapi/accept": "5.x.x",
"@hapi/ammo": "5.x.x",
"@hapi/accept": "^5.0.1",
"@hapi/ammo": "^5.0.1",
"@hapi/boom": "9.x.x",
"@hapi/bounce": "2.x.x",
"@hapi/call": "8.x.x",
Expand All @@ -29,8 +29,8 @@
"@hapi/podium": "4.x.x",
"@hapi/shot": "5.x.x",
"@hapi/somever": "3.x.x",
"@hapi/statehood": "7.x.x",
"@hapi/subtext": "7.x.x",
"@hapi/statehood": "^7.0.2",
"@hapi/subtext": "^7.0.3",
"@hapi/teamwork": "4.x.x",
"@hapi/topo": "5.x.x"
},
Expand Down
56 changes: 55 additions & 1 deletion test/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ describe('Payload', () => {
expect(res.statusCode).to.equal(415);
});

it('returns parsed multipart data', async () => {
it('returns parsed multipart data (route)', async () => {

const multipartPayload =
'--AaB03x\r\n' +
Expand Down Expand Up @@ -585,6 +585,60 @@ describe('Payload', () => {
expect(res.result.pics).to.exist();
});

it('returns parsed multipart data (server)', async () => {

const multipartPayload =
'--AaB03x\r\n' +
'content-disposition: form-data; name="x"\r\n' +
'\r\n' +
'First\r\n' +
'--AaB03x\r\n' +
'content-disposition: form-data; name="x"\r\n' +
'\r\n' +
'Second\r\n' +
'--AaB03x\r\n' +
'content-disposition: form-data; name="x"\r\n' +
'\r\n' +
'Third\r\n' +
'--AaB03x\r\n' +
'content-disposition: form-data; name="field1"\r\n' +
'\r\n' +
'Joe Blow\r\nalmost tricked you!\r\n' +
'--AaB03x\r\n' +
'content-disposition: form-data; name="field1"\r\n' +
'\r\n' +
'Repeated name segment\r\n' +
'--AaB03x\r\n' +
'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n' +
'Content-Type: text/plain\r\n' +
'\r\n' +
'... contents of file1.txt ...\r\r\n' +
'--AaB03x--\r\n';

const handler = (request) => {

const result = {};
const keys = Object.keys(request.payload);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const value = request.payload[key];
result[key] = value._readableState ? true : value;
}

return result;
};

const server = Hapi.server({ routes: { payload: { multipart: true } } });
server.route({ method: 'POST', path: '/echo', handler });

const res = await server.inject({ method: 'POST', url: '/echo', payload: multipartPayload, headers: { 'content-type': 'multipart/form-data; boundary=AaB03x' } });
expect(Object.keys(res.result).length).to.equal(3);
expect(res.result.field1).to.exist();
expect(res.result.field1.length).to.equal(2);
expect(res.result.field1[1]).to.equal('Repeated name segment');
expect(res.result.pics).to.exist();
});

it('signals connection close when payload is unconsumed', async () => {

const payload = Buffer.alloc(1024);
Expand Down

0 comments on commit 85d7801

Please sign in to comment.