1
1
'use strict'
2
2
3
- const t = require ( 'tap' )
4
- const test = t . test
3
+ const { test } = require ( 'node:test' )
5
4
const proxyquire = require ( 'proxyquire' )
6
5
const fs = require ( 'node:fs' )
7
6
const Readable = require ( 'node:stream' ) . Readable
8
7
const sget = require ( 'simple-get' ) . concat
9
8
const Fastify = require ( '..' )
10
9
11
- test ( 'should destroy stream when response is ended' , t => {
10
+ test ( 'should destroy stream when response is ended' , ( t , done ) => {
12
11
t . plan ( 4 )
13
12
const stream = require ( 'node:stream' )
14
13
const fastify = Fastify ( )
@@ -17,7 +16,7 @@ test('should destroy stream when response is ended', t => {
17
16
const reallyLongStream = new stream . Readable ( {
18
17
read : function ( ) { } ,
19
18
destroy : function ( err , callback ) {
20
- t . ok ( 'called' )
19
+ t . assert . ok ( 'called' )
21
20
callback ( err )
22
21
}
23
22
} )
@@ -26,23 +25,25 @@ test('should destroy stream when response is ended', t => {
26
25
} )
27
26
28
27
fastify . listen ( { port : 0 } , err => {
29
- t . error ( err )
30
- t . teardown ( ( ) => { fastify . close ( ) } )
28
+ t . assert . ifError ( err )
29
+ t . after ( ( ) => fastify . close ( ) )
31
30
32
31
sget ( `http://localhost:${ fastify . server . address ( ) . port } /error` , function ( err , response ) {
33
- t . error ( err )
34
- t . equal ( response . statusCode , 200 )
32
+ t . assert . ifError ( err )
33
+ t . assert . strictEqual ( response . statusCode , 200 )
34
+ done ( )
35
35
} )
36
36
} )
37
37
} )
38
38
39
- test ( 'should mark reply as sent before pumping the payload stream into response for async route handler' , t => {
39
+ test ( 'should mark reply as sent before pumping the payload stream into response for async route handler' , ( t , done ) => {
40
40
t . plan ( 3 )
41
+ t . after ( ( ) => fastify . close ( ) )
41
42
42
43
const handleRequest = proxyquire ( '../lib/handleRequest' , {
43
44
'./wrapThenable' : ( thenable , reply ) => {
44
45
thenable . then ( function ( payload ) {
45
- t . equal ( reply . sent , true )
46
+ t . assert . strictEqual ( reply . sent , true )
46
47
} )
47
48
}
48
49
} )
@@ -66,20 +67,20 @@ test('should mark reply as sent before pumping the payload stream into response
66
67
url : '/' ,
67
68
method : 'GET'
68
69
} , ( err , res ) => {
69
- t . error ( err )
70
- t . equal ( res . payload , fs . readFileSync ( __filename , 'utf8' ) )
71
- fastify . close ( )
70
+ t . assert . ifError ( err )
71
+ t . assert . strictEqual ( res . payload , fs . readFileSync ( __filename , 'utf8' ) )
72
+ done ( )
72
73
} )
73
74
} )
74
75
75
- test ( 'reply.send handles aborted requests' , t => {
76
+ test ( 'reply.send handles aborted requests' , ( t , done ) => {
76
77
t . plan ( 2 )
77
78
78
79
const spyLogger = {
79
80
level : 'error' ,
80
81
fatal : ( ) => { } ,
81
82
error : ( ) => {
82
- t . fail ( 'should not log an error' )
83
+ t . assert . fail ( 'should not log an error' )
83
84
} ,
84
85
warn : ( ) => { } ,
85
86
info : ( ) => { } ,
@@ -103,31 +104,31 @@ test('reply.send handles aborted requests', t => {
103
104
} )
104
105
105
106
fastify . listen ( { port : 0 } , err => {
106
- t . error ( err )
107
- t . teardown ( ( ) => { fastify . close ( ) } )
107
+ t . assert . ifError ( err )
108
+ t . after ( ( ) => fastify . close ( ) )
108
109
109
110
const port = fastify . server . address ( ) . port
110
111
const http = require ( 'node:http' )
111
112
const req = http . get ( `http://localhost:${ port } ` )
112
113
. on ( 'error' , ( err ) => {
113
- t . equal ( err . code , 'ECONNRESET' )
114
- fastify . close ( )
114
+ t . assert . strictEqual ( err . code , 'ECONNRESET' )
115
+ done ( )
115
116
} )
116
117
117
118
setTimeout ( ( ) => {
118
- req . abort ( )
119
+ req . destroy ( )
119
120
} , 1 )
120
121
} )
121
122
} )
122
123
123
- test ( 'request terminated should not crash fastify' , t => {
124
+ test ( 'request terminated should not crash fastify' , ( t , done ) => {
124
125
t . plan ( 10 )
125
126
126
127
const spyLogger = {
127
128
level : 'error' ,
128
129
fatal : ( ) => { } ,
129
130
error : ( ) => {
130
- t . fail ( 'should not log an error' )
131
+ t . assert . fail ( 'should not log an error' )
131
132
} ,
132
133
warn : ( ) => { } ,
133
134
info : ( ) => { } ,
@@ -156,18 +157,18 @@ test('request terminated should not crash fastify', t => {
156
157
} )
157
158
158
159
fastify . listen ( { port : 0 } , err => {
159
- t . error ( err )
160
- t . teardown ( ( ) => { fastify . close ( ) } )
160
+ t . assert . ifError ( err )
161
+ t . after ( ( ) => fastify . close ( ) )
161
162
162
163
const port = fastify . server . address ( ) . port
163
164
const http = require ( 'node:http' )
164
165
const req = http . get ( `http://localhost:${ port } ` , function ( res ) {
165
166
const { statusCode, headers } = res
166
- t . equal ( statusCode , 200 )
167
- t . equal ( headers [ 'content-type' ] , 'text/html; charset=utf-8' )
168
- t . equal ( headers [ 'transfer-encoding' ] , 'chunked' )
167
+ t . assert . strictEqual ( statusCode , 200 )
168
+ t . assert . strictEqual ( headers [ 'content-type' ] , 'text/html; charset=utf-8' )
169
+ t . assert . strictEqual ( headers [ 'transfer-encoding' ] , 'chunked' )
169
170
res . on ( 'data' , function ( chunk ) {
170
- t . equal ( chunk . toString ( ) , '<h1>HTML</h1>' )
171
+ t . assert . strictEqual ( chunk . toString ( ) , '<h1>HTML</h1>' )
171
172
} )
172
173
173
174
setTimeout ( ( ) => {
@@ -176,16 +177,17 @@ test('request terminated should not crash fastify', t => {
176
177
// the server is not crash, we can connect it
177
178
http . get ( `http://localhost:${ port } ` , function ( res ) {
178
179
const { statusCode, headers } = res
179
- t . equal ( statusCode , 200 )
180
- t . equal ( headers [ 'content-type' ] , 'text/html; charset=utf-8' )
181
- t . equal ( headers [ 'transfer-encoding' ] , 'chunked' )
180
+ t . assert . strictEqual ( statusCode , 200 )
181
+ t . assert . strictEqual ( headers [ 'content-type' ] , 'text/html; charset=utf-8' )
182
+ t . assert . strictEqual ( headers [ 'transfer-encoding' ] , 'chunked' )
182
183
let payload = ''
183
184
res . on ( 'data' , function ( chunk ) {
184
185
payload += chunk . toString ( )
185
186
} )
186
187
res . on ( 'end' , function ( ) {
187
- t . equal ( payload , '<h1>HTML</h1><h1>should display on second stream</h1>' )
188
- t . pass ( 'should end properly' )
188
+ t . assert . strictEqual ( payload , '<h1>HTML</h1><h1>should display on second stream</h1>' )
189
+ t . assert . ok ( 'should end properly' )
190
+ done ( )
189
191
} )
190
192
} )
191
193
} , 1 )
0 commit comments