Browse Source

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 <jasnell@gmail.com>
v6
joyeecheung 8 years ago
committed by Italo A. Casas
parent
commit
a84017a689
  1. 4
      lib/internal/url.js
  2. 19
      test/parallel/test-whatwg-url-origin-for.js

4
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) {

19
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);
}
}
Loading…
Cancel
Save