Skip to content

Commit 19aa41c

Browse files
rubysTrott
authored andcommitted
https: allow url and options to be passed to https.request
PR-URL: nodejs#22003 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ede279c commit 19aa41c

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

lib/https.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,11 @@ Agent.prototype._evictSession = function _evictSession(key) {
255255
const globalAgent = new Agent();
256256

257257
let urlWarningEmitted = false;
258-
function request(options, cb) {
259-
if (typeof options === 'string') {
260-
const urlStr = options;
258+
function request(...args) {
259+
let options = {};
260+
261+
if (typeof args[0] === 'string') {
262+
const urlStr = args.shift();
261263
try {
262264
options = urlToOptions(new URL(urlStr));
263265
} catch (err) {
@@ -273,19 +275,24 @@ function request(options, cb) {
273275
'DeprecationWarning', 'DEP0109');
274276
}
275277
}
276-
} else if (options && options[searchParamsSymbol] &&
277-
options[searchParamsSymbol][searchParamsSymbol]) {
278+
} else if (args[0] && args[0][searchParamsSymbol] &&
279+
args[0][searchParamsSymbol][searchParamsSymbol]) {
278280
// url.URL instance
279-
options = urlToOptions(options);
280-
} else {
281-
options = util._extend({}, options);
281+
options = urlToOptions(args.shift());
282+
}
283+
284+
if (args[0] && typeof args[0] !== 'function') {
285+
options = util._extend(options, args.shift());
282286
}
287+
283288
options._defaultAgent = globalAgent;
284-
return new ClientRequest(options, cb);
289+
args.unshift(options);
290+
291+
return new ClientRequest(...args);
285292
}
286293

287-
function get(options, cb) {
288-
const req = request(options, cb);
294+
function get(input, options, cb) {
295+
const req = request(input, options, cb);
289296
req.end();
290297
return req;
291298
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const https = require('https');
5+
const fixtures = require('../common/fixtures');
6+
7+
if (!common.hasCrypto)
8+
common.skip('missing crypto');
9+
10+
const options = {
11+
key: fixtures.readKey('agent1-key.pem'),
12+
cert: fixtures.readKey('agent1-cert.pem'),
13+
ca: fixtures.readKey('ca1-cert.pem')
14+
};
15+
16+
// Test providing both a url and options, with the options partially
17+
// replacing address and port portions of the URL provided.
18+
{
19+
const server = https.createServer(
20+
options,
21+
common.mustCall((req, res) => {
22+
assert.strictEqual(req.url, '/testpath');
23+
res.end();
24+
server.close();
25+
})
26+
);
27+
28+
server.listen(
29+
0,
30+
common.mustCall(() => {
31+
https.get(
32+
'https://example.com/testpath',
33+
34+
{
35+
hostname: 'localhost',
36+
port: server.address().port,
37+
rejectUnauthorized: false
38+
},
39+
40+
common.mustCall((res) => {
41+
res.resume();
42+
})
43+
);
44+
})
45+
);
46+
}

0 commit comments

Comments
 (0)