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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Backport-PR-URL: #21880 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 37369eb commit 2bf9a4a

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

‎lib/https.js‎

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

256256
constglobalAgent=newAgent();
257257

258-
functionrequest(options,cb){
259-
if(typeofoptions==='string'){
260-
options=url.parse(options);
258+
functionrequest(...args){
259+
letoptions={};
260+
261+
if(typeofargs[0]==='string'){
262+
consturlStr=args.shift();
263+
options=url.parse(urlStr);
261264
if(!options.hostname){
262265
thrownewERR_INVALID_DOMAIN_NAME();
263266
}
264-
}elseif(options&&options[searchParamsSymbol]&&
265-
options[searchParamsSymbol][searchParamsSymbol]){
267+
}elseif(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]&&typeofargs[0]!=='function'){
274+
options=util._extend(options,args.shift());
270275
}
276+
271277
options._defaultAgent=globalAgent;
272-
returnnewClientRequest(options,cb);
278+
args.unshift(options);
279+
280+
returnnewClientRequest(...args);
273281
}
274282

275-
functionget(options,cb){
276-
constreq=request(options,cb);
283+
functionget(input,options,cb){
284+
constreq=request(input,options,cb);
277285
req.end();
278286
returnreq;
279287
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
consthttps=require('https');
5+
constfixtures=require('../common/fixtures');
6+
7+
if(!common.hasCrypto)
8+
common.skip('missing crypto');
9+
10+
constoptions={
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+
constserver=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)