|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const cp = require('child_process'); |
| 5 | +const http = require('http'); |
| 6 | + |
| 7 | +if (process.argv[2] === 'server') { |
| 8 | + const server = http.createServer((req, res) => { |
| 9 | + if(req.method === "HEAD") { |
| 10 | + res.writeHead(200); |
| 11 | + res.end(); |
| 12 | + } |
| 13 | + else if (req.url === '/204') { |
| 14 | + res.writeHead(204); |
| 15 | + res.end(); |
| 16 | + } |
| 17 | + else if (req.url === '/304') { |
| 18 | + res.writeHead(304); |
| 19 | + res.end(); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + server.listen(0, () => { |
| 24 | + process.send(server.address().port); |
| 25 | + }); |
| 26 | +} else { |
| 27 | + const serverProcess = cp.fork(__filename, ['server'], { |
| 28 | + stdio: ['ignore', 'ignore', 'ignore', 'ipc'] |
| 29 | + }); |
| 30 | + serverProcess.once('message', common.mustCall((port) => { |
| 31 | + serverProcess.channel.unref(); |
| 32 | + serverProcess.unref(); |
| 33 | + const agent = new http.Agent({ keepAlive: true }); |
| 34 | + |
| 35 | + // Make requests without consuming response |
| 36 | + http.get({ method: "HEAD", host: common.localhostIPv4, port, agent }, common.mustCall()); |
| 37 | + http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/204' }, common.mustCall()); |
| 38 | + http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/304' }, common.mustCall()); |
| 39 | + |
| 40 | + // Ensure handlers are called/not called as expected |
| 41 | + const cb = (res) => { |
| 42 | + res.on('end', common.mustCall()) |
| 43 | + res.on('data', common.mustNotCall()) |
| 44 | + } |
| 45 | + http.get({ method: "HEAD", host: common.localhostIPv4, port, agent }, cb); |
| 46 | + http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/204' }, cb); |
| 47 | + http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/304' }, cb); |
| 48 | + })); |
| 49 | + |
| 50 | + // HEAD, 204, and 304 requests should not block script exit |
| 51 | + setTimeout(common.mustNotCall(), common.platformTimeout(3000)).unref(); |
| 52 | +} |
0 commit comments