Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow routes to specify the idle socket timeout in addition to the payload timeout #73730

Merged
merged 32 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6ce7f1e
Route options timeout -> timeout.payload
kobelb Jul 29, 2020
4e3b566
timeout.idleSocket can now be specified per route
kobelb Jul 29, 2020
26541ba
Removing nested ternary
kobelb Jul 29, 2020
4048150
Fixing integration tests
kobelb Jul 29, 2020
8267a2c
Trying to actually fix the integration tests. Existing tests are hitting
kobelb Jul 29, 2020
80e6259
Merge remote-tracking branch 'upstream/master' into core/idle-socket-…
kobelb Aug 5, 2020
66cf715
Fixing payload post timeout integration test
kobelb Aug 5, 2020
1a26b10
Fixing PUT and DELETE payload sending too long tests
kobelb Aug 5, 2020
c03a0ce
Fixing type-script errors
kobelb Aug 5, 2020
b5b7026
GET routes can't specify the payload timeout, they can't accept payloads
kobelb Aug 5, 2020
7429fea
Removing some redundancy in the tests
kobelb Aug 5, 2020
4f7e9e1
Adding 'Transfer-Encoding: chunked' to the POST test
kobelb Aug 5, 2020
828eab8
Fixing POST/GET/PUT quick tests
kobelb Aug 5, 2020
c092cbe
Adding idleSocket timeout test
kobelb Aug 5, 2020
2c939ea
Removing unnecessary `isSafeMethod` call
kobelb Aug 5, 2020
d300b9f
Updating documentation
kobelb Aug 5, 2020
84e06fa
Removing PUT/DELETE integration tests
kobelb Aug 6, 2020
3d8901a
Working around the HapiJS bug
kobelb Aug 6, 2020
afbb7e8
Deleting unused type import
kobelb Aug 6, 2020
c4a3e15
The socket can be undefined...
kobelb Aug 6, 2020
87634ff
Merge branch 'master' into core/idle-socket-timeout-2
elasticmachine Aug 10, 2020
20ff93f
Update src/core/server/http/http_server.ts
kobelb Aug 10, 2020
0e8fd23
Merge remote-tracking branch 'upstream/master' into core/idle-socket-…
kobelb Aug 10, 2020
dc430b9
Adding payload timeout functional tests
kobelb Aug 10, 2020
31dd497
Adding idle socket timeout functional tests
kobelb Aug 10, 2020
25b6db0
Adding better comments, using ?? instead of ||
kobelb Aug 10, 2020
d4e7d2a
Fixing the plugin fixture TS
kobelb Aug 10, 2020
2862387
Fixing some typescript errors
kobelb Aug 11, 2020
4c6a15d
Merge remote-tracking branch 'upstream/master' into core/idle-socket-…
kobelb Aug 11, 2020
0b0c488
Fixing plugin fixture tsconfig.json
kobelb Aug 11, 2020
2bd6bb9
Merge branch 'master' into core/idle-socket-timeout-2
elasticmachine Aug 11, 2020
44ab6dc
Merge branch 'master' into core/idle-socket-timeout-2
elasticmachine Aug 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
338 changes: 235 additions & 103 deletions src/core/server/http/http_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ beforeEach(() => {
port: 10002,
ssl: { enabled: false },
compression: { enabled: true },
socketTimeout: 2000,
} as HttpConfig;

configWithSSL = {
Expand Down Expand Up @@ -799,6 +800,7 @@ test('exposes route details of incoming request to a route handler', async () =>
authRequired: true,
xsrfRequired: false,
tags: [],
timeout: {},
},
});
});
Expand Down Expand Up @@ -906,6 +908,9 @@ test('exposes route details of incoming request to a route handler (POST + paylo
authRequired: true,
xsrfRequired: true,
tags: [],
timeout: {
payload: 10000,
},
body: {
parse: true, // hapi populates the default
maxBytes: 1024, // hapi populates the default
Expand Down Expand Up @@ -993,128 +998,255 @@ describe('body options', () => {
});

describe('timeout options', () => {
test('should accept a socket "timeout" which is 3 minutes in milliseconds, "300000" for a POST', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);
describe('payload timeout', () => {
test('POST routes set the payload timeout and automatically adjusts the idle socket timeout', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.post(
{
path: '/',
validate: false,
options: {
timeout: {
payload: 300000,
},
},
},
(context, req, res) => {
try {
return res.ok({
body: {
timeout: req.route.options.timeout,
},
});
} catch (err) {
return res.internalError({ body: err.message });
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener)
.post('/')
.send({ test: 1 })
.expect(200, {
timeout: {
payload: 300000,
idleSocket: 300001,
},
});
});

const router = new Router('', logger, enhanceWithContext);
router.post(
{
path: '/',
validate: false,
options: { timeout: 300000 },
},
(context, req, res) => {
try {
return res.ok({ body: { timeout: req.route.options.timeout } });
} catch (err) {
return res.internalError({ body: err.message });
test('DELETE routes set the payload timeout and automatically adjusts the idle socket timeout', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.delete(
{
path: '/',
validate: false,
options: {
timeout: {
payload: 300000,
},
},
},
(context, req, res) => {
try {
return res.ok({
body: {
timeout: {
payload: 300000,
idleSocket: 300001,
},
},
});
} catch (err) {
return res.internalError({ body: err.message });
}
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener).post('/').send({ test: 1 }).expect(200, {
timeout: 300000,
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener)
.delete('/')
.expect(200, {
timeout: {
payload: 300000,
idleSocket: 300001,
},
});
});
});

test('should accept a socket "timeout" which is 3 minutes in milliseconds, "300000" for a GET', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);
test('PUT routes set the payload timeout and automatically adjusts the idle socket timeout', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.put(
{
path: '/',
validate: false,
options: {
timeout: {
payload: 300000,
},
},
},
(context, req, res) => {
try {
return res.ok({
body: {
timeout: req.route.options.timeout,
},
});
} catch (err) {
return res.internalError({ body: err.message });
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener)
.put('/')
.expect(200, {
timeout: {
payload: 300000,
idleSocket: 300001,
},
});
});

const router = new Router('', logger, enhanceWithContext);
router.get(
{
path: '/',
validate: false,
options: { timeout: 300000 },
},
(context, req, res) => {
try {
return res.ok({ body: { timeout: req.route.options.timeout } });
} catch (err) {
return res.internalError({ body: err.message });
test('PATCH routes set the payload timeout and automatically adjusts the idle socket timeout', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.patch(
{
path: '/',
validate: false,
options: {
timeout: {
payload: 300000,
},
},
},
(context, req, res) => {
try {
return res.ok({
body: {
timeout: req.route.options.timeout,
},
});
} catch (err) {
return res.internalError({ body: err.message });
}
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener).get('/').expect(200, {
timeout: 300000,
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener)
.patch('/')
.expect(200, {
timeout: {
payload: 300000,
idleSocket: 300001,
},
});
});
});

test('should accept a socket "timeout" which is 3 minutes in milliseconds, "300000" for a DELETE', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);
describe('idleSocket timeout', () => {
test('sets the socket timeout when specified in the route', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.delete(
{
path: '/',
validate: false,
options: { timeout: 300000 },
},
(context, req, res) => {
try {
return res.ok({ body: { timeout: req.route.options.timeout } });
} catch (err) {
return res.internalError({ body: err.message });
const router = new Router('', logger, enhanceWithContext);
router.get(
{
path: '/',
validate: { body: schema.any() },
options: { timeout: { idleSocket: 12000 } },
},
(context, req, res) => {
return res.ok({
body: {
timeout: req.route.options.timeout,
},
});
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener).delete('/').expect(200, {
timeout: 300000,
});
});
);
registerRouter(router);

test('should accept a socket "timeout" which is 3 minutes in milliseconds, "300000" for a PUT', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);
await server.start();
await supertest(innerServer.listener)
.get('/')
.send()
.expect(200, {
timeout: {
idleSocket: 12000,
},
});
});

const router = new Router('', logger, enhanceWithContext);
router.put(
{
path: '/',
validate: false,
options: { timeout: 300000 },
},
(context, req, res) => {
try {
test(`payload timeout doesn't cause the specified idleSocket timeout to be overwritten`, async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.post(
{
path: '/',
validate: { body: schema.any() },
options: {
timeout: {
payload: 11000,
idleSocket: 12000,
},
},
},
(context, req, res) => {
return res.ok({ body: { timeout: req.route.options.timeout } });
} catch (err) {
return res.internalError({ body: err.message });
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener).put('/').expect(200, {
timeout: 300000,
);
registerRouter(router);

await server.start();
await supertest(innerServer.listener)
.post('/')
.send({})
.expect(200, {
timeout: {
payload: 11000,
idleSocket: 12000,
},
});
});
});

test('should accept a socket "timeout" which is 3 minutes in milliseconds, "300000" for a PATCH', async () => {
const { registerRouter, server: innerServer } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.patch(
{
path: '/',
validate: false,
options: { timeout: 300000 },
},
(context, req, res) => {
try {
// this can ideally be deleted in the future once https://github.com/hapijs/hapi/issues/4122 is resolved
test(`idleSocket timeout must be larger than the payload timeout`, async () => {
const { registerRouter } = await server.setup(config);

const router = new Router('', logger, enhanceWithContext);
router.post(
{
path: '/',
validate: { body: schema.any() },
options: {
timeout: {
payload: 13000,
idleSocket: 12000,
},
},
},
(context, req, res) => {
return res.ok({ body: { timeout: req.route.options.timeout } });
} catch (err) {
return res.internalError({ body: err.message });
}
}
);
registerRouter(router);
await server.start();
await supertest(innerServer.listener).patch('/').expect(200, {
timeout: 300000,
);

registerRouter(router);

await expect(() => server.start()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Payload timeout must be shorter than socket timeout: post /"`
);
});
});
});
Expand Down
Loading