Skip to content

Commit 28c3a9d

Browse files
awwrightTrott
authored andcommitted
test: add test for HTTP server response with Connection: close
PR-URL: #29836 Refs: #29758 Refs: #29649 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
1 parent f58e8eb commit 28c3a9d

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

test/parallel/test-http-generic-streams.js

+80
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const MakeDuplexPair = require('../common/duplexpair');
2323
res.on('data', common.mustCall((data) => {
2424
assert.strictEqual(data, testData);
2525
}));
26+
res.on('end', common.mustCall());
2627
}));
2728
req.end();
2829
}
@@ -58,3 +59,82 @@ const MakeDuplexPair = require('../common/duplexpair');
5859
doRequest();
5960
});
6061
}
62+
63+
// Test 3: Connection: close request/response with chunked
64+
{
65+
const testData = 'Hello, World!\n';
66+
const server = http.createServer(common.mustCall((req, res) => {
67+
req.setEncoding('utf8');
68+
req.resume();
69+
req.on('data', common.mustCall(function test3_req_data(data) {
70+
assert.strictEqual(data, testData);
71+
}));
72+
req.once('end', function() {
73+
res.statusCode = 200;
74+
res.setHeader('Content-Type', 'text/plain');
75+
res.write(testData);
76+
res.end();
77+
});
78+
}));
79+
80+
const { clientSide, serverSide } = MakeDuplexPair();
81+
server.emit('connection', serverSide);
82+
clientSide.on('end', common.mustCall());
83+
serverSide.on('end', common.mustCall());
84+
85+
const req = http.request({
86+
createConnection: common.mustCall(() => clientSide),
87+
method: 'PUT',
88+
headers: { 'Connection': 'close' }
89+
}, common.mustCall((res) => {
90+
res.setEncoding('utf8');
91+
res.on('data', common.mustCall(function test3_res_data(data) {
92+
assert.strictEqual(data, testData);
93+
}));
94+
res.on('end', common.mustCall());
95+
}));
96+
req.write(testData);
97+
req.end();
98+
}
99+
100+
// Test 4: Connection: close request/response with Content-Length
101+
// The same as Test 3, but with Content-Length headers
102+
{
103+
const testData = 'Hello, World!\n';
104+
const server = http.createServer(common.mustCall((req, res) => {
105+
assert.strictEqual(req.headers['content-length'], testData.length + '');
106+
req.setEncoding('utf8');
107+
req.on('data', common.mustCall(function test4_req_data(data) {
108+
assert.strictEqual(data, testData);
109+
}));
110+
req.once('end', function() {
111+
res.statusCode = 200;
112+
res.setHeader('Content-Type', 'text/plain');
113+
res.setHeader('Content-Length', testData.length);
114+
res.write(testData);
115+
res.end();
116+
});
117+
118+
}));
119+
120+
const { clientSide, serverSide } = MakeDuplexPair();
121+
server.emit('connection', serverSide);
122+
clientSide.on('end', common.mustCall());
123+
serverSide.on('end', common.mustCall());
124+
125+
const req = http.request({
126+
createConnection: common.mustCall(() => clientSide),
127+
method: 'PUT',
128+
headers: { 'Connection': 'close' }
129+
}, common.mustCall((res) => {
130+
res.setEncoding('utf8');
131+
assert.strictEqual(res.headers['content-length'], testData.length + '');
132+
res.on('data', common.mustCall(function test4_res_data(data) {
133+
assert.strictEqual(data, testData);
134+
}));
135+
res.on('end', common.mustCall());
136+
}));
137+
req.setHeader('Content-Length', testData.length);
138+
req.write(testData);
139+
req.end();
140+
}

0 commit comments

Comments
 (0)