0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-http-content-length.js
Antoine du Hamel 4a54a80aa3
test: replace .substr with .slice
`String.prototype.substr` is deprecated, and using it will raise an
error when using ESLint 9+.

Co-authored-by: =?UTF-8?q?Micha=C3=ABl=20Zasso?= <targos@protonmail.com>
PR-URL: https://github.com/nodejs/node/pull/53070
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2024-05-22 05:08:59 +00:00

94 lines
2.3 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');
const expectedHeadersMultipleWrites = {
'connection': 'keep-alive',
'transfer-encoding': 'chunked',
};
const expectedHeadersEndWithData = {
'connection': 'keep-alive',
'content-length': String('hello world'.length),
};
const expectedHeadersEndNoData = {
'connection': 'keep-alive',
'content-length': '0',
};
const countdown = new Countdown(3, () => server.close());
const server = http.createServer(function(req, res) {
res.removeHeader('Date');
res.setHeader('Keep-Alive', 'timeout=1');
switch (req.url.slice(1)) {
case 'multiple-writes':
delete req.headers.host;
assert.deepStrictEqual(req.headers, expectedHeadersMultipleWrites);
res.write('hello');
res.end('world');
break;
case 'end-with-data':
delete req.headers.host;
assert.deepStrictEqual(req.headers, expectedHeadersEndWithData);
res.end('hello world');
break;
case 'empty':
delete req.headers.host;
assert.deepStrictEqual(req.headers, expectedHeadersEndNoData);
res.end();
break;
default:
throw new Error('Unreachable');
}
countdown.dec();
});
server.listen(0, function() {
let req;
req = http.request({
port: this.address().port,
method: 'POST',
path: '/multiple-writes'
});
req.removeHeader('Date');
req.write('hello ');
req.end('world');
req.on('response', function(res) {
assert.deepStrictEqual(res.headers, { ...expectedHeadersMultipleWrites, 'keep-alive': 'timeout=1' });
res.resume();
});
req = http.request({
port: this.address().port,
method: 'POST',
path: '/end-with-data'
});
req.removeHeader('Date');
req.end('hello world');
req.on('response', function(res) {
assert.deepStrictEqual(res.headers, { ...expectedHeadersEndWithData, 'keep-alive': 'timeout=1' });
res.resume();
});
req = http.request({
port: this.address().port,
method: 'POST',
path: '/empty'
});
req.removeHeader('Date');
req.end();
req.on('response', function(res) {
assert.deepStrictEqual(res.headers, { ...expectedHeadersEndNoData, 'keep-alive': 'timeout=1' });
res.resume();
});
});