Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port abort-controller.js tests to node:test runner #2564

Merged
merged 9 commits into from
Jan 2, 2024
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: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@
"build:wasm": "node build/wasm.js --docker",
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"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",
"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:jest": "jest",
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
"test:node-test": "borp --coverage -p \"test/node-test/*.js\"",
"test:tdd": "tap test/*.js test/diagnostics-channel/*.js -w",
"test:typescript": "tsd && tsc --skipLibCheck test/imports/undici-import.ts",
"test:websocket": "borp --coverage -p \"test/websocket/*.js\"",
Expand Down
112 changes: 63 additions & 49 deletions test/abort-controller.js → test/node-test/abort-controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { AbortController: NPMAbortController } = require('abort-controller')
const { Client, errors } = require('..')
const { Client, errors } = require('../..')
const { createServer } = require('http')
const { createReadStream } = require('fs')
const { wrapWithAsyncIterable } = require('./utils/async-iterators')
const { wrapWithAsyncIterable } = require('../utils/async-iterators')
const { tspl } = require('@matteo.collina/tspl')

const controllers = [{
AbortControllerImpl: NPMAbortController,
Expand All @@ -19,87 +20,93 @@ if (global.AbortController) {
})
}
for (const { AbortControllerImpl, controllerName } of controllers) {
test(`Abort ${controllerName} before creating request`, (t) => {
t.plan(1)
test(`Abort ${controllerName} before creating request`, async (t) => {
const p = tspl(t, { plan: 1 })

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

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const abortController = new AbortControllerImpl()
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

abortController.abort()

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})

await p.completed
})

test(`Abort ${controllerName} before sending request (no body)`, (t) => {
t.plan(3)
test(`Abort ${controllerName} before sending request (no body)`, async (t) => {
const p = tspl(t, { plan: 3 })

let count = 0
const server = createServer((req, res) => {
if (count === 1) {
t.fail('The second request should never be executed')
p.fail('The second request should never be executed')
}
count += 1
res.end('hello')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const abortController = new AbortControllerImpl()
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
p.ifError(err)
const bufs = []
response.body.on('data', (buf) => {
bufs.push(buf)
})
response.body.on('end', () => {
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
})
})

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})

abortController.abort()
})

await p.completed
})

test(`Abort ${controllerName} while waiting response (no body)`, (t) => {
t.plan(1)
test(`Abort ${controllerName} while waiting response (no body)`, async (t) => {
const p = tspl(t, { plan: 1 })

const abortController = new AbortControllerImpl()
const server = createServer((req, res) => {
abortController.abort()
res.setHeader('content-type', 'text/plain')
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})

await p.completed
})

test(`Abort ${controllerName} while waiting response (write headers started) (no body)`, (t) => {
t.plan(1)
test(`Abort ${controllerName} while waiting response (write headers started) (no body)`, async (t) => {
const p = tspl(t, { plan: 1 })

const abortController = new AbortControllerImpl()
const server = createServer((req, res) => {
Expand All @@ -108,64 +115,69 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
abortController.abort()
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})

await p.completed
})

test(`Abort ${controllerName} while waiting response (write headers and write body started) (no body)`, (t) => {
t.plan(2)
test(`Abort ${controllerName} while waiting response (write headers and write body started) (no body)`, async (t) => {
const p = tspl(t, { plan: 2 })

const abortController = new AbortControllerImpl()
const server = createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.write('hello')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
t.error(err)
p.ifError(err)
response.body.on('data', () => {
abortController.abort()
})
response.body.on('error', err => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
})

await p.completed
})

function waitingWithBody (body, type) { // eslint-disable-line
test(`Abort ${controllerName} while waiting response (with body ${type})`, (t) => {
t.plan(1)
test(`Abort ${controllerName} while waiting response (with body ${type})`, async (t) => {
const p = tspl(t, { plan: 1 })

const abortController = new AbortControllerImpl()
const server = createServer((req, res) => {
abortController.abort()
res.setHeader('content-type', 'text/plain')
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
await p.completed
})
}

Expand All @@ -175,8 +187,8 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
waitingWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')

function writeHeadersStartedWithBody (body, type) { // eslint-disable-line
test(`Abort ${controllerName} while waiting response (write headers started) (with body ${type})`, (t) => {
t.plan(1)
test(`Abort ${controllerName} while waiting response (write headers started) (with body ${type})`, async (t) => {
const p = tspl(t, { plan: 1 })

const abortController = new AbortControllerImpl()
const server = createServer((req, res) => {
Expand All @@ -185,16 +197,17 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
abortController.abort()
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
await p.completed
})
}

Expand All @@ -204,30 +217,31 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
writeHeadersStartedWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')

function writeBodyStartedWithBody (body, type) { // eslint-disable-line
test(`Abort ${controllerName} while waiting response (write headers and write body started) (with body ${type})`, (t) => {
t.plan(2)
test(`Abort ${controllerName} while waiting response (write headers and write body started) (with body ${type})`, async (t) => {
const p = tspl(t, { plan: 2 })

const abortController = new AbortControllerImpl()
const server = createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.write('hello')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
t.error(err)
p.ifError(err)
response.body.on('data', () => {
abortController.abort()
})
response.body.on('error', err => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
})
await p.completed
})
}

Expand Down
Loading