-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: support overriding http\s.globalAgent
Overriding `require('http[s]').globalAgent` is now respected by consequent requests. In order to achieve that, the following changes were made: 1. Implmentation in `http`: `module.exports.globalAgent` is now defined through `Object.defineProperty`. Its getter and setter return \ set `require('_http_agent').globalAgent`. 2. Implementation in `https`: the https `globalAgent` is not the same as `_http_agent`, and is defined in `https` module itself. Therefore, the fix here was to simply use `module.exports.globalAgent` to support mutation. 3. According tests were added for both `http` and `https`, where in both we create a server, set the default agent to a newly created instance and make a request to that server. We then assert that the given instance was actually used by inspecting its sockets property. Fixes: #23281 PR-URL: #25170 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
4 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.Server(common.mustCall((req, res) => { | ||
res.writeHead(200); | ||
res.end('Hello, World!'); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const agent = new http.Agent(); | ||
const name = agent.getName({ port: server.address().port }); | ||
http.globalAgent = agent; | ||
|
||
makeRequest(); | ||
assert(agent.sockets.hasOwnProperty(name)); // agent has indeed been used | ||
})); | ||
|
||
function makeRequest() { | ||
const req = http.get({ | ||
port: server.address().port | ||
}); | ||
req.on('close', () => | ||
server.close()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('assert'); | ||
const https = require('https'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
// Disable strict server certificate validation by the client | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | ||
|
||
const options = { | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem') | ||
}; | ||
|
||
const server = https.Server(options, common.mustCall((req, res) => { | ||
res.writeHead(200); | ||
res.end('Hello, World!'); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const agent = new https.Agent(); | ||
const name = agent.getName({ port: server.address().port }); | ||
https.globalAgent = agent; | ||
|
||
makeRequest(); | ||
assert(agent.sockets.hasOwnProperty(name)); // agent has indeed been used | ||
})); | ||
|
||
function makeRequest() { | ||
const req = https.get({ | ||
port: server.address().port | ||
}); | ||
req.on('close', () => | ||
server.close()); | ||
} |