|
| 1 | +'use strict'; |
| 2 | +// Test that queued (pipelined) outgoing responses are destroyed when the |
| 3 | +// socket closes before the first response has finished. Previously, |
| 4 | +// socketOnClose only aborted state.incoming (pending requests) but left |
| 5 | +// state.outgoing responses with socket=null alive forever. |
| 6 | + |
| 7 | +const common = require('../common'); |
| 8 | +const http = require('http'); |
| 9 | +const net = require('net'); |
| 10 | +const assert = require('assert'); |
| 11 | + |
| 12 | +let requestCount = 0; |
| 13 | + |
| 14 | +const server = http.createServer(common.mustCall((req, res) => { |
| 15 | + requestCount++; |
| 16 | + |
| 17 | + if (requestCount === 1) { |
| 18 | + // Keep the first response open so the second response is queued in |
| 19 | + // state.outgoing with socket === null. |
| 20 | + res.writeHead(200); |
| 21 | + res.write('start'); |
| 22 | + // Intentionally do not call res.end(). |
| 23 | + } else { |
| 24 | + // The second response should be queued — no socket assigned yet. |
| 25 | + assert.strictEqual(res.socket, null); |
| 26 | + assert.strictEqual(res.destroyed, false); |
| 27 | + assert.strictEqual(res.closed, false); |
| 28 | + |
| 29 | + res.on('close', common.mustCall(() => { |
| 30 | + assert.strictEqual(res.destroyed, true); |
| 31 | + assert.strictEqual(res.closed, true); |
| 32 | + server.close(); |
| 33 | + })); |
| 34 | + |
| 35 | + // Simulate client dying while first response is still in flight. |
| 36 | + req.socket.destroy(); |
| 37 | + } |
| 38 | +}, 2)); |
| 39 | + |
| 40 | +server.listen(0, common.mustCall(function() { |
| 41 | + const port = this.address().port; |
| 42 | + const client = net.connect(port); |
| 43 | + |
| 44 | + // Send two pipelined HTTP/1.1 requests at once. |
| 45 | + client.write( |
| 46 | + `GET /1 HTTP/1.1\r\nHost: localhost:${port}\r\n\r\n` + |
| 47 | + `GET /2 HTTP/1.1\r\nHost: localhost:${port}\r\n\r\n`, |
| 48 | + ); |
| 49 | + client.resume(); |
| 50 | +})); |
0 commit comments