Skip to content

Commit

Permalink
feat: port first half of fetch tests to node test runner (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag-roy committed Jan 3, 2024
1 parent d00b822 commit f0a16f9
Show file tree
Hide file tree
Showing 22 changed files with 832 additions and 939 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"test": "node scripts/generate-pem && npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:wpt && npm run test:websocket && npm run test:jest && npm run test:typescript && npm run test:node-test",
"test:cookies": "borp --coverage -p \"test/cookie/*.js\"",
"test:node-fetch": "mocha --exit test/node-fetch",
"test:fetch": "npm run build:node && tap --expose-gc test/fetch/*.js && borp --coverage -p \"test/webidl/*.js\"",
"test:fetch": "npm run build:node && borp --expose-gc --coverage -p \"test/fetch/*.js\" && borp --coverage -p \"test/webidl/*.js\"",
"test:jest": "jest",
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
"test:node-test": "borp --coverage -p \"test/node-test/*.js\"",
Expand All @@ -102,7 +102,7 @@
"@types/node": "^18.0.3",
"abort-controller": "^3.0.0",
"atomic-sleep": "^1.0.0",
"borp": "^0.4.2",
"borp": "^0.5.0",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"chai-iterator": "^3.0.2",
Expand Down
7 changes: 4 additions & 3 deletions test/fetch/407-statuscode-window-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')

test('Receiving a 407 status code w/ a window option present should reject', async (t) => {
const server = createServer((req, res) => {
res.statusCode = 407
res.end()
}).listen(0)

t.teardown(server.close.bind(server))
t.after(server.close.bind(server))
await once(server, 'listening')

// if init.window exists, the spec tells us to set request.window to 'no-window',
// which later causes the request to be rejected if the status code is 407
await t.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
await assert.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
})
31 changes: 16 additions & 15 deletions test/fetch/abort.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')
const { tspl } = require('@matteo.collina/tspl')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
Expand All @@ -17,7 +19,7 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
res.end(JSON.stringify(body))
})

t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0)
await once(server, 'listening')
Expand All @@ -31,43 +33,42 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
signal
})
} catch (e) {
t.equal(e.code, DOMException.ABORT_ERR)
assert.strictEqual(e.code, DOMException.ABORT_ERR)
}
})

