summaryrefslogtreecommitdiff
path: root/lib/http.js
diff options
context:
space:
mode:
authorRoy Sommer <roy@sommer.co.il>2018-12-21 20:17:15 +0200
committerAnna Henningsen <anna@addaleax.net>2019-01-09 00:51:20 +0100
commit8d0c638583742d1cb43e8c7b965ebaf6f51bf69a (patch)
tree835b17f6bd993e6871acc7a852f02adff87f9bdc /lib/http.js
parentce7bbd2ad9f76addd68fb7a1dd57a027f08b4fcb (diff)
downloadnode-new-8d0c638583742d1cb43e8c7b965ebaf6f51bf69a.tar.gz
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: https://github.com/nodejs/node/issues/23281 PR-URL: https://github.com/nodejs/node/pull/25170 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/http.js')
-rw-r--r--lib/http.js16
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/http.js b/lib/http.js
index e3707ffa62..5f2fe6dc68 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -21,7 +21,7 @@
'use strict';
-const { Agent, globalAgent } = require('_http_agent');
+const httpAgent = require('_http_agent');
const { ClientRequest } = require('_http_client');
const { methods } = require('_http_common');
const { IncomingMessage } = require('_http_incoming');
@@ -52,9 +52,8 @@ module.exports = {
_connectionListener,
METHODS: methods.slice().sort(),
STATUS_CODES,
- Agent,
+ Agent: httpAgent.Agent,
ClientRequest,
- globalAgent,
IncomingMessage,
OutgoingMessage,
Server,
@@ -76,3 +75,14 @@ Object.defineProperty(module.exports, 'maxHeaderSize', {
return maxHeaderSize;
}
});
+
+Object.defineProperty(module.exports, 'globalAgent', {
+ configurable: true,
+ enumerable: true,
+ get() {
+ return httpAgent.globalAgent;
+ },
+ set(value) {
+ httpAgent.globalAgent = value;
+ }
+});