Skip to content

Commit

Permalink
feat(WPTs): add in idlharness.any.js test (#1689)
Browse files Browse the repository at this point in the history
this was painful.
  • Loading branch information
KhafraDev committed Oct 7, 2022
1 parent a04cf3d commit 552c235
Show file tree
Hide file tree
Showing 17 changed files with 10,928 additions and 38 deletions.
24 changes: 0 additions & 24 deletions lib/fetch/body.js
Expand Up @@ -530,32 +530,8 @@ function bodyMixinMethods (instance) {
return methods
}

const properties = {
body: {
enumerable: true,
get () {
if (!this || !this[kState]) {
throw new TypeError('Illegal invocation')
}

return this[kState].body ? this[kState].body.stream : null
}
},
bodyUsed: {
enumerable: true,
get () {
if (!this || !this[kState]) {
throw new TypeError('Illegal invocation')
}

return !!this[kState].body && util.isDisturbed(this[kState].body.stream)
}
}
}

function mixinBody (prototype) {
Object.assign(prototype.prototype, bodyMixinMethods(prototype))
Object.defineProperties(prototype.prototype, properties)
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion lib/fetch/headers.js
Expand Up @@ -478,7 +478,8 @@ Object.defineProperties(Headers.prototype, {
keys: kEnumerableProperty,
values: kEnumerableProperty,
entries: kEnumerableProperty,
forEach: kEnumerableProperty
forEach: kEnumerableProperty,
[Symbol.iterator]: { enumerable: false }
})

webidl.converters.HeadersInit = function (V) {
Expand Down
48 changes: 38 additions & 10 deletions lib/fetch/request.js
Expand Up @@ -711,6 +711,30 @@ class Request {
return this[kSignal]
}

get body () {
if (!this || !this[kState]) {
throw new TypeError('Illegal invocation')
}

return this[kState].body ? this[kState].body.stream : null
}

get bodyUsed () {
if (!this || !this[kState]) {
throw new TypeError('Illegal invocation')
}

return !!this[kState].body && util.isDisturbed(this[kState].body.stream)
}

get duplex () {
if (!(this instanceof Request)) {
throw new TypeError('Illegal invocation')
}

return 'half'
}

// Returns a clone of request.
clone () {
if (!(this instanceof Request)) {
Expand Down Expand Up @@ -828,16 +852,20 @@ Object.defineProperties(Request.prototype, {
redirect: kEnumerableProperty,
clone: kEnumerableProperty,
signal: kEnumerableProperty,
duplex: {
...kEnumerableProperty,
get () {
// The duplex getter steps are to return "half".
return 'half'
},
set () {

}
}
duplex: kEnumerableProperty,
destination: kEnumerableProperty,
body: kEnumerableProperty,
bodyUsed: kEnumerableProperty,
isHistoryNavigation: kEnumerableProperty,
isReloadNavigation: kEnumerableProperty,
keepalive: kEnumerableProperty,
integrity: kEnumerableProperty,
cache: kEnumerableProperty,
credentials: kEnumerableProperty,
attribute: kEnumerableProperty,
referrerPolicy: kEnumerableProperty,
referrer: kEnumerableProperty,
mode: kEnumerableProperty
})

webidl.converters.Request = webidl.interfaceConverter(
Expand Down
26 changes: 25 additions & 1 deletion lib/fetch/response.js
Expand Up @@ -259,6 +259,22 @@ class Response {
return this[kHeaders]
}

get body () {
if (!this || !this[kState]) {
throw new TypeError('Illegal invocation')
}

return this[kState].body ? this[kState].body.stream : null
}

get bodyUsed () {
if (!this || !this[kState]) {
throw new TypeError('Illegal invocation')
}

return !!this[kState].body && util.isDisturbed(this[kState].body.stream)
}

// Returns a clone of response.
clone () {
if (!(this instanceof Response)) {
Expand Down Expand Up @@ -299,7 +315,15 @@ Object.defineProperties(Response.prototype, {
redirected: kEnumerableProperty,
statusText: kEnumerableProperty,
headers: kEnumerableProperty,
clone: kEnumerableProperty
clone: kEnumerableProperty,
body: kEnumerableProperty,
bodyUsed: kEnumerableProperty
})

Object.defineProperties(Response, {
json: kEnumerableProperty,
redirect: kEnumerableProperty,
error: kEnumerableProperty
})

// https://fetch.spec.whatwg.org/#concept-response-clone
Expand Down
7 changes: 5 additions & 2 deletions test/wpt/runner/runner/runner.mjs
Expand Up @@ -3,7 +3,7 @@ import { readdirSync, readFileSync, statSync } from 'node:fs'
import { basename, isAbsolute, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { Worker } from 'node:worker_threads'
import { parseMeta } from './util.mjs'
import { parseMeta, resolveSymLink } from './util.mjs'

const basePath = fileURLToPath(join(import.meta.url, '../../..'))
const testPath = join(basePath, 'tests')
Expand Down Expand Up @@ -155,7 +155,10 @@ export class WPTRunner extends EventEmitter {
resolveMeta (code, path) {
const meta = parseMeta(code)
const scripts = meta.scripts.map((script) => {
if (isAbsolute(script)) {
if (script === '/resources/WebIDLParser.js') {
// See https://github.com/web-platform-tests/wpt/pull/731
return resolveSymLink(join(testPath, script))
} else if (isAbsolute(script)) {
return readFileSync(join(testPath, script), 'utf-8')
}

Expand Down
6 changes: 6 additions & 0 deletions test/wpt/runner/runner/util.mjs
@@ -1,4 +1,5 @@
import { exit } from 'node:process'
import { readFileSync, readlinkSync } from 'node:fs'

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

return meta
}

export function resolveSymLink (path) {
const symlink = readlinkSync(path)
return readFileSync(symlink, 'utf-8')
}
5 changes: 5 additions & 0 deletions test/wpt/runner/runner/worker.mjs
Expand Up @@ -26,6 +26,7 @@ const globalPropertyDescriptors = {
Object.defineProperties(globalThis, {
fetch: {
...globalPropertyDescriptors,
enumerable: true,
value: fetch
},
File: {
Expand Down Expand Up @@ -60,10 +61,14 @@ runInThisContext(`
},
isShadowRealm () {
return false
},
isWindow () {
return false
}
}
globalThis.window = globalThis
globalThis.location = new URL('${url}')
globalThis.Window = Object.getPrototypeOf(globalThis).constructor
`)

const harness = readFileSync(join(basePath, '../runner/resources/testharness.cjs'), 'utf-8')
Expand Down
4 changes: 4 additions & 0 deletions test/wpt/server/server.mjs
Expand Up @@ -30,6 +30,10 @@ const server = createServer(async (req, res) => {
const fullUrl = new URL(req.url, `http://localhost:${server.address().port}`)

switch (fullUrl.pathname) {
case '/interfaces/dom.idl':
case '/interfaces/html.idl':
case '/interfaces/fetch.idl':
case '/interfaces/referrer-policy.idl':
case '/xhr/resources/utf16-bom.json':
case '/fetch/data-urls/resources/base64.json':
case '/fetch/data-urls/resources/data-urls.json':
Expand Down
5 changes: 5 additions & 0 deletions test/wpt/status/fetch.status.json
Expand Up @@ -23,5 +23,10 @@
"Consume response's body as formData with correct multipart type (error case)",
"Consume empty FormData response body as text"
]
},
"idlharness.any.js": {
"fail": [
"Response interface: operation json(any, optional ResponseInit)"
]
}
}
21 changes: 21 additions & 0 deletions test/wpt/tests/fetch/api/idlharness.any.js
@@ -0,0 +1,21 @@
// META: global=window,worker
// META: script=/resources/WebIDLParser.js
// META: script=/resources/idlharness.js
// META: timeout=long

idl_test(
['fetch'],
['referrer-policy', 'html', 'dom'],
idl_array => {
idl_array.add_objects({
Headers: ["new Headers()"],
Request: ["new Request('about:blank')"],
Response: ["new Response()"],
});
if (self.GLOBAL.isWindow()) {
idl_array.add_objects({ Window: ['window'] });
} else if (self.GLOBAL.isWorker()) {
idl_array.add_objects({ WorkerGlobalScope: ['self'] });
}
}
);

0 comments on commit 552c235

Please sign in to comment.