Skip to content

Commit

Permalink
test: remove unnecessary .toString() calls in HTTP tests
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
addaleax authored and targos committed Jul 12, 2022
1 parent 61e6d78 commit f2f7d7b
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion test/parallel/test-http-missing-header-separator-cr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http-server-close-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http-server-close-idle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http-server-headers-timeout-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http-server-headers-timeout-pipelining.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http-server-request-timeout-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http-server-request-timeout-pipelining.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http-server-request-timeout-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http-server-request-timeouts-mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-transfer-encoding-smuggling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-https-server-close-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-https-server-close-idle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit f2f7d7b

Please sign in to comment.