diff --git a/src/parse-url.ts b/src/parse-url.ts index 4f15cd6..29954fe 100644 --- a/src/parse-url.ts +++ b/src/parse-url.ts @@ -29,13 +29,18 @@ export default function parseURL(url: string) { pathname = '/' + pathname; // IE: prepend leading slash } - var host = parsedUrl.host; - if (parsedUrl.port === '80' || parsedUrl.port === '443') { - host = parsedUrl.hostname; // IE: remove default port + var hostname = parsedUrl.hostname; + if (hostname === 'localhost') { + hostname = '127.0.0.1'; + } + + var port = ''; + if (parsedUrl.port && parsedUrl.port !== '80' && parsedUrl.port !== '443') { + port = ':' + parsedUrl.port; // IE: include only non-default ports } return { - host: host, + host: hostname + port, protocol: parsedUrl.protocol, search: parsedUrl.query, hash: parsedUrl.hash, diff --git a/test/url_parsing_test.js b/test/url_parsing_test.js index 7517385..59cfabc 100644 --- a/test/url_parsing_test.js +++ b/test/url_parsing_test.js @@ -48,7 +48,7 @@ describe('parseURL', function() { testUrl('/mock/my/request?test=abc#def', { protocol: 'http:', pathname: '/mock/my/request', - host: window.location.host, + host: '127.0.0.1:9876', search: '?test=abc', hash: '#def', fullpath: '/mock/my/request?test=abc#def', @@ -64,7 +64,7 @@ describe('parseURL', function() { { protocol: window.location.protocol, pathname: '/mock/my/request', - host: window.location.host, + host: '127.0.0.1:9876', search: '?test=abc', hash: '#def', fullpath: '/mock/my/request?test=abc#def', @@ -82,4 +82,15 @@ describe('parseURL', function() { fullpath: '/mock/my/request?test=abc', }); }); + + describe('localhost HTTP URLs', function() { + testUrl('https://localhost:8080/mock/my/request?test=abc', { + protocol: 'https:', + pathname: '/mock/my/request', + host: '127.0.0.1:8080', + search: '?test=abc', + hash: '', + fullpath: '/mock/my/request?test=abc', + }); + }); });