Skip to content

Commit

Permalink
chore: transform tests from tap to native test runner (#2719)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Feb 12, 2024
1 parent 4e69afd commit 42adfca
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 157 deletions.
92 changes: 49 additions & 43 deletions test/client-head-reset-override.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,68 @@
'use strict'

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

test('override HEAD reset', (t) => {
test('override HEAD reset', async (t) => {
t = tspl(t, { plan: 4 })

const expected = 'testing123'
const server = createServer((req, res) => {
if (req.method === 'GET') {
res.write(expected)
}
res.end()
})
t.teardown(server.close.bind(server))
}).listen(0)

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

let done
client.on('disconnect', () => {
if (!done) {
t.fail()
}
})
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.close())

client.request({
path: '/',
method: 'HEAD',
reset: false
}, (err, res) => {
t.error(err)
res.body.resume()
})
let done
client.on('disconnect', () => {
if (!done) {
t.fail()
}
})

client.request({
path: '/',
method: 'HEAD',
reset: false
}, (err, res) => {
t.error(err)
res.body.resume()
})
client.request({
path: '/',
method: 'HEAD',
reset: false
}, (err, res) => {
t.ifError(err)
res.body.resume()
})

client.request({
path: '/',
method: 'GET',
reset: false
}, (err, res) => {
t.error(err)
let str = ''
res.body.on('data', (data) => {
str += data
}).on('end', () => {
t.same(str, expected)
done = true
t.end()
})
client.request({
path: '/',
method: 'HEAD',
reset: false
}, (err, res) => {
t.ifError(err)
res.body.resume()
})

client.request({
path: '/',
method: 'GET',
reset: false
}, (err, res) => {
t.ifError(err)
let str = ''
res.body.on('data', (data) => {
str += data
}).on('end', () => {
t.strictEqual(str, expected)
done = true
t.end()
})
})

await t.completed
})
98 changes: 54 additions & 44 deletions test/client-post.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,83 @@
'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 { Blob } = require('node:buffer')

test('request post blob', { skip: !Blob }, (t) => {
t.plan(4)
test('request post blob', { skip: !Blob }, async (t) => {
t = tspl(t, { plan: 3 })

const server = createServer(async (req, res) => {
t.equal(req.headers['content-type'], 'application/json')
t.strictEqual(req.headers['content-type'], 'application/json')
let str = ''
for await (const chunk of req) {
str += chunk
}
t.equal(str, 'asd')
t.strictEqual(str, 'asd')
res.end()
})
t.teardown(server.close.bind(server))

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

client.request({
path: '/',
method: 'GET',
body: new Blob(['asd'], {
type: 'application/json'
})
}, (err, data) => {
t.error(err)
data.body.resume().on('end', () => {
t.ok(true, 'pass')
})
after(server.close.bind(server))

server.listen(0)

await once(server, 'listening')

const client = new Client(`http://localhost:${server.address().port}`)
after(client.destroy.bind(client))

client.request({
path: '/',
method: 'GET',
body: new Blob(['asd'], {
type: 'application/json'
})
}, (err, data) => {
t.ifError(err)
data.body.resume().on('end', () => {
t.end()
})
})
await t.completed
})

test('request post arrayBuffer', { skip: !Blob }, (t) => {
t.plan(3)
test('request post arrayBuffer', { skip: !Blob }, async (t) => {
t = tspl(t, { plan: 3 })

const server = createServer(async (req, res) => {
let str = ''
for await (const chunk of req) {
str += chunk
}
t.equal(str, 'asd')
t.strictEqual(str, 'asd')
res.end()
})
t.teardown(server.close.bind(server))

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

const buf = Buffer.from('asd')
const dst = new ArrayBuffer(buf.byteLength)
buf.copy(new Uint8Array(dst))

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

after(() => server.close())

server.listen(0)

await once(server, 'listening')

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

const buf = Buffer.from('asd')
const dst = new ArrayBuffer(buf.byteLength)
buf.copy(new Uint8Array(dst))

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

await t.completed
})
57 changes: 30 additions & 27 deletions test/client-reconnect.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
'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 FakeTimers = require('@sinonjs/fake-timers')
const timers = require('../lib/timers')

test('multiple reconnect', (t) => {
t.plan(5)
test('multiple reconnect', async (t) => {
t = tspl(t, { plan: 5 })

let n = 0
const clock = FakeTimers.install()
t.teardown(clock.uninstall.bind(clock))
after(() => clock.uninstall())

const orgTimers = { ...timers }
Object.assign(timers, { setTimeout, clearTimeout })
t.teardown(() => {
after(() => {
Object.assign(timers, orgTimers)
})

const server = createServer((req, res) => {
n === 0 ? res.destroy() : res.end('ok')
})
t.teardown(server.close.bind(server))
after(() => server.close())

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

client.request({ path: '/', method: 'GET' }, (err, data) => {
t.ok(err)
t.equal(err.code, 'UND_ERR_SOCKET')
})

client.request({ path: '/', method: 'GET' }, (err, data) => {
t.error(err)
data.body
.resume()
.on('end', () => {
t.ok(true, 'pass')
})
})
client.request({ path: '/', method: 'GET' }, (err, data) => {
t.ok(err)
t.strictEqual(err.code, 'UND_ERR_SOCKET')
})

client.on('disconnect', () => {
if (++n === 1) {
client.request({ path: '/', method: 'GET' }, (err, data) => {
t.ifError(err)
data.body
.resume()
.on('end', () => {
t.ok(true, 'pass')
}
process.nextTick(() => {
clock.tick(1000)
})
})

client.on('disconnect', () => {
if (++n === 1) {
t.ok(true, 'pass')
}
process.nextTick(() => {
clock.tick(1000)
})
})
await t.completed
})
32 changes: 19 additions & 13 deletions test/client-unref.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,41 @@
const { Worker, isMainThread, workerData } = require('node:worker_threads')

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

tap.test('client automatically closes itself when idle', t => {
t.plan(1)
test('client automatically closes itself when idle', async t => {
t = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
res.end()
})
t.teardown(server.close.bind(server))
after(server.close.bind(server))
server.keepAliveTimeout = 9999

server.listen(0, () => {
const url = `http://localhost:${server.address().port}`
const worker = new Worker(__filename, { workerData: { url } })
worker.on('exit', code => {
t.equal(code, 0)
})
server.listen(0)

await once(server, 'listening')
const url = `http://localhost:${server.address().port}`
const worker = new Worker(__filename, { workerData: { url } })
worker.on('exit', code => {
t.strictEqual(code, 0)
})
await t.completed
})

tap.test('client automatically closes itself if the server is not there', t => {
t.plan(1)
test('client automatically closes itself if the server is not there', async t => {
t = tspl(t, { plan: 1 })

const url = 'http://localhost:4242' // hopefully empty port
const worker = new Worker(__filename, { workerData: { url } })
worker.on('exit', code => {
t.equal(code, 0)
t.strictEqual(code, 0)
})

await t.completed
})
} else {
const { Client } = require('..')
Expand Down
Loading

0 comments on commit 42adfca

Please sign in to comment.