Skip to content

Commit

Permalink
https: allow url and options to be passed to https.request
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rubys authored and targos committed Aug 7, 2018
1 parent 37369eb commit 2bf9a4a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/https.js
Expand Up @@ -255,25 +255,33 @@ Agent.prototype._evictSession = function _evictSession(key) {

const globalAgent = new Agent();

function request(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
function request(...args) {
let options = {};

if (typeof args[0] === 'string') {
const urlStr = args.shift();
options = url.parse(urlStr);
if (!options.hostname) {
throw new ERR_INVALID_DOMAIN_NAME();
}
} else if (options && options[searchParamsSymbol] &&
options[searchParamsSymbol][searchParamsSymbol]) {
} else if (args[0] && args[0][searchParamsSymbol] &&
args[0][searchParamsSymbol][searchParamsSymbol]) {
// url.URL instance
options = urlToOptions(options);
} else {
options = util._extend({}, options);
options = urlToOptions(args.shift());
}

if (args[0] && typeof args[0] !== 'function') {
options = util._extend(options, args.shift());
}

options._defaultAgent = globalAgent;
return new ClientRequest(options, cb);
args.unshift(options);

return new ClientRequest(...args);
}

function get(options, cb) {
const req = request(options, cb);
function get(input, options, cb) {
const req = request(input, options, cb);
req.end();
return req;
}
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-https-request-arguments.js
@@ -0,0 +1,46 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const https = require('https');
const fixtures = require('../common/fixtures');

if (!common.hasCrypto)
common.skip('missing crypto');

const options = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
ca: fixtures.readKey('ca1-cert.pem')
};

// Test providing both a url and options, with the options partially
// replacing address and port portions of the URL provided.
{
const server = https.createServer(
options,
common.mustCall((req, res) => {
assert.strictEqual(req.url, '/testpath');
res.end();
server.close();
})
);

server.listen(
0,
common.mustCall(() => {
https.get(
'https://example.com/testpath',

{
hostname: 'localhost',
port: server.address().port,
rejectUnauthorized: false
},

common.mustCall((res) => {
res.resume();
})
);
})
);
}

0 comments on commit 2bf9a4a

Please sign in to comment.