Skip to content

Commit 80cccce

Browse files
joyeecheungFishrock123
authored andcommitted
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: #10021 Reviewed-By: James M Snell <[email protected]>
1 parent 1f11deb commit 80cccce

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/internal/url.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,9 @@ Object.defineProperty(URLSearchParamsIteratorPrototype, Symbol.toStringTag, {
781781
configurable: true
782782
});
783783

784-
URL.originFor = function(url) {
784+
URL.originFor = function(url, base) {
785785
if (!(url instanceof URL))
786-
url = new URL(url);
786+
url = new URL(url, base);
787787
var origin;
788788
const protocol = url.protocol;
789789
switch (protocol) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const URL = require('url').URL;
6+
const path = require('path');
7+
const assert = require('assert');
8+
const tests = require(path.join(common.fixturesDir, 'url-tests.json'));
9+
10+
for (const test of tests) {
11+
if (typeof test === 'string')
12+
continue;
13+
14+
if (test.origin) {
15+
const origin = URL.originFor(test.input, test.base);
16+
// Pass true to origin.toString() to enable unicode serialization.
17+
assert.strictEqual(origin.toString(true), test.origin);
18+
}
19+
}

0 commit comments

Comments
 (0)