Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[fix] Handle the case where the port is specified but empty
  • Loading branch information
lpinca committed Feb 19, 2022
1 parent 4f2ae67 commit d5c6479
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
13 changes: 10 additions & 3 deletions index.js
Expand Up @@ -39,7 +39,7 @@ var rules = [
['/', 'pathname'], // Extract from the back.
['@', 'auth', 1], // Extract from the front.
[NaN, 'host', undefined, 1, 1], // Set left over value.
[/:(\d+)$/, 'port', undefined, 1], // RegExp the back.
[/:(\d*)$/, 'port', undefined, 1], // RegExp the back.
[NaN, 'hostname', undefined, 1, 1] // Set left over.
];

Expand Down Expand Up @@ -524,6 +524,7 @@ function toString(stringify) {

var query
, url = this
, host = url.host
, protocol = url.protocol;

if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';
Expand All @@ -542,7 +543,7 @@ function toString(stringify) {
} else if (
url.protocol !== 'file:' &&
isSpecial(url.protocol) &&
!url.host &&
!host &&
url.pathname !== '/'
) {
//
Expand All @@ -552,7 +553,13 @@ function toString(stringify) {
result += '@';
}

result += url.host + url.pathname;
//
// Trailing colon is removed from `url.host` when it is parsed. If it still
// ends with a colon, then add back the trailing colon that was removed. This
// prevents an invalid URL from being transformed into a valid one.
//
if (host[host.length - 1] === ':') host += ':';
result += host + url.pathname;

query = 'object' === typeof url.query ? stringify(url.query) : url.query;
if (query) result += '?' !== query.charAt(0) ? '?'+ query : query;
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Expand Up @@ -442,6 +442,28 @@ describe('url-parse', function () {
assume(parsed.href).equals('sip:alice@atlanta.com');
});

it('handles the case where the port is specified but empty', function () {
var parsed = parse('http://example.com:');

assume(parsed.protocol).equals('http:');
assume(parsed.port).equals('');
assume(parsed.host).equals('example.com');
assume(parsed.hostname).equals('example.com');
assume(parsed.pathname).equals('/');
assume(parsed.origin).equals('http://example.com');
assume(parsed.href).equals('http://example.com/');

parsed = parse('http://example.com::');

assume(parsed.protocol).equals('http:');
assume(parsed.port).equals('');
assume(parsed.host).equals('example.com:');
assume(parsed.hostname).equals('example.com:');
assume(parsed.pathname).equals('/');
assume(parsed.origin).equals('http://example.com:');
assume(parsed.href).equals('http://example.com::/');
});

describe('origin', function () {
it('generates an origin property', function () {
var url = 'http://google.com:80/pathname'
Expand Down

0 comments on commit d5c6479

Please sign in to comment.