From 4c1db665219617bc2eb19e4a32289ee6cd37a5c8 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Fri, 7 Nov 2014 18:48:11 -0700 Subject: [PATCH] allow specifying the hostname as the host, eg. host: 'localhost' --- src/lib/host.js | 7 ++++--- test/unit/specs/host.js | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/host.js b/src/lib/host.js index 921f6e212..8d95779c0 100644 --- a/src/lib/host.js +++ b/src/lib/host.js @@ -47,9 +47,10 @@ function Host(config, globalConfig) { if (typeof config === 'string') { var firstColon = config.indexOf(':'); var firstSlash = config.indexOf('/'); - var portWithPath = firstColon < firstSlash; - var portNoPath = firstColon > -1 && firstSlash === -1; - if ((portWithPath || portNoPath) && !startsWithProtocolRE.test(config)) { + var noSlash = firstSlash === -1; + var portNoPath = firstColon > -1 && noSlash; + var portWithPath = !portNoPath && firstColon < firstSlash; + if ((noSlash || portNoPath || portWithPath) && !startsWithProtocolRE.test(config)) { config = defaultProto + '//' + config; } config = _.pick(url.parse(config, false, true), urlParseFields); diff --git a/test/unit/specs/host.js b/test/unit/specs/host.js index 28db76a8c..395123234 100644 --- a/test/unit/specs/host.js +++ b/test/unit/specs/host.js @@ -71,8 +71,9 @@ describe('Host class', function () { it('parses simple urls properly', function () { var host; - host = new Host('elasticsearch'); - expect(host.path).to.be('/elasticsearch'); + host = new Host('localhost'); + expect(host.host).to.be('localhost'); + expect(host.path).to.be(''); host = new Host('/elasticsearch'); expect(host.path).to.be('/elasticsearch');