Skip to content

Commit

Permalink
feat: port remaining fetch tests to node test runner (#2587)
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag-roy committed Jan 4, 2024
1 parent 990b96e commit f7c8db8
Show file tree
Hide file tree
Showing 18 changed files with 483 additions and 568 deletions.
62 changes: 30 additions & 32 deletions test/fetch/integrity.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 { createServer } = require('http')
const { createHash, getHashes } = require('crypto')
const { gzipSync } = require('zlib')
Expand All @@ -14,49 +15,46 @@ setGlobalDispatcher(new Agent({
keepAliveMaxTimeout: 1
}))

test('request with correct integrity checksum', (t) => {
test('request with correct integrity checksum', (t, done) => {
const body = 'Hello world!'
const hash = createHash('sha256').update(body).digest('base64')

const server = createServer((req, res) => {
res.end(body)
})

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

server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
})
t.strictSame(body, await response.text())
t.end()
assert.strictEqual(body, await response.text())
done()
})
})

test('request with wrong integrity checksum', (t) => {
test('request with wrong integrity checksum', async (t) => {
const body = 'Hello world!'
const hash = 'c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51b'

const server = createServer((req, res) => {
res.end(body)
})
}).listen(0)

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

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
}).then(response => {
t.pass('request did not fail')
}).catch((err) => {
t.equal(err.cause.message, 'integrity mismatch')
}).finally(() => {
t.end()
})
const expectedError = new TypeError('fetch failed', {
cause: new Error('integrity mismatch')
})

await assert.rejects(fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
}), expectedError)
})

test('request with integrity checksum on encoded body', (t) => {
test('request with integrity checksum on encoded body', (t, done) => {
const body = 'Hello world!'
const hash = createHash('sha256').update(body).digest('base64')

Expand All @@ -65,14 +63,14 @@ test('request with integrity checksum on encoded body', (t) => {
res.end(gzipSync(body))
})

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

server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
})
t.strictSame(body, await response.text())
t.end()
assert.strictEqual(body, await response.text())
done()
})
})

Expand All @@ -81,10 +79,10 @@ test('request with a totally incorrect integrity', async (t) => {
res.end()
}).listen(0)

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

await t.resolves(fetch(`http://localhost:${server.address().port}`, {
await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, {
integrity: 'what-integrityisthis'
}))
})
Expand All @@ -97,10 +95,10 @@ test('request with mixed in/valid integrities', async (t) => {
res.end(body)
}).listen(0)

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

await t.resolves(fetch(`http://localhost:${server.address().port}`, {
await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, {
integrity: `invalid-integrity sha256-${hash}`
}))
})
Expand All @@ -113,16 +111,16 @@ test('request with sha384 hash', { skip: !supportedHashes.includes('sha384') },
res.end(body)
}).listen(0)

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

// request should succeed
await t.resolves(fetch(`http://localhost:${server.address().port}`, {
await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, {
integrity: `sha384-${hash}`
}))

// request should fail
await t.rejects(fetch(`http://localhost:${server.address().port}`, {
await assert.rejects(fetch(`http://localhost:${server.address().port}`, {
integrity: 'sha384-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
}))
})
Expand All @@ -135,16 +133,16 @@ test('request with sha512 hash', { skip: !supportedHashes.includes('sha512') },
res.end(body)
}).listen(0)

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

// request should succeed
await t.resolves(fetch(`http://localhost:${server.address().port}`, {
await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, {
integrity: `sha512-${hash}`
}))

// request should fail
await t.rejects(fetch(`http://localhost:${server.address().port}`, {
await assert.rejects(fetch(`http://localhost:${server.address().port}`, {
integrity: 'sha512-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
}))
})
9 changes: 5 additions & 4 deletions test/fetch/issue-1447.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')

const undici = require('../..')
const { fetch: theoreticalGlobalFetch } = require('../../undici-fetch')

test('Mocking works with both fetches', async (t) => {
const { strictEqual } = tspl(t, { plan: 3 })

const mockAgent = new undici.MockAgent()
const body = JSON.stringify({ foo: 'bar' })

Expand All @@ -17,7 +20,7 @@ test('Mocking works with both fetches', async (t) => {
path: '/path',
method: 'POST',
body (bodyString) {
t.equal(bodyString, body)
strictEqual(bodyString, body)
return true
}
}).reply(200, { ok: 1 }).times(2)
Expand All @@ -35,6 +38,4 @@ test('Mocking works with both fetches', async (t) => {
method: 'POST',
body
})

t.end()
})
9 changes: 6 additions & 3 deletions test/fetch/issue-2009.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')

test('issue 2009', async (t) => {
const { doesNotReject } = tspl(t, { plan: 10 })

const server = createServer((req, res) => {
res.setHeader('a', 'b')
res.flushHeaders()

res.socket.end()
}).listen(0)

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

for (let i = 0; i < 10; i++) {
await t.resolves(
await doesNotReject(
fetch(`http://localhost:${server.address().port}`).then(
async (resp) => {
await resp.body.cancel('Some message')
Expand Down
7 changes: 4 additions & 3 deletions test/fetch/issue-2021.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 { once } = require('events')
const { createServer } = require('http')
const { fetch } = require('../..')
Expand All @@ -17,12 +18,12 @@ test('content-length header is removed on redirect', async (t) => {
res.end()
}).listen(0).unref()

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

const body = 'a+b+c'

await t.resolves(fetch(`http://localhost:${server.address().port}/redirect`, {
await assert.doesNotReject(fetch(`http://localhost:${server.address().port}/redirect`, {
method: 'POST',
body,
headers: {
Expand Down
12 changes: 9 additions & 3 deletions test/fetch/issue-2242.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use strict'

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

test('fetch with signal already aborted', async (t) => {
await t.rejects(fetch('http://localhost', { signal: AbortSignal.abort('Already aborted') }), 'Already aborted')
test('fetch with signal already aborted', async () => {
await assert.rejects(
fetch('http://localhost', {
signal: AbortSignal.abort('Already aborted')
}),
/Already aborted/
)
})
9 changes: 5 additions & 4 deletions test/fetch/issue-2318.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const { once } = require('events')
const { createServer } = require('http')
const { fetch } = require('../..')

test('Undici overrides user-provided `Host` header', async (t) => {
t.plan(1)
const { strictEqual } = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
t.equal(req.headers.host, `localhost:${server.address().port}`)
strictEqual(req.headers.host, `localhost:${server.address().port}`)

res.end()
}).listen(0)

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

await fetch(`http://localhost:${server.address().port}`, {
Expand Down
8 changes: 4 additions & 4 deletions test/fetch/issue-node-46525.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

const { once } = require('events')
const { createServer } = require('http')
const { test } = require('tap')
const { test } = require('node:test')
const { fetch } = require('../..')

// https://github.com/nodejs/node/issues/46525
test('No warning when reusing AbortController', async (t) => {
function onWarning (error) {
t.error(error, 'Got warning')
function onWarning () {
throw new Error('Got warning')
}

const server = createServer((req, res) => res.end()).listen(0)

await once(server, 'listening')

process.on('warning', onWarning)
t.teardown(() => {
t.after(() => {
process.off('warning', onWarning)
return server.close()
})
Expand Down

0 comments on commit f7c8db8

Please sign in to comment.