From 6cc1820d95fb46f495074f011dc5fa035bf0133a Mon Sep 17 00:00:00 2001
From: Luigi Pinca <luigipinca@gmail.com>
Date: Sun, 18 Mar 2018 10:59:44 +0100
Subject: [PATCH] http: make socketPath work with no agent

Currently `Agent.prototype.createConnection()` is called uncoditionally
if the `socketPath` option is used. This throws an error if no agent is
used, preventing, for example, the `socketPath` and `createConnection`
options to be used together.

This commit fixes the issue by falling back to the `createConnection`
option or `net.createConnection()`.
---
 lib/_http_client.js                           |  6 +++-
 .../test-http-client-unix-socket-no-agent.js  | 28 +++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 test/parallel/test-http-client-unix-socket-no-agent.js

diff --git a/lib/_http_client.js b/lib/_http_client.js
index 9d2057814133b7..ea181cff31a482 100644
--- a/lib/_http_client.js
+++ b/lib/_http_client.js
@@ -138,7 +138,11 @@ function ClientRequest(options, cb) {
       timeout: self.timeout,
       rejectUnauthorized: !!options.rejectUnauthorized
     };
-    const newSocket = self.agent.createConnection(optionsPath, oncreate);
+    const newSocket = self.agent
+      ? self.agent.createConnection(optionsPath, oncreate)
+      : typeof options.createConnection === 'function'
+        ? options.createConnection(optionsPath, oncreate)
+        : net.createConnection(optionsPath);
     if (newSocket && !called) {
       called = true;
       self.onSocket(newSocket);
diff --git a/test/parallel/test-http-client-unix-socket-no-agent.js b/test/parallel/test-http-client-unix-socket-no-agent.js
new file mode 100644
index 00000000000000..9dd11403c6bf79
--- /dev/null
+++ b/test/parallel/test-http-client-unix-socket-no-agent.js
@@ -0,0 +1,28 @@
+'use strict';
+const common = require('../common');
+const Countdown = require('../common/countdown');
+
+const http = require('http');
+const { createConnection } = require('net');
+
+const server = http.createServer((req, res) => {
+  res.end();
+});
+
+const countdown = new Countdown(2, () => {
+  server.close();
+});
+
+common.refreshTmpDir();
+
+server.listen(common.PIPE, common.mustCall(() => {
+  http.get({ createConnection, socketPath: common.PIPE }, onResponse);
+  http.get({ agent: 0, socketPath: common.PIPE }, onResponse);
+}));
+
+function onResponse(res) {
+  res.on('end', () => {
+    countdown.dec();
+  });
+  res.resume();
+}