|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const http = require('http'); |
| 4 | +const assert = require('assert'); |
| 5 | +const Countdown = require('../common/countdown'); |
| 6 | + |
| 7 | +// The HEAD:204, GET:200 is the most pathological test case. |
| 8 | +// GETs following a 204 response with a content-encoding header failed. |
| 9 | +// Responses without bodies and without content-length or encoding caused |
| 10 | +// the socket to be closed. |
| 11 | +const codes = [204, 200, 200, 304, 200]; |
| 12 | +const methods = ['HEAD', 'HEAD', 'GET', 'HEAD', 'GET']; |
| 13 | + |
| 14 | +const sockets = []; |
| 15 | +const agent = new http.Agent(); |
| 16 | +agent.maxSockets = 1; |
| 17 | + |
| 18 | +const countdown = new Countdown(codes.length, () => server.close()); |
| 19 | + |
| 20 | +const server = http.createServer(common.mustCall((req, res) => { |
| 21 | + const code = codes.shift(); |
| 22 | + assert.strictEqual(typeof code, 'number'); |
| 23 | + assert.ok(code > 0); |
| 24 | + res.writeHead(code, {}); |
| 25 | + res.end(); |
| 26 | +}, codes.length)); |
| 27 | + |
| 28 | +function nextRequest() { |
| 29 | + const request = http.request({ |
| 30 | + port: server.address().port, |
| 31 | + path: '/', |
| 32 | + agent: agent, |
| 33 | + method: methods.shift() |
| 34 | + }, common.mustCall((response) => { |
| 35 | + response.on('end', common.mustCall(() => { |
| 36 | + if (countdown.dec()) { |
| 37 | + nextRequest(); |
| 38 | + } |
| 39 | + assert.strictEqual(sockets.length, 1); |
| 40 | + })); |
| 41 | + response.resume(); |
| 42 | + })); |
| 43 | + request.on('socket', common.mustCall((socket) => { |
| 44 | + if (!sockets.includes(socket)) { |
| 45 | + sockets.push(socket); |
| 46 | + } |
| 47 | + })); |
| 48 | + request.end(); |
| 49 | +} |
| 50 | + |
| 51 | +server.listen(0, common.mustCall(nextRequest)); |
0 commit comments