-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https: make https.globalAgent overridable also under ECMAScript Modules
Under ECMAScript modules when you do "import * as https from 'https'" you get a new object with properties copied from https module exports. So if this is a regular data property, you will just override a copy, but if this would be a accessor property, we can still access the actual https.globalAgent. Refs: #25170, #9386
- Loading branch information
Showing
2 changed files
with
55 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { hasCrypto, skip, mustCall } from '../common'; | ||
if (!hasCrypto) skip('missing crypto'); | ||
import { readKey } from '../common/fixtures'; | ||
import assert from 'assert'; | ||
// to override https.globalAgent we need to use namespace import | ||
import * as https from 'https'; | ||
|
||
// Disable strict server certificate validation by the client | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | ||
|
||
const options = { | ||
key: readKey('agent1-key.pem'), | ||
cert: readKey('agent1-cert.pem'), | ||
}; | ||
|
||
const server = https.Server( | ||
options, | ||
mustCall((req, res) => { | ||
res.writeHead(200); | ||
res.end('Hello, World!'); | ||
}) | ||
); | ||
|
||
server.listen( | ||
0, | ||
mustCall(() => { | ||
const agent = new https.Agent(); | ||
const name = agent.getName({ port: server.address().port }); | ||
https.globalAgent = agent; | ||
|
||
makeRequest(); | ||
assert(name in agent.sockets); // Agent has indeed been used | ||
}) | ||
); | ||
|
||
function makeRequest() { | ||
const req = https.get({ | ||
port: server.address().port, | ||
}); | ||
req.on('close', () => server.close()); | ||
} |