|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const http = require('http'); |
| 5 | + |
| 6 | +// Test that ClientRequest#end with default options |
| 7 | +// and empty payload sends neither Content-Length nor Transfer-Encoding. |
| 8 | +// Sending Content-Length: 0 would be acceptable, but is unnecessary. |
| 9 | +const upload = 'PUT / HTTP/1.1\r\n\r\n'; |
| 10 | +const response = ''; |
| 11 | + |
| 12 | +// Test that the upload is properly received with the same headers, |
| 13 | +// regardless of request method. |
| 14 | +const methods = [ 'GET', 'HEAD', 'DELETE', 'POST', 'PATCH', 'PUT', 'OPTIONS' ]; |
| 15 | + |
| 16 | +const server = http.createServer(common.mustCall(function(req, res) { |
| 17 | + req.on('data', function(chunk) { |
| 18 | + assert.strictEqual(chunk.toString(), upload); |
| 19 | + }); |
| 20 | + res.setHeader('Content-Type', 'text/plain'); |
| 21 | + res.write(`${req.method}\r\n`); |
| 22 | + for (let i = 0; i < req.rawHeaders.length; i += 2) { |
| 23 | + // Ignore a couple headers that may vary |
| 24 | + if (req.rawHeaders[i].toLowerCase() === 'host') continue; |
| 25 | + if (req.rawHeaders[i].toLowerCase() === 'connection') continue; |
| 26 | + res.write(`${req.rawHeaders[i]}: ${req.rawHeaders[i + 1]}\r\n`); |
| 27 | + } |
| 28 | + res.end(); |
| 29 | +}), methods.length); |
| 30 | + |
| 31 | +server.listen(0, function tryNextRequest() { |
| 32 | + const method = methods.pop(); |
| 33 | + if (method === undefined) return; |
| 34 | + const port = server.address().port; |
| 35 | + const req = http.request({ method, port }, function(res) { |
| 36 | + const chunks = []; |
| 37 | + res.on('data', function(chunk) { |
| 38 | + chunks.push(chunk); |
| 39 | + }); |
| 40 | + res.on('end', function() { |
| 41 | + const received = Buffer.concat(chunks).toString(); |
| 42 | + const expected = method.toLowerCase() + '\r\n' + response; |
| 43 | + assert.strictEqual(received.toLowerCase(), expected); |
| 44 | + tryNextRequest(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + req.end(); |
| 49 | +}).unref(); |
0 commit comments