-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: transform tests from tap to native test runner (#2719)
- Loading branch information
Showing
9 changed files
with
194 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.