test('allows aborting with custom errors', async (t) => {
const server = createServer().listen(0)

t.teardown(server.close.bind(server))
t.after(server.close.bind(server))
await once(server, 'listening')

t.test('Using AbortSignal.timeout with cause', async (t) => {
t.plan(2)

await t.test('Using AbortSignal.timeout with cause', async () => {
const { strictEqual } = tspl(t, { plan: 2 })
try {
await fetch(`http://localhost:${server.address().port}`, {
signal: AbortSignal.timeout(50)
})
t.fail('should throw')
assert.fail('should throw')
} catch (err) {
if (err.name === 'TypeError') {
const cause = err.cause
t.equal(cause.name, 'HeadersTimeoutError')
t.equal(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
strictEqual(cause.name, 'HeadersTimeoutError')
strictEqual(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
} else if (err.name === 'TimeoutError') {
t.equal(err.code, DOMException.TIMEOUT_ERR)
t.equal(err.cause, undefined)
strictEqual(err.code, DOMException.TIMEOUT_ERR)
strictEqual(err.cause, undefined)
} else {
t.error(err)
throw err
}
}
})

t.test('Error defaults to an AbortError DOMException', async (t) => {
t.test('Error defaults to an AbortError DOMException', async () => {
const ac = new AbortController()
ac.abort() // no reason

await t.rejects(
await assert.rejects(
fetch(`http://localhost:${server.address().port}`, {
signal: ac.signal
}),
Expand Down
15 changes: 7 additions & 8 deletions test/fetch/abort2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
Expand All @@ -18,7 +19,7 @@ test('parallel fetch with the same AbortController works as expected', async (t)
res.end(JSON.stringify(body))
})

t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const abortController = new AbortController()

Expand Down Expand Up @@ -49,11 +50,9 @@ test('parallel fetch with the same AbortController works as expected', async (t)
return a
}, { resolved: [], rejected: [] })

t.equal(rejected.length, 9) // out of 10 requests, only 1 should succeed
t.equal(resolved.length, 1)
assert.strictEqual(rejected.length, 9) // out of 10 requests, only 1 should succeed
assert.strictEqual(resolved.length, 1)

t.ok(rejected.every(rej => rej.reason?.code === DOMException.ABORT_ERR))
t.same(resolved[0].value, body)

t.end()
assert.ok(rejected.every(rej => rej.reason?.code === DOMException.ABORT_ERR))
assert.deepStrictEqual(resolved[0].value, body)
})
15 changes: 7 additions & 8 deletions test/fetch/about-uri.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')

test('fetching about: uris', async (t) => {
t.test('about:blank', async (t) => {
await t.rejects(fetch('about:blank'))
await t.test('about:blank', async () => {
await assert.rejects(fetch('about:blank'))
})

t.test('All other about: urls should return an error', async (t) => {
await t.test('All other about: urls should return an error', async () => {
try {
await fetch('about:config')
t.fail('fetching about:config should fail')
assert.fail('fetching about:config should fail')
} catch (e) {
t.ok(e, 'this error was expected')
} finally {
t.end()
assert.ok(e, 'this error was expected')
}
})
})
57 changes: 23 additions & 34 deletions test/fetch/blob-uri.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')
const { Blob } = require('buffer')

Expand All @@ -16,85 +17,73 @@ test('fetching blob: uris', async (t) => {
objectURL = URL.createObjectURL(blob)
})

t.test('a normal fetch request works', async (t) => {
await t.test('a normal fetch request works', async () => {
const res = await fetch(objectURL)

t.equal(blobContents, await res.text())
t.equal(blob.type, res.headers.get('Content-Type'))
t.equal(`${blob.size}`, res.headers.get('Content-Length'))
t.end()
assert.strictEqual(blobContents, await res.text())
assert.strictEqual(blob.type, res.headers.get('Content-Type'))
assert.strictEqual(`${blob.size}`, res.headers.get('Content-Length'))
})

t.test('non-GET method to blob: fails', async (t) => {
await t.test('non-GET method to blob: fails', async () => {
try {
await fetch(objectURL, {
method: 'POST'
})
t.fail('expected POST to blob: uri to fail')
assert.fail('expected POST to blob: uri to fail')
} catch (e) {
t.ok(e, 'Got the expected error')
} finally {
t.end()
assert.ok(e, 'Got the expected error')
}
})

// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L36-L41
t.test('fetching revoked URL should fail', async (t) => {
await t.test('fetching revoked URL should fail', async () => {
URL.revokeObjectURL(objectURL)

try {
await fetch(objectURL)
t.fail('expected revoked blob: url to fail')
assert.fail('expected revoked blob: url to fail')
} catch (e) {
t.ok(e, 'Got the expected error')
} finally {
t.end()
assert.ok(e, 'Got the expected error')
}
})

// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L28-L34
t.test('works with a fragment', async (t) => {
await t.test('works with a fragment', async () => {
const res = await fetch(objectURL + '#fragment')

t.equal(blobContents, await res.text())
t.end()
assert.strictEqual(blobContents, await res.text())
})

// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L52-L56
t.test('Appending a query string to blob: url should cause fetch to fail', async (t) => {
await t.test('Appending a query string to blob: url should cause fetch to fail', async () => {
try {
await fetch(objectURL + '?querystring')
t.fail('expected ?querystring blob: url to fail')
assert.fail('expected ?querystring blob: url to fail')
} catch (e) {
t.ok(e, 'Got the expected error')
} finally {
t.end()
assert.ok(e, 'Got the expected error')
}
})

// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L58-L62
t.test('Appending a path should cause fetch to fail', async (t) => {
await t.test('Appending a path should cause fetch to fail', async () => {
try {
await fetch(objectURL + '/path')
t.fail('expected /path blob: url to fail')
assert.fail('expected /path blob: url to fail')
} catch (e) {
t.ok(e, 'Got the expected error')
} finally {
t.end()
assert.ok(e, 'Got the expected error')
}
})

// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L64-L70
t.test('these http methods should fail', async (t) => {
await t.test('these http methods should fail', async () => {
for (const method of ['HEAD', 'POST', 'DELETE', 'OPTIONS', 'PUT', 'CUSTOM']) {
try {
await fetch(objectURL, { method })
t.fail(`${method} fetch should have failed`)
assert.fail(`${method} fetch should have failed`)
} catch (e) {
t.ok(e, `${method} blob url - test succeeded`)
assert.ok(e, `${method} blob url - test succeeded`)
}
}

t.end()
})
})
28 changes: 13 additions & 15 deletions test/fetch/bundle.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')

const { Response, Request, FormData, Headers } = require('../../undici-fetch')

test('bundle sets constructor.name and .name properly', (t) => {
t.equal(new Response().constructor.name, 'Response')
t.equal(Response.name, 'Response')
test('bundle sets constructor.name and .name properly', () => {
assert.strictEqual(new Response().constructor.name, 'Response')
assert.strictEqual(Response.name, 'Response')

t.equal(new Request('http://a').constructor.name, 'Request')
t.equal(Request.name, 'Request')
assert.strictEqual(new Request('http://a').constructor.name, 'Request')
assert.strictEqual(Request.name, 'Request')

t.equal(new Headers().constructor.name, 'Headers')
t.equal(Headers.name, 'Headers')
assert.strictEqual(new Headers().constructor.name, 'Headers')
assert.strictEqual(Headers.name, 'Headers')

t.equal(new FormData().constructor.name, 'FormData')
t.equal(FormData.name, 'FormData')

t.end()
assert.strictEqual(new FormData().constructor.name, 'FormData')
assert.strictEqual(FormData.name, 'FormData')
})

test('regression test for https://github.com/nodejs/node/issues/50263', (t) => {
test('regression test for https://github.com/nodejs/node/issues/50263', () => {
const request = new Request('https://a', {
headers: {
test: 'abc'
Expand All @@ -30,6 +29,5 @@ test('regression test for https://github.com/nodejs/node/issues/50263', (t) => {

const request1 = new Request(request, { body: 'does not matter' })

t.equal(request1.headers.get('test'), 'abc')
t.end()
assert.strictEqual(request1.headers.get('test'), 'abc')
})
7 changes: 4 additions & 3 deletions test/fetch/client-error-stack-trace.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')
const { fetch: fetchIndex } = require('../../index-fetch')

test('FETCH: request errors and prints trimmed stack trace', async (t) => {
try {
await fetch('http://a.com')
} catch (error) {
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
assert.ok(error.stack.includes(`at async TestContext.<anonymous> (${__filename}`))
}
})

test('FETCH-index: request errors and prints trimmed stack trace', async (t) => {
try {
await fetchIndex('http://a.com')
} catch (error) {
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
assert.ok(error.stack.includes(`at async TestContext.<anonymous> (${__filename}`))
}
})

0 comments on commit f0a16f9

Please sign in to comment.