Skip to content

Commit 2bf9a4a

Browse files
rubystargos
authored andcommitted
https: allow url and options to be passed to https.request
PR-URL: #22003 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Backport-PR-URL: #21880 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 37369eb commit 2bf9a4a

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

lib/https.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,33 @@ Agent.prototype._evictSession = function _evictSession(key) {
255255

256256
const globalAgent = new Agent();
257257

258-
function request(options, cb) {
259-
if (typeof options === 'string') {
260-
options = url.parse(options);
258+
function request(...args) {
259+
let options = {};
260+
261+
if (typeof args[0] === 'string') {
262+
const urlStr = args.shift();
263+
options = url.parse(urlStr);
261264
if (!options.hostname) {
262265
throw new ERR_INVALID_DOMAIN_NAME();
263266
}
264-
} else if (options && options[searchParamsSymbol] &&
265-
options[searchParamsSymbol][searchParamsSymbol]) {
267+
} else if (args[0] && args[0][searchParamsSymbol] &&
268+
args[0][searchParamsSymbol][searchParamsSymbol]) {
266269
// url.URL instance
267-
options = urlToOptions(options);
268-
} else {
269-
options = util._extend({}, options);
270+
options = urlToOptions(args.shift());
271+
}
272+
273+
if (args[0] && typeof args[0] !== 'function') {
274+
options = util._extend(options, args.shift());
270275
}
276+
271277
options._defaultAgent = globalAgent;
272-
return new ClientRequest(options, cb);
278+
args.unshift(options);
279+
280+
return new ClientRequest(...args);
273281
}
274282

275-
function get(options, cb) {
276-
const req = request(options, cb);
283+
function get(input, options, cb) {
284+
const req = request(input, options, cb);
277285
req.end();
278286
return req;
279287
}
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)