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(), ''); +}