/
test-http-slow-headers.js
56 lines (44 loc) · 1.37 KB
/
test-http-slow-headers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const common = require('../common');
const assert = require('assert');
const { createServer } = require('http');
const { connect } = require('net');
// This test validates that the 'timeout' event fires
// after server.headersTimeout.
const headers =
'GET / HTTP/1.1\r\n' +
'Host: localhost\r\n' +
'Agent: node\r\n';
const server = createServer(common.mustNotCall());
let sendCharEvery = 1000;
// 40 seconds is the default
assert.strictEqual(server.headersTimeout, 40 * 1000);
// Pass a REAL env variable to shortening up the default
// value which is 40s otherwise this is useful for manual
// testing
if (!process.env.REAL) {
sendCharEvery = common.platformTimeout(10);
server.headersTimeout = 2 * sendCharEvery;
}
server.once('timeout', common.mustCall((socket) => {
socket.destroy();
}));
server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
client.write(headers);
client.write('X-CRASH: ');
const interval = setInterval(() => {
client.write('a');
}, sendCharEvery);
client.resume();
const onClose = common.mustCall(() => {
client.removeListener('close', onClose);
client.removeListener('error', onClose);
client.removeListener('end', onClose);
clearInterval(interval);
server.close();
});
client.on('error', onClose);
client.on('close', onClose);
client.on('end', onClose);
}));