Skip to content

Commit fb8470a

Browse files
lpincatargos
authored andcommitted
test: deflake test-http-remove-header-stays-removed
Instead of relying on a timer, verify that `socket.end()` is called when the `'finish'` event is emitted on the `ServerResponse` object. PR-URL: #55004 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
1 parent ee0a98b commit fb8470a

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

test/parallel/test-http-remove-header-stays-removed.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const assert = require('assert');
2525

2626
const http = require('http');
2727

28-
const server = http.createServer(function(request, response) {
28+
const server = http.createServer(common.mustCall(function(request, response) {
29+
const socket = response.socket;
30+
2931
// Removed headers should stay removed, even if node automatically adds them
3032
// to the output:
3133
response.removeHeader('connection');
@@ -36,32 +38,29 @@ const server = http.createServer(function(request, response) {
3638
response.removeHeader('date');
3739
response.setHeader('date', 'coffee o clock');
3840

39-
response.end('beep boop\n');
40-
});
41-
42-
let response = '';
41+
response.on('finish', common.mustCall(function() {
42+
// The socket should be closed immediately, with no keep-alive, because
43+
// no content-length or transfer-encoding are used.
44+
assert.strictEqual(socket.writableEnded, true);
45+
}));
4346

44-
process.on('exit', function() {
45-
assert.strictEqual(response, 'beep boop\n');
46-
console.log('ok');
47-
});
47+
response.end('beep boop\n');
48+
}));
4849

4950
server.listen(0, function() {
5051
http.get({ port: this.address().port }, function(res) {
5152
assert.strictEqual(res.statusCode, 200);
5253
assert.deepStrictEqual(res.headers, { date: 'coffee o clock' });
5354

55+
let response = '';
5456
res.setEncoding('ascii');
5557
res.on('data', function(chunk) {
5658
response += chunk;
57-
if (response === 'beep boop\n') {
58-
setTimeout(function() {
59-
// The socket should be closed immediately, with no keep-alive, because
60-
// no content-length or transfer-encoding are used:
61-
assert.strictEqual(res.socket.closed, true);
62-
server.close();
63-
}, common.platformTimeout(15));
64-
}
59+
});
60+
61+
res.on('end', function() {
62+
assert.strictEqual(response, 'beep boop\n');
63+
server.close();
6564
});
6665
});
6766
});

0 commit comments

Comments
 (0)