-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
web-platform.js
227 lines (197 loc) · 5.92 KB
/
web-platform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const { URL, URLSearchParams } = require("..");
const DOMException = require("domexception");
const testharness = require("./testharness");
const parsingTestCases = require("./web-platform-tests/urltestdata.json");
const additionalParsingTestCases = require("./to-upstream.json");
const setterTestData = require("./web-platform-tests/setters_tests.json");
const toASCIITestCases = require("./web-platform-tests/toascii.json");
const wptDir = path.join(__dirname, "web-platform-tests");
function asyncTest() {
return {
step(cb) {
cb();
},
step_func(cb) { // eslint-disable-line camelcase
return cb;
},
done() {}
};
}
class FauxXMLHttpRequest {
constructor() {
this._path = undefined;
this.responseType = undefined;
this.response = undefined;
}
open(method, pathToOpen) {
this._path = pathToOpen;
}
send() {}
set onload(cb) {
assert(this.responseType === "json");
const buf = fs.readFileSync(path.resolve(__dirname, `web-platform-tests/${this._path}`), "utf8");
this.response = JSON.parse(buf);
cb();
}
}
function createSandbox() {
const sandbox = {
URL,
URLSearchParams,
// Shim for urlsearchparams-constructor.js
DOMException,
// Shim for url-constructor.js
async_test: asyncTest, // eslint-disable-line camelcase
XMLHttpRequest: FauxXMLHttpRequest
};
Object.assign(sandbox, testharness);
vm.createContext(sandbox);
return sandbox;
}
function runWPTFile(file) {
const code = fs.readFileSync(file, "utf8");
vm.runInContext(code, createSandbox(), {
filename: file,
displayErrors: true
});
}
function testURL(expected) {
return () => {
let url;
try {
url = new URL(expected.input, expected.base);
} catch (e) {
if (e instanceof TypeError && expected.failure) {
return;
}
throw e;
}
assert.equal(url.href, expected.href, "href");
if ("origin" in expected) {
assert.equal(url.origin, expected.origin, "origin");
}
assert.equal(url.protocol, expected.protocol, "protocol");
assert.equal(url.username, expected.username, "username");
assert.equal(url.password, expected.password, "password");
assert.equal(url.host, expected.host, "host");
assert.equal(url.hostname, expected.hostname, "hostname");
assert.equal(url.port, expected.port, "port");
assert.equal(url.pathname, expected.pathname, "pathname");
assert.equal(url.search, expected.search, "search");
assert.equal(url.hash, expected.hash, "hash");
};
}
function testSetterCase(testCase, propertyName) {
return () => {
const url = new URL(testCase.href);
url[propertyName] = testCase.new_value;
for (const expectedProperty in testCase.expected) {
assert.equal(url[expectedProperty], testCase.expected[expectedProperty]);
}
};
}
function testToASCII(testCase) {
return () => {
if (testCase.output !== null) {
const url = new URL(`https://${testCase.input}/x`);
assert.equal(url.host, testCase.output);
assert.equal(url.hostname, testCase.output);
assert.equal(url.pathname, "/x");
assert.equal(url.href, `https://${testCase.output}/x`);
const url2 = new URL("https://x/x");
url2.hostname = testCase.input;
assert.equal(url2.hostname, testCase.output);
const url3 = new URL("https://x/x");
url3.host = testCase.input;
assert.equal(url3.host, testCase.output);
} else {
assert.throws(() => new URL(`https://${testCase.input}/x`), TypeError);
const url2 = new URL("https://x/x");
url2.hostname = testCase.input;
assert.equal(url2.hostname, "x");
const url3 = new URL("https://x/x");
url3.host = testCase.input;
assert.equal(url3.host, "x");
}
};
}
describe("Web platform tests", () => {
describe("parsing", () => {
for (const expected of parsingTestCases) {
if (typeof expected === "string") {
// It's a "comment"; skip it.
continue;
}
test(`<${expected.input}> against <${expected.base}>`, testURL(expected));
}
});
describe("bad base URL", () => {
for (const rawExpected of parsingTestCases) {
if (typeof rawExpected === "string" || !rawExpected.failure) {
continue;
}
const expected = {
input: "about:blank",
base: rawExpected.input,
failure: true
};
test(`<${expected.input}> against <${expected.base}>`, testURL(expected));
}
});
describe("setters", () => {
for (const key of Object.keys(setterTestData)) {
if (key === "comment") {
continue;
}
describe(key, () => {
for (const testCase of setterTestData[key]) {
test(
`<${testCase.href}>.${key} = "${testCase.new_value}" ${testCase.comment || ""}`,
testSetterCase(testCase, key)
);
}
});
}
});
describe("Other tests extracted from .html files", () => {
for (const file of fs.readdirSync(wptDir)) {
if (path.extname(file) === ".js") {
describe(file, () => {
runWPTFile(path.join(wptDir, file));
});
}
}
});
describe("toASCII", () => {
for (const testCase of toASCIITestCases) {
if (typeof testCase === "string") {
// It's a "comment"; skip it.
continue;
}
let description = testCase.input;
if (testCase.comment) {
description += ` (${testCase.comment})`;
}
test(description, testToASCII(testCase));
}
});
});
describe("To-upstream tests", () => {
describe("parsing", () => {
for (const expected of additionalParsingTestCases) {
if (typeof expected === "string") {
// It's a "comment"; skip it.
continue;
}
test(
"<" + expected.input + "> against <" + expected.base + ">",
testURL(expected)
);
}
});
});