Skip to content

Commit b753797

Browse files
awwright98lenviaduh95
committed
test: verify request payload is uploaded consistently
Node.js seems to change how it is uploaded based on the method, but HTTP doesn't make any distinction. Co-authored-by: Austin Wright <[email protected]> Co-authored-by: Lenvin Gonsalves <[email protected]> Co-authored-by: Antoine du Hamel <[email protected]>
1 parent ff7910b commit b753797

4 files changed

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

0 commit comments

Comments
 (0)