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-event-emitter.js tests to node:test runnner #2565

Merged
merged 8 commits into from
Jan 3, 2024
Merged
Changes from all 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
115 changes: 64 additions & 51 deletions test/abort-event-emitter.js → test/node-test/abort-event-emitter.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,65 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const EventEmitter = require('events')
const { Client, errors } = require('..')
const { Client, errors } = require('../..')
const { createServer } = require('http')
const { createReadStream } = require('fs')
const { Readable } = require('stream')
const { wrapWithAsyncIterable } = require('./utils/async-iterators')
const { tspl } = require('@matteo.collina/tspl')
const { wrapWithAsyncIterable } = require('../utils/async-iterators')

test('Abort before sending request (no body)', (t) => {
t.plan(4)
test('Abort before sending request (no body)', async (t) => {
const p = tspl(t, { plan: 4 })

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 ee = new EventEmitter()
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'))
})
})

const body = new Readable({ read () { } })
body.on('error', (err) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
client.request({
path: '/',
method: 'GET',
signal: ee,
body
}, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})

ee.emit('abort')
})

await p.completed
})

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

let count = 0
const server = createServer((req, res) => {
Expand All @@ -67,21 +70,21 @@ test('Abort before sending request (no body) async iterator', (t) => {
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 ee = new EventEmitter()
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'))
})
})

Expand All @@ -92,36 +95,40 @@ test('Abort before sending request (no body) async iterator', (t) => {
signal: ee,
body
}, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})

ee.emit('abort')
})

await p.completed
})

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

const ee = new EventEmitter()
const server = createServer((req, res) => {
ee.emit('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: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})

await p.completed
})

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

const ee = new EventEmitter()
const server = createServer((req, res) => {
Expand All @@ -130,64 +137,68 @@ test('Abort while waiting response (write headers started) (no body)', (t) => {
ee.emit('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: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})

await p.completed
})

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

const ee = new EventEmitter()
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: ee }, (err, response) => {
t.error(err)
p.ifError(err)
response.body.on('data', () => {
ee.emit('abort')
})
response.body.on('error', err => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
})
await p.completed
})

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

const ee = new EventEmitter()
const server = createServer((req, res) => {
ee.emit('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: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
await p.completed
})
}

Expand All @@ -197,8 +208,8 @@ waitingWithBody(new Uint8Array([42]), 'Uint8Array')
waitingWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')

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

const ee = new EventEmitter()
const server = createServer((req, res) => {
Expand All @@ -207,16 +218,17 @@ function writeHeadersStartedWithBody (body, type) {
ee.emit('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: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
await p.completed
})
}

Expand All @@ -226,30 +238,31 @@ writeHeadersStartedWithBody(new Uint8Array([42]), 'Uint8Array')
writeHeadersStartedWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')

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

const ee = new EventEmitter()
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: ee }, (err, response) => {
t.error(err)
p.ifError(err)
response.body.on('data', () => {
ee.emit('abort')
})
response.body.on('error', err => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})
})
await p.completed
})
}

Expand Down