Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit da0ef90

Browse files
committedSep 4, 2022
test: http add tests for content-length mismatch
1 parent 26651f0 commit da0ef90

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
function shouldThrowOnMismatch() {
8+
const server = http.createServer(common.mustCall((req, res) => {
9+
res.setHeader('Content-Length', 5);
10+
assert.throws(() => {
11+
res.write('hello');
12+
res.write('a');
13+
res.statusCode = 200;
14+
}, {
15+
code: 'ERR_HTTP_CONTENT_LENGTH_MISMATCH'
16+
})
17+
res.end();
18+
}));
19+
20+
server.listen(0, () => {
21+
http.get({
22+
port: server.address().port,
23+
}, common.mustCall((res) => {
24+
console.log(res.statusMessage);
25+
res.resume();
26+
assert.strictEqual(res.statusCode, 200);
27+
server.close();
28+
}));
29+
});
30+
}
31+
32+
function shouldNotThrow() {
33+
const server = http.createServer(common.mustCall((req, res) => {
34+
assert.doesNotThrow(() => {
35+
res.write('hello');
36+
res.write('a');
37+
res.statusCode = 200;
38+
})
39+
res.end();
40+
}));
41+
42+
server.listen(0, () => {
43+
http.get({
44+
port: server.address().port,
45+
headers: {
46+
'Content-Length': '6'
47+
}
48+
}, common.mustCall((res) => {
49+
console.log(res.statusMessage);
50+
res.resume();
51+
assert.strictEqual(res.statusCode, 200);
52+
server.close();
53+
}));
54+
});
55+
}
56+
57+
function shouldOverwriteContentLength() {
58+
const server = http.createServer(common.mustCall((req, res) => {
59+
res.writeHead(200, {
60+
'Content-Length': '1'
61+
})
62+
assert.throws(() => {
63+
res.write('hello');
64+
res.write('a');
65+
res.statusCode = 200;
66+
})
67+
res.end();
68+
}));
69+
70+
server.listen(0, () => {
71+
http.get({
72+
port: server.address().port,
73+
headers: {
74+
'Content-Length': '6'
75+
}
76+
}, common.mustCall((res) => {
77+
console.log(res.statusMessage);
78+
res.resume();
79+
assert.strictEqual(res.statusCode, 200);
80+
server.close();
81+
}));
82+
});
83+
}
84+
85+
shouldThrowOnMismatch()
86+
shouldNotThrow()
87+
shouldOverwriteContentLength()

0 commit comments

Comments
 (0)
Please sign in to comment.