diff --git a/lib/_http_client.js b/lib/_http_client.js index cb50dab1a1caa9..f9fd3700c3d19d 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -336,7 +336,16 @@ function ClientRequest(input, options, cb) { // No agent, default to Connection:close. this._last = true; this.shouldKeepAlive = false; - if (typeof optsWithoutSignal.createConnection === 'function') { + let opts = optsWithoutSignal; + if (opts.path || opts.socketPath) { + opts = { ...optsWithoutSignal }; + if (opts.socketPath) { + opts.path = opts.socketPath; + } else if (opts.path) { + opts.path = undefined; + } + } + if (typeof opts.createConnection === 'function') { const oncreate = once((err, socket) => { if (err) { process.nextTick(() => this.emit('error', err)); @@ -346,8 +355,7 @@ function ClientRequest(input, options, cb) { }); try { - const newSocket = optsWithoutSignal.createConnection(optsWithoutSignal, - oncreate); + const newSocket = opts.createConnection(opts, oncreate); if (newSocket) { oncreate(null, newSocket); } @@ -355,8 +363,8 @@ function ClientRequest(input, options, cb) { oncreate(err); } } else { - debug('CLIENT use net.createConnection', optsWithoutSignal); - this.onSocket(net.createConnection(optsWithoutSignal)); + debug('CLIENT use net.createConnection', opts); + this.onSocket(net.createConnection(opts)); } } } diff --git a/test/parallel/test-http-client-with-create-connection.js b/test/parallel/test-http-client-with-create-connection.js new file mode 100644 index 00000000000000..5c99de6c496ba5 --- /dev/null +++ b/test/parallel/test-http-client-with-create-connection.js @@ -0,0 +1,55 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); +const net = require('net'); +const tmpdir = require('../common/tmpdir'); + +tmpdir.refresh(); + +let count = 0; +let server1; +let server2; + +function request(options) { + count++; + http.get({ + ...options, + createConnection: (...args) => { + return net.connect(...args); + } + }, (res) => { + res.resume(); + res.on('end', () => { + if (--count === 0) { + server1.close(); + server2.close(); + } + }); + }); +} + +server1 = http.createServer((req, res) => { + res.end('ok'); +}).listen(common.PIPE, () => { + server2 = http.createServer((req, res) => { + res.end('ok'); + }).listen(() => { + request({ + path: '/', + socketPath: common.PIPE, + }); + + request({ + socketPath: common.PIPE, + }); + + request({ + path: '/', + port: server2.address().port, + }); + + request({ + port: server2.address().port, + }); + }); +});