diff --git a/index.js b/index.js index 84bfe51..b441c69 100644 --- a/index.js +++ b/index.js @@ -23,8 +23,9 @@ http.request = function (opts, cb) { var path = opts.path || '/' // Necessary for IPv6 addresses - if (host && host.indexOf(':') !== -1) - host = '[' + host + ']' + if (host && host === opts.hostname && host.indexOf(':') !== -1 && host.indexOf('[') !== 0) { + host = '[' + host + ']' + } // This may be a relative url. The browser should always be able to interpret it correctly. opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path @@ -82,4 +83,4 @@ http.METHODS = [ 'TRACE', 'UNLOCK', 'UNSUBSCRIBE' -] \ No newline at end of file +] diff --git a/test/node/http-browserify.js b/test/node/http-browserify.js index 42a718f..f2483be 100644 --- a/test/node/http-browserify.js +++ b/test/node/http-browserify.js @@ -121,7 +121,7 @@ test('Test withCredentials param', function(t) { t.end() }) -test('Test ipv6 address', function(t) { +test('Test ipv6 address as string', function(t) { var testUrl = 'http://[::1]:80/foo' var request = http.get(testUrl, noop) @@ -130,6 +130,47 @@ test('Test ipv6 address', function(t) { t.end() }) +test('Test ipv6 address as opts with hostname [brackets] and port', function(t) { + var opts = { + protocol: "http:", + hostname: "[::1]", + port: "80", + path: "/foo" + } + var request = http.get(opts, noop) + + var resolved = url.resolve(location, request._opts.url) + t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct') + t.end() +}) + +test('Test ipv6 address as opts with hostname [no brackets] and port', function(t) { + var opts = { + protocol: "http:", + hostname: "::1", + port: "80", + path: "/foo" + } + var request = http.get(opts, noop) + + var resolved = url.resolve(location, request._opts.url) + t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct') + t.end() +}) + +test('Test ipv6 address as opts with host', function(t) { + var opts = { + protocol: "http:", + host: "[::1]:80", + path: "/foo" + } + var request = http.get(opts, noop) + + var resolved = url.resolve(location, request._opts.url) + t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct') + t.end() +}) + test('Test relative path in url', function(t) { var params = { path: './bar' } var request = http.get(params, noop)