Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions test/fetch/407-statuscode-window-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { fetch } = require('../..')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { test } = require('node:test')
const assert = require('node:assert')

const { closeServerAsPromise } = require('../utils/node-http')

Expand All @@ -19,5 +18,5 @@ test('Receiving a 407 status code w/ a window option present should reject', asy

// 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 assert.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
await t.assert.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
})
16 changes: 7 additions & 9 deletions test/fetch/abort.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { tspl } = require('@matteo.collina/tspl')
const { fetch } = require('../..')
const { createServer } = require('node:http')
const { once } = require('node:events')
Expand All @@ -15,20 +13,20 @@ test('allows aborting with custom errors', async (t) => {
await once(server, 'listening')

await t.test('Using AbortSignal.timeout with cause', async (t) => {
const { strictEqual } = tspl(t, { plan: 2 })
t.plan(2)
try {
await fetch(`http://localhost:${server.address().port}`, {
signal: AbortSignal.timeout(50)
})
assert.fail('should throw')
t.assert.fail('should throw')
} catch (err) {
if (err.name === 'TypeError') {
const cause = err.cause
strictEqual(cause.name, 'HeadersTimeoutError')
strictEqual(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
t.assert.strictEqual(cause.name, 'HeadersTimeoutError')
t.assert.strictEqual(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
} else if (err.name === 'TimeoutError') {
strictEqual(err.code, DOMException.TIMEOUT_ERR)
strictEqual(err.cause, undefined)
t.assert.strictEqual(err.code, DOMException.TIMEOUT_ERR)
t.assert.strictEqual(err.cause, undefined)
} else {
throw err
}
Expand All @@ -39,7 +37,7 @@ test('allows aborting with custom errors', async (t) => {
const ac = new AbortController()
ac.abort() // no reason

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

const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')
const { createServer } = require('node:http')
const { once } = require('node:events')
Expand Down Expand Up @@ -52,9 +51,9 @@ test('parallel fetch with the same AbortController works as expected', async (t)
return a
}, { resolved: [], rejected: [] })

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

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

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

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

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

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

test('fetching blob: uris', async (t) => {
Expand All @@ -19,19 +18,19 @@ test('fetching blob: uris', async (t) => {
await t.test('a normal fetch request works', async () => {
const res = await fetch(objectURL)

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.assert.strictEqual(blobContents, await res.text())
t.assert.strictEqual(blob.type, res.headers.get('Content-Type'))
t.assert.strictEqual(`${blob.size}`, res.headers.get('Content-Length'))
})

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

Expand All @@ -41,36 +40,36 @@ test('fetching blob: uris', async (t) => {

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

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

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

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

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

Expand All @@ -79,9 +78,9 @@ test('fetching blob: uris', async (t) => {
for (const method of ['HEAD', 'POST', 'DELETE', 'OPTIONS', 'PUT', 'CUSTOM']) {
try {
await fetch(objectURL, { method })
assert.fail(`${method} fetch should have failed`)
t.assert.fail(`${method} fetch should have failed`)
} catch (e) {
assert.ok(e, `${method} blob url - test succeeded`)
t.assert.ok(e, `${method} blob url - test succeeded`)
}
}
})
Expand Down
29 changes: 14 additions & 15 deletions test/fetch/bundle.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
'use strict'

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

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

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

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

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

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

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

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

assert.strictEqual(request1.headers.get('test'), 'abc')
t.assert.strictEqual(request1.headers.get('test'), 'abc')
})

test('WebSocket related events are exported', (t) => {
assert.deepStrictEqual(typeof CloseEvent, 'function')
assert.deepStrictEqual(typeof MessageEvent, 'function')
assert.deepStrictEqual(typeof ErrorEvent, 'function')
t.assert.deepStrictEqual(typeof CloseEvent, 'function')
t.assert.deepStrictEqual(typeof MessageEvent, 'function')
t.assert.deepStrictEqual(typeof ErrorEvent, 'function')
})
17 changes: 8 additions & 9 deletions test/fetch/client-error-stack-trace.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { sep } = require('node:path')
const { fetch, setGlobalDispatcher, Agent } = require('../..')
const { fetch: fetchIndex } = require('../../index-fetch')
Expand All @@ -16,10 +15,10 @@ test('FETCH: request errors and prints trimmed stack trace', async (t) => {
await fetch('http://a.com')
} catch (error) {
const stackLines = error.stack.split('\n')
assert.ok(stackLines[0].includes('TypeError: fetch failed'))
assert.ok(stackLines[1].includes(`undici${sep}index.js`))
assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
t.assert.ok(stackLines[0].includes('TypeError: fetch failed'))
t.assert.ok(stackLines[1].includes(`undici${sep}index.js`))
t.assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
t.assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
}
})

Expand All @@ -28,9 +27,9 @@ test('FETCH-index: request errors and prints trimmed stack trace', async (t) =>
await fetchIndex('http://a.com')
} catch (error) {
const stackLines = error.stack.split('\n')
assert.ok(stackLines[0].includes('TypeError: fetch failed'))
assert.ok(stackLines[1].includes(`undici${sep}index-fetch.js`))
assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
t.assert.ok(stackLines[0].includes('TypeError: fetch failed'))
t.assert.ok(stackLines[1].includes(`undici${sep}index-fetch.js`))
t.assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
t.assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
}
})
Loading
Loading