From 95660832d158d6f2fb9858b8290125afeefd484e Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Wed, 13 Jan 2021 16:17:16 +0800 Subject: [PATCH] url: align url format behavior with browsers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/36887 PR-URL: https://github.com/nodejs/node/pull/36903 Reviewed-By: Michaël Zasso Reviewed-By: Yongsheng Zhang Reviewed-By: Juan José Arboleda Reviewed-By: Anna Henningsen Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Daijiro Wachi --- lib/internal/url.js | 2 +- .../test-whatwg-url-override-hostname.js | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-whatwg-url-override-hostname.js diff --git a/lib/internal/url.js b/lib/internal/url.js index 519ebbb0c8dd38..3c90ec3d3672a8 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -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}`; } diff --git a/test/parallel/test-whatwg-url-override-hostname.js b/test/parallel/test-whatwg-url-override-hostname.js new file mode 100644 index 00000000000000..61e3412c6b7b53 --- /dev/null +++ b/test/parallel/test-whatwg-url-override-hostname.js @@ -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(), ''); +}