mirror of
https://github.com/nodejs/node.git
synced 2025-05-01 17:03:34 +00:00

Let’s not have bad examples in our test suite and instead use the proper way of converting stream data to UTF-8 (i.e. `stream.setEncoding('utf8')`) in all places. PR-URL: https://github.com/nodejs/node/pull/43731 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { createServer } = require('http');
|
|
const { connect } = require('net');
|
|
|
|
// This test validates that the server returns 408
|
|
// after server.requestTimeout if the client
|
|
// does not complete a request when using pipelining.
|
|
|
|
const requestTimeout = common.platformTimeout(2000);
|
|
const server = createServer({
|
|
headersTimeout: 0,
|
|
requestTimeout,
|
|
keepAliveTimeout: 0,
|
|
connectionsCheckingInterval: requestTimeout / 4
|
|
}, common.mustCallAtLeast((req, res) => {
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
res.end();
|
|
}));
|
|
|
|
assert.strictEqual(server.requestTimeout, requestTimeout);
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const client = connect(server.address().port);
|
|
let second = false;
|
|
let response = '';
|
|
|
|
client.setEncoding('utf8');
|
|
client.on('data', common.mustCallAtLeast((chunk) => {
|
|
response += chunk;
|
|
|
|
// First response has ended
|
|
if (!second && response.endsWith('\r\n\r\n')) {
|
|
assert.strictEqual(
|
|
response.split('\r\n')[0],
|
|
'HTTP/1.1 200 OK'
|
|
);
|
|
|
|
response = '';
|
|
second = true;
|
|
}
|
|
}, 1));
|
|
|
|
const errOrEnd = common.mustCall(function(err) {
|
|
if (!second) {
|
|
return;
|
|
}
|
|
|
|
assert.strictEqual(
|
|
response,
|
|
// Empty because of https://github.com/nodejs/node/commit/e8d7fedf7cad6e612e4f2e0456e359af57608ac7
|
|
// 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
|
|
''
|
|
);
|
|
server.close();
|
|
});
|
|
|
|
client.on('error', errOrEnd);
|
|
client.on('end', errOrEnd);
|
|
|
|
// Send two requests using pipelining. Delay before finishing the second one
|
|
client.resume();
|
|
client.write('GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\nGET / HTTP/1.1\r\nConnection: ');
|
|
|
|
// Complete the request
|
|
setTimeout(() => {
|
|
client.write('close\r\n\r\n');
|
|
}, requestTimeout * 2).unref();
|
|
}));
|