From d13fc6ed689ab9231ef61617600fd3e325982f87 Mon Sep 17 00:00:00 2001
From: ZiJian Liu <Lxxyxzj@gmail.com>
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 <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>
---
 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 22dc145b85e0c7..32e298b412c555 100644
--- a/lib/internal/url.js
+++ b/lib/internal/url.js
@@ -416,7 +416,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}`;
       } else if (ctx.scheme === 'file:') {
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(), '');
+}