From 80cccce21884fa10af0f7c9be8147b15fd6dd512 Mon Sep 17 00:00:00 2001 From: joyeecheung Date: Thu, 1 Dec 2016 11:32:31 -0600 Subject: [PATCH] url, test: including base argument in originFor - Add tests to check if the `originFor` implementation for WHATWG url parsing is correnct. - Fix `originFor` by including a base as argument PR-URL: https://github.com/nodejs/node/pull/10021 Reviewed-By: James M Snell --- lib/internal/url.js | 4 ++-- test/parallel/test-whatwg-url-origin-for.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-whatwg-url-origin-for.js diff --git a/lib/internal/url.js b/lib/internal/url.js index 79a40f90116f3e..acec7b8f6a324d 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -781,9 +781,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 00000000000000..a82f624e4e3392 --- /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); + } +}