diff --git a/lib/internal/url.js b/lib/internal/url.js index 1129060b09..d8053dc2ad 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -790,9 +790,9 @@ Object.defineProperty(URLSearchParamsIteratorPrototype, Symbol.toStringTag, { configurable: true }); -URL.originFor = function(url) { +URL.originFor = function(url, base) { if (!(url instanceof URL)) - url = new URL(url); + url = new URL(url, base); var origin; const protocol = url.protocol; switch (protocol) { diff --git a/test/parallel/test-whatwg-url-origin-for.js b/test/parallel/test-whatwg-url-origin-for.js new file mode 100644 index 0000000000..a82f624e4e --- /dev/null +++ b/test/parallel/test-whatwg-url-origin-for.js @@ -0,0 +1,19 @@ +'use strict'; + +const common = require('../common'); + +const URL = require('url').URL; +const path = require('path'); +const assert = require('assert'); +const tests = require(path.join(common.fixturesDir, 'url-tests.json')); + +for (const test of tests) { + if (typeof test === 'string') + continue; + + if (test.origin) { + const origin = URL.originFor(test.input, test.base); + // Pass true to origin.toString() to enable unicode serialization. + assert.strictEqual(origin.toString(true), test.origin); + } +}