Skip to content

Commit a84017a

Browse files
joyeecheungItalo A. Casas
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 9f58e02 commit a84017a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/internal/url.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,9 @@ Object.defineProperty(URLSearchParamsIteratorPrototype, Symbol.toStringTag, {
790790
configurable: true
791791
});
792792

793-
URL.originFor = function(url) {
793+
URL.originFor = function(url, base) {
794794
if (!(url instanceof URL))
795-
url = new URL(url);
795+
url = new URL(url, base);
796796
var origin;
797797
const protocol = url.protocol;
798798
switch (protocol) {
Lines changed: 19 additions & 0 deletions
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)