Skip to content

Commit 4cc822c

Browse files
Uzlopakcrysmags
authored andcommitted
chore: migrate a batch of tests to node test runner (nodejs#2741)
1 parent 077a24f commit 4cc822c

File tree

8 files changed

+363
-304
lines changed

8 files changed

+363
-304
lines changed

test/content-length.js

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
45
const { Client, errors } = require('..')
56
const { createServer } = require('node:http')
67
const { Readable } = require('node:stream')
78
const { maybeWrapStream, consts } = require('./utils/async-iterators')
89

9-
test('request invalid content-length', (t) => {
10-
t.plan(7)
10+
test('request invalid content-length', async (t) => {
11+
t = tspl(t, { plan: 7 })
1112

1213
const server = createServer((req, res) => {
1314
res.end()
1415
})
15-
t.teardown(server.close.bind(server))
16+
after(() => server.close())
1617
server.listen(0, () => {
1718
const client = new Client(`http://localhost:${server.address().port}`)
18-
t.teardown(client.destroy.bind(client))
19+
after(() => client.close())
1920

2021
client.request({
2122
path: '/',
@@ -94,19 +95,21 @@ test('request invalid content-length', (t) => {
9495
t.ok(err instanceof errors.RequestContentLengthMismatchError)
9596
})
9697
})
98+
99+
await t.completed
97100
})
98101

99102
function invalidContentLength (bodyType) {
100-
test(`request streaming ${bodyType} invalid content-length`, (t) => {
101-
t.plan(4)
103+
test(`request streaming ${bodyType} invalid content-length`, async (t) => {
104+
t = tspl(t, { plan: 4 })
102105

103106
const server = createServer((req, res) => {
104107
res.end()
105108
})
106-
t.teardown(server.close.bind(server))
109+
after(() => server.close())
107110
server.listen(0, () => {
108111
const client = new Client(`http://localhost:${server.address().port}`)
109-
t.teardown(client.destroy.bind(client))
112+
after(() => client.close())
110113

111114
client.once('disconnect', () => {
112115
t.ok(true, 'pass')
@@ -151,23 +154,24 @@ function invalidContentLength (bodyType) {
151154
t.ok(err instanceof errors.RequestContentLengthMismatchError)
152155
})
153156
})
157+
await t.completed
154158
})
155159
}
156160

157161
invalidContentLength(consts.STREAM)
158162
invalidContentLength(consts.ASYNC_ITERATOR)
159163

160164
function zeroContentLength (bodyType) {
161-
test(`request ${bodyType} streaming data when content-length=0`, (t) => {
162-
t.plan(1)
165+
test(`request ${bodyType} streaming data when content-length=0`, async (t) => {
166+
t = tspl(t, { plan: 1 })
163167

164168
const server = createServer((req, res) => {
165169
res.end()
166170
})
167-
t.teardown(server.close.bind(server))
171+
after(() => server.close())
168172
server.listen(0, () => {
169173
const client = new Client(`http://localhost:${server.address().port}`)
170-
t.teardown(client.destroy.bind(client))
174+
after(() => client.close())
171175

172176
client.request({
173177
path: '/',
@@ -187,22 +191,23 @@ function zeroContentLength (bodyType) {
187191
t.ok(err instanceof errors.RequestContentLengthMismatchError)
188192
})
189193
})
194+
await t.completed
190195
})
191196
}
192197

193198
zeroContentLength(consts.STREAM)
194199
zeroContentLength(consts.ASYNC_ITERATOR)
195200

196-
test('request streaming no body data when content-length=0', (t) => {
197-
t.plan(2)
201+
test('request streaming no body data when content-length=0', async (t) => {
202+
t = tspl(t, { plan: 2 })
198203

199204
const server = createServer((req, res) => {
200205
req.pipe(res)
201206
})
202-
t.teardown(server.close.bind(server))
207+
after(() => server.close())
203208
server.listen(0, () => {
204209
const client = new Client(`http://localhost:${server.address().port}`)
205-
t.teardown(client.destroy.bind(client))
210+
after(() => client.close())
206211

207212
client.request({
208213
path: '/',
@@ -211,7 +216,7 @@ test('request streaming no body data when content-length=0', (t) => {
211216
'content-length': 0
212217
}
213218
}, (err, data) => {
214-
t.error(err)
219+
t.ifError(err)
215220
data.body
216221
.on('data', () => {
217222
t.fail()
@@ -221,86 +226,92 @@ test('request streaming no body data when content-length=0', (t) => {
221226
})
222227
})
223228
})
229+
230+
await t.completed
224231
})
225232

226-
test('response invalid content length with close', (t) => {
227-
t.plan(3)
233+
test('response invalid content length with close', async (t) => {
234+
t = tspl(t, { plan: 3 })
228235

229236
const server = createServer((req, res) => {
230237
res.writeHead(200, {
231238
'content-length': 10
232239
})
233240
res.end('123')
234241
})
235-
t.teardown(server.close.bind(server))
242+
after(() => server.close())
236243
server.listen(0, () => {
237244
const client = new Client(`http://localhost:${server.address().port}`, {
238245
pipelining: 0
239246
})
240-
t.teardown(client.destroy.bind(client))
247+
after(() => client.close())
241248

242249
client.on('disconnect', (origin, client, err) => {
243-
t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
250+
t.strictEqual(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
244251
})
245252

246253
client.request({
247254
path: '/',
248255
method: 'GET'
249256
}, (err, data) => {
250-
t.error(err)
257+
t.ifError(err)
251258
data.body
252259
.on('end', () => {
253260
t.fail()
254261
})
255262
.on('error', (err) => {
256-
t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
263+
t.strictEqual(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
257264
})
258265
.resume()
259266
})
260267
})
268+
269+
await t.completed
261270
})
262271

263-
test('request streaming with Readable.from(buf)', (t) => {
272+
test('request streaming with Readable.from(buf)', async (t) => {
264273
const server = createServer((req, res) => {
265274
req.pipe(res)
266275
})
267-
t.teardown(server.close.bind(server))
276+
after(() => server.close())
268277
server.listen(0, () => {
269278
const client = new Client(`http://localhost:${server.address().port}`)
270-
t.teardown(client.destroy.bind(client))
279+
after(() => client.close())
271280

272281
client.request({
273282
path: '/',
274283
method: 'PUT',
275284
body: Readable.from(Buffer.from('hello'))
276285
}, (err, data) => {
277286
const chunks = []
278-
t.error(err)
287+
t.ifError(err)
279288
data.body
280289
.on('data', (chunk) => {
281290
chunks.push(chunk)
282291
})
283292
.on('end', () => {
284-
t.equal(Buffer.concat(chunks).toString(), 'hello')
293+
t.strictEqual(Buffer.concat(chunks).toString(), 'hello')
285294
t.ok(true, 'pass')
286295
t.end()
287296
})
288297
})
289298
})
299+
300+
await t.completed
290301
})
291302

292-
test('request DELETE, content-length=0, with body', (t) => {
293-
t.plan(5)
303+
test('request DELETE, content-length=0, with body', async (t) => {
304+
t = tspl(t, { plan: 5 })
294305
const server = createServer((req, res) => {
295306
res.end()
296307
})
297308
server.on('request', (req, res) => {
298-
t.equal(req.headers['content-length'], undefined)
309+
t.strictEqual(req.headers['content-length'], undefined)
299310
})
300-
t.teardown(server.close.bind(server))
311+
after(() => server.close())
301312
server.listen(0, () => {
302313
const client = new Client(`http://localhost:${server.address().port}`)
303-
t.teardown(client.destroy.bind(client))
314+
after(() => client.close())
304315

305316
client.request({
306317
path: '/',
@@ -325,38 +336,41 @@ test('request DELETE, content-length=0, with body', (t) => {
325336
'content-length': 0
326337
}
327338
}, (err, resp) => {
328-
t.equal(resp.headers['content-length'], '0')
329-
t.error(err)
339+
t.strictEqual(resp.headers['content-length'], '0')
340+
t.ifError(err)
330341
})
331342

332343
client.on('disconnect', () => {
333344
t.ok(true, 'pass')
334345
})
335346
})
347+
348+
await t.completed
336349
})
337350

338-
test('content-length shouldSendContentLength=false', (t) => {
339-
t.plan(15)
351+
test('content-length shouldSendContentLength=false', async (t) => {
352+
t = tspl(t, { plan: 15 })
353+
340354
const server = createServer((req, res) => {
341355
res.end()
342356
})
343357
server.on('request', (req, res) => {
344358
switch (req.url) {
345359
case '/put0':
346-
t.equal(req.headers['content-length'], '0')
360+
t.strictEqual(req.headers['content-length'], '0')
347361
break
348362
case '/head':
349-
t.equal(req.headers['content-length'], undefined)
363+
t.strictEqual(req.headers['content-length'], undefined)
350364
break
351365
case '/get':
352-
t.equal(req.headers['content-length'], undefined)
366+
t.strictEqual(req.headers['content-length'], undefined)
353367
break
354368
}
355369
})
356-
t.teardown(server.close.bind(server))
370+
after(() => server.close())
357371
server.listen(0, () => {
358372
const client = new Client(`http://localhost:${server.address().port}`)
359-
t.teardown(client.destroy.bind(client))
373+
after(() => client.close())
360374

361375
client.request({
362376
path: '/put0',
@@ -365,8 +379,8 @@ test('content-length shouldSendContentLength=false', (t) => {
365379
'content-length': 0
366380
}
367381
}, (err, resp) => {
368-
t.equal(resp.headers['content-length'], '0')
369-
t.error(err)
382+
t.strictEqual(resp.headers['content-length'], '0')
383+
t.ifError(err)
370384
})
371385

372386
client.request({
@@ -376,8 +390,8 @@ test('content-length shouldSendContentLength=false', (t) => {
376390
'content-length': 10
377391
}
378392
}, (err, resp) => {
379-
t.equal(resp.headers['content-length'], undefined)
380-
t.error(err)
393+
t.strictEqual(resp.headers['content-length'], undefined)
394+
t.ifError(err)
381395
})
382396

383397
client.request({
@@ -387,7 +401,7 @@ test('content-length shouldSendContentLength=false', (t) => {
387401
'content-length': 0
388402
}
389403
}, (err) => {
390-
t.error(err)
404+
t.ifError(err)
391405
})
392406

393407
client.request({
@@ -403,7 +417,7 @@ test('content-length shouldSendContentLength=false', (t) => {
403417
}
404418
})
405419
}, (err) => {
406-
t.error(err)
420+
t.ifError(err)
407421
})
408422

409423
client.request({
@@ -419,7 +433,7 @@ test('content-length shouldSendContentLength=false', (t) => {
419433
}
420434
})
421435
}, (err) => {
422-
t.error(err)
436+
t.ifError(err)
423437
})
424438

425439
client.request({
@@ -435,11 +449,13 @@ test('content-length shouldSendContentLength=false', (t) => {
435449
}
436450
})
437451
}, (err) => {
438-
t.error(err)
452+
t.ifError(err)
439453
})
440454

441455
client.on('disconnect', () => {
442456
t.ok(true, 'pass')
443457
})
444458
})
459+
460+
await t.completed
445461
})

test/esm-wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
await import('./utils/esm-wrapper.mjs')
66
} catch (e) {
77
if (e.message === 'Not supported') {
8-
require('tap') // shows skipped
8+
require('node:test') // shows skipped
99
return
1010
}
1111
console.error(e.stack)

0 commit comments

Comments
 (0)