test: remove unnecessary .toString() calls in HTTP tests

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>
This commit is contained in:
Anna Henningsen 2022-07-11 16:40:08 +02:00 committed by GitHub
parent 02eb10b87b
commit febb5390aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 37 additions and 19 deletions

View File

@ -26,7 +26,7 @@ server.listen(0, common.mustSucceed(() => {
let response = '';
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
client.setEncoding('utf8');

View File

@ -31,8 +31,10 @@ server.listen(0, function() {
const client2 = connect(port);
let response = '';
client2.setEncoding('utf8');
client2.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));

View File

@ -33,8 +33,10 @@ server.listen(0, function() {
const client2 = connect(port);
let response = '';
client2.setEncoding('utf8');
client2.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));

View File

@ -33,8 +33,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
const errOrEnd = common.mustSucceed(function(err) {

View File

@ -33,8 +33,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
const errOrEnd = common.mustSucceed(function(err) {

View File

@ -47,8 +47,9 @@ server.listen(0, common.mustCall(() => {
let second = false;
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCallAtLeast((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
// First response has ended
if (!second && response.endsWith('\r\n\r\n')) {

View File

@ -33,8 +33,9 @@ server.listen(0, common.mustCall(() => {
let second = false;
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCallAtLeast((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
// First response has ended
if (!second && response.endsWith('\r\n\r\n')) {

View File

@ -40,8 +40,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
client.resume();

View File

@ -28,8 +28,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
const errOrEnd = common.mustSucceed(function(err) {

View File

@ -40,8 +40,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
const errOrEnd = common.mustSucceed(function(err) {

View File

@ -28,8 +28,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
const errOrEnd = common.mustSucceed(function(err) {

View File

@ -45,8 +45,9 @@ server.listen(0, common.mustCall(() => {
let second = false;
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCallAtLeast((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
// First response has ended
if (!second && response.endsWith('\r\n\r\n')) {

View File

@ -27,8 +27,9 @@ server.listen(0, common.mustCall(() => {
let second = false;
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCallAtLeast((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
// First response has ended
if (!second && response.endsWith('\r\n\r\n')) {

View File

@ -33,8 +33,9 @@ server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCallAtLeast((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}, 1));
client.on('end', common.mustCall(() => {

View File

@ -43,8 +43,9 @@ function createClient(server) {
completed: false
};
request.client.setEncoding('utf8');
request.client.on('data', common.mustCallAtLeast((chunk) => {
request.response += chunk.toString('utf-8');
request.response += chunk;
}));
request.client.on('end', common.mustCall(() => {

View File

@ -34,7 +34,7 @@ server.listen(0, common.mustSucceed(() => {
let response = '';
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
client.setEncoding('utf8');

View File

@ -35,7 +35,7 @@ server.listen(0, common.mustSucceed(() => {
// Verify that the server listener is never called
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
}));
client.setEncoding('utf8');

View File

@ -42,8 +42,9 @@ server.listen(0, function() {
const client2 = connect({ port, rejectUnauthorized: false });
let response = '';
client2.setEncoding('utf8');
client2.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));

View File

@ -44,8 +44,9 @@ server.listen(0, function() {
const client2 = connect({ port, rejectUnauthorized: false });
let response = '';
client2.setEncoding('utf8');
client2.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
response += chunk;
if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));