Skip to content

Commit 552c235

Browse files
authored
feat(WPTs): add in idlharness.any.js test (nodejs#1689)
this was painful.
1 parent a04cf3d commit 552c235

File tree

17 files changed

+10928
-38
lines changed

17 files changed

+10928
-38
lines changed

lib/fetch/body.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -530,32 +530,8 @@ function bodyMixinMethods (instance) {
530530
return methods
531531
}
532532

533-
const properties = {
534-
body: {
535-
enumerable: true,
536-
get () {
537-
if (!this || !this[kState]) {
538-
throw new TypeError('Illegal invocation')
539-
}
540-
541-
return this[kState].body ? this[kState].body.stream : null
542-
}
543-
},
544-
bodyUsed: {
545-
enumerable: true,
546-
get () {
547-
if (!this || !this[kState]) {
548-
throw new TypeError('Illegal invocation')
549-
}
550-
551-
return !!this[kState].body && util.isDisturbed(this[kState].body.stream)
552-
}
553-
}
554-
}
555-
556533
function mixinBody (prototype) {
557534
Object.assign(prototype.prototype, bodyMixinMethods(prototype))
558-
Object.defineProperties(prototype.prototype, properties)
559535
}
560536

561537
module.exports = {

lib/fetch/headers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ Object.defineProperties(Headers.prototype, {
478478
keys: kEnumerableProperty,
479479
values: kEnumerableProperty,
480480
entries: kEnumerableProperty,
481-
forEach: kEnumerableProperty
481+
forEach: kEnumerableProperty,
482+
[Symbol.iterator]: { enumerable: false }
482483
})
483484

484485
webidl.converters.HeadersInit = function (V) {

lib/fetch/request.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,30 @@ class Request {
711711
return this[kSignal]
712712
}
713713

714+
get body () {
715+
if (!this || !this[kState]) {
716+
throw new TypeError('Illegal invocation')
717+
}
718+
719+
return this[kState].body ? this[kState].body.stream : null
720+
}
721+
722+
get bodyUsed () {
723+
if (!this || !this[kState]) {
724+
throw new TypeError('Illegal invocation')
725+
}
726+
727+
return !!this[kState].body && util.isDisturbed(this[kState].body.stream)
728+
}
729+
730+
get duplex () {
731+
if (!(this instanceof Request)) {
732+
throw new TypeError('Illegal invocation')
733+
}
734+
735+
return 'half'
736+
}
737+
714738
// Returns a clone of request.
715739
clone () {
716740
if (!(this instanceof Request)) {
@@ -828,16 +852,20 @@ Object.defineProperties(Request.prototype, {
828852
redirect: kEnumerableProperty,
829853
clone: kEnumerableProperty,
830854
signal: kEnumerableProperty,
831-
duplex: {
832-
...kEnumerableProperty,
833-
get () {
834-
// The duplex getter steps are to return "half".
835-
return 'half'
836-
},
837-
set () {
838-
839-
}
840-
}
855+
duplex: kEnumerableProperty,
856+
destination: kEnumerableProperty,
857+
body: kEnumerableProperty,
858+
bodyUsed: kEnumerableProperty,
859+
isHistoryNavigation: kEnumerableProperty,
860+
isReloadNavigation: kEnumerableProperty,
861+
keepalive: kEnumerableProperty,
862+
integrity: kEnumerableProperty,
863+
cache: kEnumerableProperty,
864+
credentials: kEnumerableProperty,
865+
attribute: kEnumerableProperty,
866+
referrerPolicy: kEnumerableProperty,
867+
referrer: kEnumerableProperty,
868+
mode: kEnumerableProperty
841869
})
842870

843871
webidl.converters.Request = webidl.interfaceConverter(

lib/fetch/response.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ class Response {
259259
return this[kHeaders]
260260
}
261261

262+
get body () {
263+
if (!this || !this[kState]) {
264+
throw new TypeError('Illegal invocation')
265+
}
266+
267+
return this[kState].body ? this[kState].body.stream : null
268+
}
269+
270+
get bodyUsed () {
271+
if (!this || !this[kState]) {
272+
throw new TypeError('Illegal invocation')
273+
}
274+
275+
return !!this[kState].body && util.isDisturbed(this[kState].body.stream)
276+
}
277+
262278
// Returns a clone of response.
263279
clone () {
264280
if (!(this instanceof Response)) {
@@ -299,7 +315,15 @@ Object.defineProperties(Response.prototype, {
299315
redirected: kEnumerableProperty,
300316
statusText: kEnumerableProperty,
301317
headers: kEnumerableProperty,
302-
clone: kEnumerableProperty
318+
clone: kEnumerableProperty,
319+
body: kEnumerableProperty,
320+
bodyUsed: kEnumerableProperty
321+
})
322+
323+
Object.defineProperties(Response, {
324+
json: kEnumerableProperty,
325+
redirect: kEnumerableProperty,
326+
error: kEnumerableProperty
303327
})
304328

305329
// https://fetch.spec.whatwg.org/#concept-response-clone

test/wpt/runner/runner/runner.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readdirSync, readFileSync, statSync } from 'node:fs'
33
import { basename, isAbsolute, join, resolve } from 'node:path'
44
import { fileURLToPath } from 'node:url'
55
import { Worker } from 'node:worker_threads'
6-
import { parseMeta } from './util.mjs'
6+
import { parseMeta, resolveSymLink } from './util.mjs'
77

88
const basePath = fileURLToPath(join(import.meta.url, '../../..'))
99
const testPath = join(basePath, 'tests')
@@ -155,7 +155,10 @@ export class WPTRunner extends EventEmitter {
155155
resolveMeta (code, path) {
156156
const meta = parseMeta(code)
157157
const scripts = meta.scripts.map((script) => {
158-
if (isAbsolute(script)) {
158+
if (script === '/resources/WebIDLParser.js') {
159+
// See https://github.com/web-platform-tests/wpt/pull/731
160+
return resolveSymLink(join(testPath, script))
161+
} else if (isAbsolute(script)) {
159162
return readFileSync(join(testPath, script), 'utf-8')
160163
}
161164

test/wpt/runner/runner/util.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { exit } from 'node:process'
2+
import { readFileSync, readlinkSync } from 'node:fs'
23

34
/**
45
* Parse the `Meta:` tags sometimes included in tests.
@@ -63,3 +64,8 @@ export function parseMeta (fileContents) {
6364

6465
return meta
6566
}
67+
68+
export function resolveSymLink (path) {
69+
const symlink = readlinkSync(path)
70+
return readFileSync(symlink, 'utf-8')
71+
}

test/wpt/runner/runner/worker.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const globalPropertyDescriptors = {
2626
Object.defineProperties(globalThis, {
2727
fetch: {
2828
...globalPropertyDescriptors,
29+
enumerable: true,
2930
value: fetch
3031
},
3132
File: {
@@ -60,10 +61,14 @@ runInThisContext(`
6061
},
6162
isShadowRealm () {
6263
return false
64+
},
65+
isWindow () {
66+
return false
6367
}
6468
}
6569
globalThis.window = globalThis
6670
globalThis.location = new URL('${url}')
71+
globalThis.Window = Object.getPrototypeOf(globalThis).constructor
6772
`)
6873

6974
const harness = readFileSync(join(basePath, '../runner/resources/testharness.cjs'), 'utf-8')

test/wpt/server/server.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const server = createServer(async (req, res) => {
3030
const fullUrl = new URL(req.url, `http://localhost:${server.address().port}`)
3131

3232
switch (fullUrl.pathname) {
33+
case '/interfaces/dom.idl':
34+
case '/interfaces/html.idl':
35+
case '/interfaces/fetch.idl':
36+
case '/interfaces/referrer-policy.idl':
3337
case '/xhr/resources/utf16-bom.json':
3438
case '/fetch/data-urls/resources/base64.json':
3539
case '/fetch/data-urls/resources/data-urls.json':

test/wpt/status/fetch.status.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@
2323
"Consume response's body as formData with correct multipart type (error case)",
2424
"Consume empty FormData response body as text"
2525
]
26+
},
27+
"idlharness.any.js": {
28+
"fail": [
29+
"Response interface: operation json(any, optional ResponseInit)"
30+
]
2631
}
2732
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// META: global=window,worker
2+
// META: script=/resources/WebIDLParser.js
3+
// META: script=/resources/idlharness.js
4+
// META: timeout=long
5+
6+
idl_test(
7+
['fetch'],
8+
['referrer-policy', 'html', 'dom'],
9+
idl_array => {
10+
idl_array.add_objects({
11+
Headers: ["new Headers()"],
12+
Request: ["new Request('about:blank')"],
13+
Response: ["new Response()"],
14+
});
15+
if (self.GLOBAL.isWindow()) {
16+
idl_array.add_objects({ Window: ['window'] });
17+
} else if (self.GLOBAL.isWorker()) {
18+
idl_array.add_objects({ WorkerGlobalScope: ['self'] });
19+
}
20+
}
21+
);

0 commit comments

Comments
 (0)