url: align url format behavior with browsers

Fixes: https://github.com/nodejs/node/issues/36887

PR-URL: https://github.com/nodejs/node/pull/36903
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
This commit is contained in:
ZiJian Liu authored and Antoine du Hamel committed 2021-01-18 11:50:04 +01:00
1 parent 146f0fc495
commit 95660832d1
2 files changed
+21 -1

No files matched your search

+1 -1
View File
@@ -437,7 +437,7 @@ ObjectDefineProperties(URL.prototype, {
ret += '@';
}
ret += options.unicode ?
domainToUnicode(this.hostname) : this.hostname;
domainToUnicode(ctx.host) : ctx.host;
if (ctx.port !== null)
ret += `:${ctx.port}`;
}
@@ -0,0 +1,20 @@
'use strict';
require('../common');
const assert = require('assert');
{
const url = new (class extends URL { get hostname() { return 'bar.com'; } })('http://foo.com/');
assert.strictEqual(url.href, 'http://foo.com/');
assert.strictEqual(url.toString(), 'http://foo.com/');
assert.strictEqual(url.toJSON(), 'http://foo.com/');
assert.strictEqual(url.hash, '');
assert.strictEqual(url.host, 'foo.com');
assert.strictEqual(url.hostname, 'bar.com');
assert.strictEqual(url.origin, 'http://foo.com');
assert.strictEqual(url.password, '');
assert.strictEqual(url.protocol, 'http:');
assert.strictEqual(url.username, '');
assert.strictEqual(url.search, '');
assert.strictEqual(url.searchParams.toString(), '');
}