Skip to content

Commit

Permalink
fix: add test helper for closing server as promise (#2604)
Browse files Browse the repository at this point in the history
* Introduce `closeServerAsPromise` helper

* Remove needless call

* Add `closeClientAndServerAsPromise` to http2

* Move `debug.js` to `node-test`
  • Loading branch information
sosukesuzuki authored Jan 8, 2024
1 parent 5b0d401 commit cf74740
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 112 deletions.
4 changes: 3 additions & 1 deletion test/fetch/407-statuscode-window-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const { once } = require('events')
const { test } = require('node:test')
const assert = require('node:assert')

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

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.after(server.close.bind(server))
t.after(closeServerAsPromise(server))
await once(server, 'listening')

// if init.window exists, the spec tells us to set request.window to 'no-window',
Expand Down
6 changes: 4 additions & 2 deletions test/fetch/abort.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')

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

const { AbortController: NPMAbortController } = require('abort-controller')

test('Allow the usage of custom implementation of AbortController', async (t) => {
Expand All @@ -19,7 +21,7 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
res.end(JSON.stringify(body))
})

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

server.listen(0)
await once(server, 'listening')
Expand All @@ -40,7 +42,7 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
test('allows aborting with custom errors', async (t) => {
const server = createServer().listen(0)

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

await t.test('Using AbortSignal.timeout with cause', async () => {
Expand Down
4 changes: 3 additions & 1 deletion test/fetch/abort2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')

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

/* global AbortController */

test('parallel fetch with the same AbortController works as expected', async (t) => {
Expand All @@ -19,7 +21,7 @@ test('parallel fetch with the same AbortController works as expected', async (t)
res.end(JSON.stringify(body))
})

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

const abortController = new AbortController()

Expand Down
56 changes: 28 additions & 28 deletions test/fetch/client-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const { gzipSync } = require('zlib')
const { promisify } = require('util')
const { randomFillSync, createHash } = require('crypto')

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

setGlobalDispatcher(new Agent({
keepAliveTimeout: 1,
keepAliveMaxTimeout: 1
Expand All @@ -41,7 +43,7 @@ test('request json', (t, done) => {
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -57,7 +59,7 @@ test('request text', (t, done) => {
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -73,7 +75,7 @@ test('request arrayBuffer', (t, done) => {
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -90,7 +92,7 @@ test('should set type of blob object to the value of the `Content-Type` header f
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(obj))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -104,7 +106,7 @@ test('pre aborted with readable request body', (t, done) => {

const server = createServer((req, res) => {
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const ac = new AbortController()
Expand All @@ -129,7 +131,7 @@ test('pre aborted with closed readable request body', (t, done) => {

const server = createServer((req, res) => {
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const ac = new AbortController()
Expand Down Expand Up @@ -163,7 +165,7 @@ test('unsupported formData 1', (t, done) => {
res.setHeader('content-type', 'asdasdsad')
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
Expand Down Expand Up @@ -193,7 +195,7 @@ test('multipart formdata not base64', async (t) => {
res.write(formRaw)
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

const listen = promisify(server.listen.bind(server))
await listen(0)
Expand Down Expand Up @@ -221,7 +223,7 @@ test('multipart formdata base64', (t, done) => {
}
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
Expand Down Expand Up @@ -268,7 +270,7 @@ test('busboy emit error', async (t) => {
res.write(formRaw)
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

const listen = promisify(server.listen.bind(server))
await listen(0)
Expand Down Expand Up @@ -296,7 +298,7 @@ test('urlencoded formData', (t, done) => {
res.setHeader('content-type', 'application/x-www-form-urlencoded')
res.end('field1=value1&field2=value2')
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
Expand All @@ -316,7 +318,7 @@ test('text with BOM', (t, done) => {
res.setHeader('content-type', 'application/x-www-form-urlencoded')
res.end('\uFEFFtest=\uFEFF')
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
Expand All @@ -335,7 +337,7 @@ test('formData with BOM', (t, done) => {
res.setHeader('content-type', 'application/x-www-form-urlencoded')
res.end('\uFEFFtest=\uFEFF')
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
Expand All @@ -353,7 +355,7 @@ test('locked blob body', (t, done) => {
const server = createServer((req, res) => {
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -371,7 +373,7 @@ test('disturbed blob body', (t, done) => {
const server = createServer((req, res) => {
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`)
Expand Down Expand Up @@ -403,7 +405,7 @@ test('redirect with body', (t, done) => {
res.end(String(count))
}
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`, {
Expand Down Expand Up @@ -431,7 +433,7 @@ test('redirect with stream', (t, done) => {
}
}, 50)
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`, {
Expand Down Expand Up @@ -485,7 +487,7 @@ test('post FormData with Blob', (t, done) => {
const server = createServer((req, res) => {
req.pipe(res)
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`, {
Expand All @@ -506,7 +508,7 @@ test('post FormData with File', (t, done) => {
const server = createServer((req, res) => {
req.pipe(res)
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`, {
Expand Down Expand Up @@ -537,7 +539,7 @@ test('custom agent', (t, done) => {
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const dispatcher = new Client('http://localhost:' + server.address().port, {
Expand All @@ -549,7 +551,6 @@ test('custom agent', (t, done) => {
ok(true)
return oldDispatch.call(this, options, handler)
}
t.after(server.close.bind(server))
const body = await fetch(`http://localhost:${server.address().port}`, {
dispatcher
})
Expand All @@ -565,7 +566,7 @@ test('custom agent node fetch', (t, done) => {
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const dispatcher = new Client('http://localhost:' + server.address().port, {
Expand All @@ -577,7 +578,6 @@ test('custom agent node fetch', (t, done) => {
ok(true)
return oldDispatch.call(this, options, handler)
}
t.after(server.close.bind(server))
const body = await nodeFetch.fetch(`http://localhost:${server.address().port}`, {
dispatcher
})
Expand All @@ -591,7 +591,7 @@ test('error on redirect', (t, done) => {
res.statusCode = 302
res.end()
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const errorCause = await fetch(`http://localhost:${server.address().port}`, {
Expand All @@ -610,7 +610,7 @@ test('fetching with Request object - issue #1527', async (t) => {
res.end()
}).listen(0)

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

const body = JSON.stringify({ foo: 'bar' })
Expand Down Expand Up @@ -641,7 +641,7 @@ test('do not decode redirect body', (t, done) => {
res.setHeader('content-encoding', 'gzip')
res.end(gzipSync(JSON.stringify(obj)))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}/resource`)
Expand All @@ -661,7 +661,7 @@ test('decode non-redirect body with location header', (t, done) => {
res.setHeader('content-encoding', 'gzip')
res.end(gzipSync(JSON.stringify(obj)))
})
t.after(server.close.bind(server))
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}/resource`)
Expand All @@ -687,7 +687,7 @@ test('Receiving non-Latin1 headers', async (t) => {
res.end()
}).listen(0)

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

const url = `http://localhost:${server.address().port}`
Expand Down
3 changes: 2 additions & 1 deletion test/fetch/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { createServer } = require('http')
const { once } = require('events')
const { Blob } = require('buffer')
const { fetch, FormData } = require('../..')
const { closeServerAsPromise } = require('../utils/node-http')

// https://github.com/nodejs/undici/issues/1783
test('Content-Length is set when using a FormData body with fetch', async (t) => {
Expand All @@ -17,7 +18,7 @@ test('Content-Length is set when using a FormData body with fetch', async (t) =>
}).listen(0)

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

const fd = new FormData()
fd.set('file', new Blob(['hello world 👋'], { type: 'text/plain' }), 'readme.md')
Expand Down
7 changes: 4 additions & 3 deletions test/fetch/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const { test } = require('node:test')
const assert = require('node:assert')
const { tspl } = require('@matteo.collina/tspl')
const { fetch, Headers } = require('../..')
const { closeServerAsPromise } = require('../utils/node-http')

test('Can receive set-cookie headers from a server using fetch - issue #1262', async (t) => {
const server = createServer((req, res) => {
res.setHeader('set-cookie', 'name=value; Domain=example.com')
res.end()
}).listen(0)

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

const response = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -33,7 +34,7 @@ test('Can send cookies to a server with fetch - issue #1463', async (t) => {
res.end()
}).listen(0)

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

const headersInit = [
Expand All @@ -55,7 +56,7 @@ test('Cookie header is delimited with a semicolon rather than a comma - issue #1
res.end()
}).listen(0)

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

await fetch(`http://localhost:${server.address().port}`, {
Expand Down
5 changes: 3 additions & 2 deletions test/fetch/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { createServer } = require('http')
const { once } = require('events')
const { fetch } = require('../..')
const { createBrotliCompress, createGzip, createDeflate } = require('zlib')
const { closeServerAsPromise } = require('../utils/node-http')

test('content-encoding header is case-iNsENsITIve', async (t) => {
const contentCodings = 'GZiP, bR'
Expand All @@ -24,7 +25,7 @@ test('content-encoding header is case-iNsENsITIve', async (t) => {
brotli.end()
}).listen(0)

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

const response = await fetch(`http://localhost:${server.address().port}`)
Expand All @@ -50,7 +51,7 @@ test('response decompression according to content-encoding should be handled in
gzip.end()
}).listen(0)

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

const response = await fetch(`http://localhost:${server.address().port}`)
Expand Down
Loading

0 comments on commit cf74740

Please sign in to comment.