Skip to content

Commit 25cd753

Browse files
committed
http: throw an error for unexpected agent values
As per #9069 unexpected things can happen when supplying an unexpected value to agent. Beings as the docs clearly state the expected values, this throws an error on an unexpected value. Signed-off-by: brad-decker <[email protected]>
1 parent d86ff5f commit 25cd753

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

lib/_http_client.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ function ClientRequest(options, cb) {
6262
var defaultAgent = options._defaultAgent || Agent.globalAgent;
6363
if (agent === false) {
6464
agent = new defaultAgent.constructor();
65-
} else if ((agent === null || agent === undefined) &&
66-
typeof options.createConnection !== 'function') {
67-
agent = defaultAgent;
65+
} else if (agent === null || agent === undefined) {
66+
if (typeof options.createConnection !== 'function') {
67+
agent = defaultAgent;
68+
}
69+
// Explicitly pass through this statement as agent will not be used
70+
// when createConnection is provided.
71+
} else if (!(agent instanceof Agent.Agent)) {
72+
throw new TypeError(
73+
'Agent option must be an instance of http.Agent, undefined or false.'
74+
);
6875
}
6976
self.agent = agent;
7077

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const baseOptions = {
7+
method: 'GET',
8+
port: undefined,
9+
host: common.localhostIPv4,
10+
};
11+
12+
const failingAgentOptions = [
13+
true,
14+
'agent',
15+
{},
16+
1,
17+
() => null,
18+
Symbol(),
19+
];
20+
21+
const acceptableAgentOptions = [
22+
false,
23+
undefined,
24+
null,
25+
new http.Agent(),
26+
];
27+
28+
const server = http.createServer((req, res) => {
29+
res.end('hello');
30+
});
31+
32+
let numberOfResponses = 0;
33+
34+
function createRequest(agent) {
35+
const options = Object.assign(baseOptions, {agent});
36+
const request = http.request(options);
37+
request.end();
38+
request.on('response', common.mustCall(() => {
39+
numberOfResponses++;
40+
if (numberOfResponses === acceptableAgentOptions.length) {
41+
server.close();
42+
}
43+
}));
44+
}
45+
46+
server.listen(0, baseOptions.host, common.mustCall(function() {
47+
baseOptions.port = this.address().port;
48+
49+
failingAgentOptions.forEach((agent) => {
50+
assert.throws(
51+
() => createRequest(agent),
52+
/^TypeError: Agent option must be an instance of http.Agent/,
53+
`Expected typeof agent: ${typeof agent} to be rejected`
54+
);
55+
});
56+
57+
acceptableAgentOptions.forEach((agent) => {
58+
assert.doesNotThrow(() => createRequest(agent));
59+
});
60+
}));
61+
62+
process.on('exit', () => {
63+
assert.strictEqual(numberOfResponses, acceptableAgentOptions.length);
64+
});

0 commit comments

Comments
 (0)