Skip to content

Commit

Permalink
feat(3013): Add handling for the Expect header at onPreAuth (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxtrot0304 authored Jul 18, 2024
1 parent 18fe16d commit 5390eaa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
22 changes: 22 additions & 0 deletions plugins/caches.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ exports.plugin = {
id: SCHEMA_SCOPE_ID,
cacheName: SCHEMA_CACHE_NAME
})
},
ext: {
onPreAuth: {
method: (request, h) => {
const contentLength = request.headers['content-length'];
const expectHeader = request.headers.expect;

if (expectHeader && expectHeader.toLowerCase() === '100-continue') {
if (contentLength && parseInt(contentLength, 10) > options.maxByteSize) {
request.log(
'Payload content length greater than maximum allowed',
options.maxByteSize
);
throw boom.entityTooLarge();
}

request.raw.res.writeContinue();
}

return h.continue;
}
}
}
}
},
Expand Down
20 changes: 19 additions & 1 deletion test/plugins/caches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ describe('caches plugin test using s3', () => {
plugin,
options: {
expiresInSec: '100',
maxByteSize: '5368709120'
maxByteSize: 10 * 1024 * 1024
}
})
.then(() => server.start());
Expand Down Expand Up @@ -931,6 +931,7 @@ describe('caches plugin test using s3', () => {
headers: {
'x-foo': 'bar',
'content-type': 'text/plain',
expect: '100-continue',
ignore: 'true'
},
auth: {
Expand Down Expand Up @@ -978,6 +979,23 @@ describe('caches plugin test using s3', () => {

assert.equal(putResponse.statusCode, 503);
});

it('returns 100 Continue if Expect header is set and payload size is within limit', async () => {
options.url = `/caches/events/${mockEventID}/foo`;

const putResponse = await server.inject(options);

assert.equal(putResponse.statusCode, 202);
});

it('returns 413 if Expect header is set and payload size exceeds limit', async () => {
options.url = `/caches/events/${mockEventID}/foo`;
options.payload = 'A'.repeat(1024 * 1024 * 11);

const putResponse = await server.inject(options);

assert.equal(putResponse.statusCode, 413);
});
});

describe('DELETE /caches/:scope/:id', () => {
Expand Down

0 comments on commit 5390eaa

Please sign in to comment.