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 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
113 changes: 64 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,13 @@
'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 { ttype } = require('../utils/node-test')

const controllers = [{
AbortControllerImpl: NPMAbortController,
Expand All @@ -19,87 +21,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)
ttype(p, err, 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)
ttype(p, err, 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)
ttype(p, err, 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 +116,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)
ttype(p, err, 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)
ttype(p, err, 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)
ttype(p, err, errors.RequestAbortedError)
})
})
await p.completed
})
}

Expand All @@ -175,8 +188,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 +198,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)
ttype(p, err, errors.RequestAbortedError)
})
})
await p.completed
})
}

Expand All @@ -204,30 +218,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)
ttype(p, err, errors.RequestAbortedError)
})
})
})
await p.completed
})
}

Expand Down
60 changes: 60 additions & 0 deletions test/utils/node-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* A port of tap's `t.type` that can be used with `node:assert`
* https://github.com/tapjs/tapjs/blob/511019b2ac0fa014370154c3a341a0e632f50b19/src/asserts/src/index.ts#L199
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the license of that file if copied over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, 88d2c96

function ttype (plan, obj, klass) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not inject the plan object and rather make ttype return a boolean value so we are not coupling this utils to specific tools; otherwise will be harder to decuple or maintain. wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done a874a48

const name =
typeof klass === 'function'
? klass.name || '(anonymous constructor)'
: klass

if (obj === klass) {
return plan.ok(1)
}

const tof = typeof obj
const type =
!obj && tof === 'object'
? 'null'
// treat as object, but not Object
// t.type(() => {}, Function)
: tof === 'function' &&
typeof klass === 'function' &&
klass !== Object
? 'object'
: tof

if (
(type === 'number' && klass === Number) ||
(type === 'string' && klass === String) ||
(type === 'bigint' && klass === BigInt) ||
(klass === 'array' && Array.isArray(obj)) ||
(type === 'symbol' && klass === Symbol)
) {
return plan.ok(1)
}

// simplest case, it literally is the same thing
if (type === 'object' && klass !== 'object') {
if (typeof klass === 'function') {
return plan.ok(obj instanceof klass)
}

// check prototype chain for name
// at this point, we already know klass is not a function
// if the klass specified is an obj in the proto chain, pass
// if the name specified is the name of a ctor in the chain, pass
for (let p = obj; p; p = Object.getPrototypeOf(p)) {
const ctor = p.constructor && p.constructor.name
if (p === klass || ctor === name) {
return plan.ok(1)
}
}
}

return plan.strictEqual(type, name)
}

module.exports = {
ttype
}