mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
test: avoid idle HTTP/HTTPS connections
Single-use requests otherwise wait for the keep-alive timeout. Use nonpersistent agents where agent selection is unrelated to coverage. For default-agent tests, close the server after consuming the response. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: https://github.com/nodejs/node/pull/65980 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
This commit is contained in:
37 files changed
+45
-15
No files matched your search
@@ -55,6 +55,7 @@ const server = new http.Server(common.mustCallAtLeast(function(req, res) {
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const req = http.request({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
method: 'POST',
|
||||
path: '/',
|
||||
|
||||
@@ -51,5 +51,5 @@ const httpServer = http.createServer(common.mustCall(function(req, res) {
|
||||
}));
|
||||
|
||||
httpServer.listen(0, function() {
|
||||
http.get({ port: this.address().port });
|
||||
http.get({ port: this.address().port, agent: false });
|
||||
});
|
||||
@@ -29,6 +29,6 @@ server.listen(0, common.mustCall(() => {
|
||||
});
|
||||
|
||||
expectedSuccesses.forEach((method) => {
|
||||
http.request({ method, port: server.address().port }).end();
|
||||
http.request({ method, port: server.address().port, agent: false }).end();
|
||||
});
|
||||
}));
|
||||
@@ -29,6 +29,7 @@ const server = http.createServer((req, res) => {
|
||||
server.close();
|
||||
}).listen(0, common.mustCall(() => {
|
||||
http.request({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
encoding: 'utf8'
|
||||
}, common.mustCall((res) => {
|
||||
|
||||
@@ -49,6 +49,7 @@ function test() {
|
||||
}));
|
||||
|
||||
const req = http.get({
|
||||
agent: false,
|
||||
socketPath: common.PIPE,
|
||||
headers: { 'Content-Length': '1' },
|
||||
method: 'POST',
|
||||
|
||||
@@ -43,6 +43,6 @@ for (const testCase of testCases) {
|
||||
server.listen(0, function() {
|
||||
// make the request
|
||||
const url = new URL(`http://${testCase.username}:${testCase.password}@localhost:${this.address().port}`);
|
||||
http.request(url).end();
|
||||
http.request(url, { agent: false }).end();
|
||||
});
|
||||
}
|
||||
@@ -44,7 +44,6 @@ for (const { mod, createServer } of [
|
||||
assert.strictEqual(req.headers['x-port'], `${server.address().port}`);
|
||||
res.writeHead(200);
|
||||
res.end('ok');
|
||||
server.close();
|
||||
})).listen(0, common.mustCall(() => {
|
||||
mod.globalAgent.defaultPort = server.address().port;
|
||||
mod.get({
|
||||
@@ -54,6 +53,7 @@ for (const { mod, createServer } of [
|
||||
'x-port': server.address().port
|
||||
}
|
||||
}, common.mustCall((res) => {
|
||||
res.on('end', common.mustCall(() => server.close()));
|
||||
res.resume();
|
||||
}));
|
||||
}));
|
||||
|
||||
@@ -14,6 +14,7 @@ const server = http.createServer(common.mustCall(function(req, res) {
|
||||
}));
|
||||
server.listen(0, common.localhostIPv4, function() {
|
||||
http.request({
|
||||
agent: false,
|
||||
method: 'POST',
|
||||
host: common.localhostIPv4,
|
||||
port: this.address().port,
|
||||
|
||||
@@ -17,6 +17,7 @@ const server = http.createServer(common.mustCall(function(req, res) {
|
||||
}));
|
||||
server.listen(0, common.localhostIPv4, function() {
|
||||
http.request({
|
||||
agent: false,
|
||||
method: 'POST',
|
||||
host: common.localhostIPv4,
|
||||
port: this.address().port,
|
||||
|
||||
@@ -38,7 +38,7 @@ const testResBody = 'response content\n';
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const req = http.request({
|
||||
port: server.address().port, path: '/'
|
||||
port: server.address().port, path: '/', agent: false
|
||||
});
|
||||
|
||||
req.end();
|
||||
@@ -79,7 +79,7 @@ const testResBody = 'response content\n';
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const req = http.request({
|
||||
port: server.address().port, path: '/'
|
||||
port: server.address().port, path: '/', agent: false
|
||||
});
|
||||
|
||||
req.end();
|
||||
|
||||
@@ -35,6 +35,7 @@ function test(headers) {
|
||||
|
||||
server.listen(0, common.mustCall(function() {
|
||||
const request = http.request({
|
||||
agent: false,
|
||||
port: this.address().port,
|
||||
method: 'HEAD',
|
||||
path: '/'
|
||||
|
||||
@@ -34,7 +34,7 @@ http.createServer(function(q, s) {
|
||||
s.end();
|
||||
this.close();
|
||||
}).listen(0, common.mustCall(function() {
|
||||
http.request({ port: this.address().port })
|
||||
http.request({ port: this.address().port, agent: false })
|
||||
.on('response', common.mustCall(function(res) {
|
||||
let data = '';
|
||||
|
||||
|
||||
@@ -14,5 +14,5 @@ const httpServer = http.createServer(common.mustCall(function(req, res) {
|
||||
}));
|
||||
|
||||
httpServer.listen(0, common.mustCall(function() {
|
||||
http.get({ port: this.address().port });
|
||||
http.get({ port: this.address().port, agent: false });
|
||||
}));
|
||||
@@ -25,6 +25,7 @@ server.listen(0);
|
||||
|
||||
server.on('listening', common.mustCall(function() {
|
||||
const clientRequest = http.request({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
method: 'GET',
|
||||
path: '/'
|
||||
|
||||
@@ -33,6 +33,7 @@ http.createServer(function(req, res) {
|
||||
this.close();
|
||||
}).listen(0, function() {
|
||||
const req = http.request({
|
||||
agent: false,
|
||||
port: this.address().port,
|
||||
method: 'PUT'
|
||||
});
|
||||
|
||||
@@ -36,6 +36,7 @@ const OutgoingMessage = http.OutgoingMessage;
|
||||
|
||||
server.on('listening', common.mustCall(function() {
|
||||
const clientRequest = http.request({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
method: 'GET',
|
||||
path: '/'
|
||||
@@ -62,6 +63,7 @@ const OutgoingMessage = http.OutgoingMessage;
|
||||
|
||||
server.on('listening', common.mustCall(() => {
|
||||
const req = http.request({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
method: 'GET',
|
||||
path: '/'
|
||||
|
||||
@@ -20,5 +20,5 @@ const httpServer = http.createServer(common.mustCall(function(req, res) {
|
||||
}));
|
||||
|
||||
httpServer.listen(0, common.mustCall(function() {
|
||||
http.get({ port: this.address().port });
|
||||
http.get({ port: this.address().port, agent: false });
|
||||
}));
|
||||
@@ -18,7 +18,7 @@ const http = require('http');
|
||||
common.mustCall(() => {
|
||||
http.get(
|
||||
'http://example.com/testpath',
|
||||
{ hostname: 'localhost', port: server.address().port },
|
||||
{ hostname: 'localhost', port: server.address().port, agent: false },
|
||||
common.mustCall((res) => {
|
||||
res.resume();
|
||||
})
|
||||
|
||||
@@ -16,6 +16,7 @@ const server = http.createServer(function(req, res) {
|
||||
|
||||
server.listen(0, function() {
|
||||
const req = http.request({
|
||||
agent: false,
|
||||
method: 'POST',
|
||||
port: this.address().port
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ const http = require('http');
|
||||
|
||||
function request(server) {
|
||||
http.get({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
path: '/',
|
||||
}, (res) => {
|
||||
|
||||
@@ -14,6 +14,7 @@ const server = http.createServer(common.mustCall((req, res) => {
|
||||
|
||||
server.listen(0, '127.0.0.1', common.mustCall(() => {
|
||||
const req = http.request({
|
||||
agent: false,
|
||||
port: server.address().port,
|
||||
host: '127.0.0.1',
|
||||
method: 'GET',
|
||||
|
||||
@@ -48,6 +48,7 @@ const server = http.createServer(common.mustCall((req, res) => {
|
||||
|
||||
server.listen(0, function() {
|
||||
http.get({
|
||||
agent: false,
|
||||
host: 'localhost',
|
||||
port: this.address().port,
|
||||
path: '/',
|
||||
|
||||
@@ -100,6 +100,7 @@ const headers = []
|
||||
|
||||
server.listen(0, function() {
|
||||
http.get({
|
||||
agent: false,
|
||||
host: 'localhost',
|
||||
port: this.address().port,
|
||||
path: '/',
|
||||
|
||||
@@ -41,6 +41,7 @@ const server = http.createServer(function(request, response) {
|
||||
server.listen(0, function() {
|
||||
const testURL =
|
||||
url.parse(`http://asdf:qwer@localhost:${this.address().port}`);
|
||||
testURL.agent = false;
|
||||
// The test here is if you set a specific authorization header in the
|
||||
// request we should not override that with basic auth
|
||||
testURL.headers = {
|
||||
|
||||
@@ -42,6 +42,7 @@ server.listen(0, function() {
|
||||
const port = this.address().port;
|
||||
// username = "user", password = "pass:"
|
||||
const testURL = url.parse(`http://user:pass%3A@localhost:${port}`);
|
||||
testURL.agent = false;
|
||||
|
||||
// make the request
|
||||
http.request(testURL).end();
|
||||
|
||||
@@ -43,7 +43,6 @@ const server = http.createServer(function(request, response) {
|
||||
check(request);
|
||||
response.writeHead(200, {});
|
||||
response.end('ok');
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.listen(0, common.mustCall(function() {
|
||||
@@ -54,5 +53,9 @@ server.listen(0, common.mustCall(function() {
|
||||
// Since there is a little magic with the agent
|
||||
// make sure that an http request uses the http.Agent
|
||||
assert.ok(clientRequest.agent instanceof http.Agent);
|
||||
clientRequest.on('response', common.mustCall((response) => {
|
||||
response.on('end', common.mustCall(() => server.close()));
|
||||
response.resume();
|
||||
}));
|
||||
clientRequest.end();
|
||||
}));
|
||||
@@ -45,7 +45,6 @@ const server = https.createServer(httpsOptions, function(request, response) {
|
||||
check(request);
|
||||
response.writeHead(200, {});
|
||||
response.end('ok');
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.listen(0, common.mustCall(function() {
|
||||
@@ -57,5 +56,9 @@ server.listen(0, common.mustCall(function() {
|
||||
// Since there is a little magic with the agent
|
||||
// make sure that the request uses the https.Agent
|
||||
assert.ok(clientRequest.agent instanceof https.Agent);
|
||||
clientRequest.on('response', common.mustCall((response) => {
|
||||
response.on('end', common.mustCall(() => server.close()));
|
||||
response.resume();
|
||||
}));
|
||||
clientRequest.end();
|
||||
}));
|
||||
@@ -40,6 +40,7 @@ const server = http.createServer(function(request, response) {
|
||||
|
||||
server.listen(0, function() {
|
||||
const testURL = url.parse(`http://localhost:${this.address().port}/asdf`);
|
||||
testURL.agent = false;
|
||||
|
||||
// make the request
|
||||
http.request(testURL).end();
|
||||
|
||||
@@ -47,6 +47,7 @@ const server = http.createServer(function(request, response) {
|
||||
|
||||
server.listen(0, function() {
|
||||
testURL = url.parse(`http://localhost:${this.address().port}/asdf?qwer=zxcv`);
|
||||
testURL.agent = false;
|
||||
testURL.method = 'POST';
|
||||
|
||||
// make the request
|
||||
|
||||
@@ -41,6 +41,7 @@ const server = http.createServer(function(request, response) {
|
||||
server.listen(0, function() {
|
||||
const port = this.address().port;
|
||||
const testURL = url.parse(`http://localhost:${port}/asdf?qwer=zxcv`);
|
||||
testURL.agent = false;
|
||||
|
||||
// make the request
|
||||
http.request(testURL).end();
|
||||
|
||||
@@ -71,6 +71,7 @@ server.on('checkContinue', common.mustCall((req, res) => {
|
||||
|
||||
server.listen(0, common.mustCall(function() {
|
||||
const req = http.request({
|
||||
agent: false,
|
||||
port: this.address().port,
|
||||
method: 'PUT',
|
||||
headers: { 'expect': '100-continue' }
|
||||
|
||||
@@ -39,7 +39,7 @@ const server = http.createServer(function(request, response) {
|
||||
});
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
http.get({ port: server.address().port }, common.mustCall((res) => {
|
||||
http.get({ port: server.address().port, agent: false }, common.mustCall((res) => {
|
||||
let response = '';
|
||||
|
||||
assert.strictEqual(res.statusCode, 200);
|
||||
|
||||
@@ -75,7 +75,7 @@ const server = http.createServer(common.mustCall((req, res) => {
|
||||
}));
|
||||
|
||||
server.listen(0, common.mustCall(function() {
|
||||
const req = http.request({ port: this.address().port, method: 'POST' });
|
||||
const req = http.request({ port: this.address().port, method: 'POST', agent: false });
|
||||
let actual = '';
|
||||
req.on('response', common.mustCall((res) => {
|
||||
res.setEncoding('utf8');
|
||||
|
||||
@@ -45,6 +45,7 @@ const server = https.createServer(options, function(req, res) {
|
||||
server.listen(0, common.mustCall(function() {
|
||||
let resumed = false;
|
||||
const req = https.request({
|
||||
agent: false,
|
||||
method: 'POST',
|
||||
port: this.address().port,
|
||||
rejectUnauthorized: false
|
||||
|
||||
@@ -32,6 +32,7 @@ const options = {
|
||||
'https://example.com/testpath',
|
||||
|
||||
{
|
||||
agent: false,
|
||||
hostname: 'localhost',
|
||||
port: server.address().port,
|
||||
rejectUnauthorized: false
|
||||
|
||||
@@ -47,7 +47,7 @@ function httpsTest() {
|
||||
});
|
||||
|
||||
server.listen(0, function() {
|
||||
const opts = { port: this.address().port, rejectUnauthorized: false };
|
||||
const opts = { port: this.address().port, rejectUnauthorized: false, agent: false };
|
||||
https.get(opts).on('response', function(res) {
|
||||
test(res);
|
||||
});
|
||||
|
||||
@@ -21,6 +21,7 @@ const server = https.createServer(options, common.mustCall((req, res) => {
|
||||
|
||||
server.listen(common.PIPE, common.mustCall(() => {
|
||||
https.get({
|
||||
agent: false,
|
||||
socketPath: common.PIPE,
|
||||
rejectUnauthorized: false
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user