|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { readKey } = require('../common/fixtures'); |
| 5 | + |
| 6 | +if (!common.hasCrypto) |
| 7 | + common.skip('missing crypto'); |
| 8 | + |
| 9 | +const assert = require('assert'); |
| 10 | +const { createServer } = require('https'); |
| 11 | +const { connect } = require('tls'); |
| 12 | + |
| 13 | +// This test validates that the 'timeout' event fires |
| 14 | +// after server.headersTimeout. |
| 15 | + |
| 16 | +const headers = |
| 17 | + 'GET / HTTP/1.1\r\n' + |
| 18 | + 'Host: localhost\r\n' + |
| 19 | + 'Agent: node\r\n'; |
| 20 | + |
| 21 | +const server = createServer({ |
| 22 | + key: readKey('agent1-key.pem'), |
| 23 | + cert: readKey('agent1-cert.pem'), |
| 24 | + ca: readKey('ca1-cert.pem'), |
| 25 | +}, common.mustNotCall()); |
| 26 | + |
| 27 | +let sendCharEvery = 1000; |
| 28 | + |
| 29 | +// 40 seconds is the default |
| 30 | +assert.strictEqual(server.headersTimeout, 40 * 1000); |
| 31 | + |
| 32 | +// pass a REAL env variable to shortening up the default |
| 33 | +// value which is 40s otherwise |
| 34 | +// this is useful for manual testing |
| 35 | +if (!process.env.REAL) { |
| 36 | + sendCharEvery = common.platformTimeout(10); |
| 37 | + server.headersTimeout = 2 * sendCharEvery; |
| 38 | +} |
| 39 | + |
| 40 | +server.once('timeout', common.mustCall((socket) => { |
| 41 | + socket.destroy(); |
| 42 | +})); |
| 43 | + |
| 44 | +server.listen(0, common.mustCall(() => { |
| 45 | + const client = connect({ |
| 46 | + port: server.address().port, |
| 47 | + rejectUnauthorized: false |
| 48 | + }); |
| 49 | + client.write(headers); |
| 50 | + client.write('X-CRASH: '); |
| 51 | + |
| 52 | + const interval = setInterval(() => { |
| 53 | + client.write('a'); |
| 54 | + }, sendCharEvery); |
| 55 | + |
| 56 | + client.resume(); |
| 57 | + |
| 58 | + const onClose = common.mustCall(() => { |
| 59 | + client.removeListener('close', onClose); |
| 60 | + client.removeListener('error', onClose); |
| 61 | + client.removeListener('end', onClose); |
| 62 | + clearInterval(interval); |
| 63 | + server.close(); |
| 64 | + }); |
| 65 | + |
| 66 | + client.on('error', onClose); |
| 67 | + client.on('close', onClose); |
| 68 | + client.on('end', onClose); |
| 69 | +})); |
0 commit comments