Skip to content

Commit

Permalink
chore: migrate a batch of tests to node test runner (nodejs#2738)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored and crysmags committed Feb 21, 2024
1 parent fe81224 commit 0255c94
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 252 deletions.
7 changes: 4 additions & 3 deletions test/invalid-headers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

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

test('invalid headers', (t) => {
t.plan(10)
t = tspl(t, { plan: 10 })

const client = new Client('http://localhost:3000')
t.teardown(client.destroy.bind(client))
after(() => client.close())
client.request({
path: '/',
method: 'GET',
Expand Down
13 changes: 9 additions & 4 deletions test/issue-2078.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
'use strict'

const { test } = require('tap')
const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { MockAgent, getGlobalDispatcher, setGlobalDispatcher, fetch } = require('..')

test('MockPool.reply headers are an object, not an array - issue #2078', async (t) => {
t = tspl(t, { plan: 1 })

const global = getGlobalDispatcher()
const mockAgent = new MockAgent()
const mockPool = mockAgent.get('http://localhost')

t.teardown(() => setGlobalDispatcher(global))
after(() => setGlobalDispatcher(global))
setGlobalDispatcher(mockAgent)

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply((options) => {
t.ok(!Array.isArray(options.headers))
t.strictEqual(Array.isArray(options.headers), false)

return { statusCode: 200 }
})

await t.resolves(fetch('http://localhost/foo'))
await fetch('http://localhost/foo')

await t.completed
})
48 changes: 26 additions & 22 deletions test/issue-803.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict'

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

test('https://github.com/nodejs/undici/issues/803', (t) => {
t.plan(2)
test('https://github.com/nodejs/undici/issues/803', async (t) => {
t = tspl(t, { plan: 2 })

const SIZE = 5900373096

Expand All @@ -23,25 +25,27 @@ test('https://github.com/nodejs/undici/issues/803', (t) => {

res.end()
})
t.teardown(server.close.bind(server))

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

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.error(err)

let pos = 0
data.body.on('data', (buf) => {
pos += buf.length
})
data.body.on('end', () => {
t.equal(pos, SIZE)
})
after(() => server.close())

server.listen(0)

await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.close())

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.ifError(err)

let pos = 0
data.body.on('data', (buf) => {
pos += buf.length
})
data.body.on('end', () => {
t.strictEqual(pos, SIZE)
})
})
await t.completed
})
178 changes: 95 additions & 83 deletions test/issue-810.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict'

const { test } = require('tap')
const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { once } = require('node:events')
const { Client, errors } = require('..')
const net = require('node:net')

test('https://github.com/mcollina/undici/issues/810', (t) => {
t.plan(3)
test('https://github.com/mcollina/undici/issues/810', async (t) => {
t = tspl(t, { plan: 3 })

let x = 0
const server = net.createServer(socket => {
Expand All @@ -19,117 +21,127 @@ test('https://github.com/mcollina/undici/issues/810', (t) => {
socket.write('\r\n')
}
})
t.teardown(server.close.bind(server))

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

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.error(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
}).on('error', err => (
t.ok(err instanceof errors.HTTPParserError)
))
})
client.request({
path: '/',
method: 'GET'
}, (err, data) => {
after(() => server.close())

server.listen(0)

await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 2 })
after(() => client.close())

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.ifError(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
}).on('error', err => (
t.ok(err instanceof errors.HTTPParserError)
})
))
})
client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.ok(err instanceof errors.HTTPParserError)
})
await t.completed
})

test('https://github.com/mcollina/undici/issues/810 no pipelining', (t) => {
t.plan(2)
test('https://github.com/mcollina/undici/issues/810 no pipelining', async (t) => {
t = tspl(t, { plan: 2 })

const server = net.createServer(socket => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 1\r\n\r\n')
socket.write('11111\r\n')
})
t.teardown(server.close.bind(server))

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

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.error(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
})
after(() => server.close())

server.listen(0)

await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.ifError(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
})
})
await t.completed
})

test('https://github.com/mcollina/undici/issues/810 pipelining', (t) => {
t.plan(2)
test('https://github.com/mcollina/undici/issues/810 pipelining', async (t) => {
t = tspl(t, { plan: 2 })

const server = net.createServer(socket => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 1\r\n\r\n')
socket.write('11111\r\n')
})
t.teardown(server.close.bind(server))

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

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.error(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
})
after(() => server.close())

server.listen(0)

await once(server, 'listening')

const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true })
after(() => client.close())

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.ifError(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
})
})
await t.completed
})

test('https://github.com/mcollina/undici/issues/810 pipelining 2', (t) => {
t.plan(4)
test('https://github.com/mcollina/undici/issues/810 pipelining 2', async (t) => {
t = tspl(t, { plan: 4 })

const server = net.createServer(socket => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 1\r\n\r\n')
socket.write('11111\r\n')
})
t.teardown(server.close.bind(server))

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

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.error(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
})
})
after(() => server.close())

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.equal(err.code, 'HPE_INVALID_CONSTANT')
t.ok(err instanceof errors.HTTPParserError)
server.listen(0)

await once(server, 'listening')

const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true })
after(() => client.close())

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.ifError(err)
data.body.resume().on('end', () => {
// t.fail() FIX: Should fail.
t.ok(true, 'pass')
})
})

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.strictEqual(err.code, 'HPE_INVALID_CONSTANT')
t.ok(err instanceof errors.HTTPParserError)
})
await t.completed
})

0 comments on commit 0255c94

Please sign in to comment.