1
1
'use strict'
2
2
3
- const { test } = require ( 'tap ' )
3
+ const { test } = require ( 'node:test ' )
4
4
const plugin = require ( '..' )
5
5
6
6
const Fastify = require ( 'fastify' )
7
+ const { setTimeout : sleep } = require ( 'node:timers/promises' )
7
8
8
9
test ( 'cache property gets added to instance' , async ( t ) => {
9
10
t . plan ( 2 )
@@ -12,8 +13,8 @@ test('cache property gets added to instance', async (t) => {
12
13
await fastify . register ( plugin )
13
14
await fastify . ready ( )
14
15
15
- t . ok ( fastify . cache )
16
- t . ok ( fastify . cache . set )
16
+ t . assert . ok ( fastify . cache )
17
+ t . assert . ok ( fastify . cache . set )
17
18
} )
18
19
19
20
test ( 'cache is usable' , async ( t ) => {
@@ -22,13 +23,13 @@ test('cache is usable', async (t) => {
22
23
const fastify = Fastify ( )
23
24
await fastify . register ( async ( instance , options ) => {
24
25
instance . addHook ( 'onRequest' , async function ( req , reply ) {
25
- t . notOk ( instance [ Symbol . for ( 'fastify-caching.registered' ) ] )
26
+ t . assert . ifError ( instance [ Symbol . for ( 'fastify-caching.registered' ) ] )
26
27
} )
27
28
} )
28
29
await fastify . register ( plugin )
29
30
30
31
fastify . addHook ( 'onRequest' , async function ( req , reply ) {
31
- t . equal ( this [ Symbol . for ( 'fastify-caching.registered' ) ] , true )
32
+ t . assert . strictEqual ( this [ Symbol . for ( 'fastify-caching.registered' ) ] , true )
32
33
} )
33
34
34
35
fastify . get ( '/one' , ( req , reply ) => {
@@ -40,9 +41,8 @@ test('cache is usable', async (t) => {
40
41
41
42
fastify . get ( '/two' , ( req , reply ) => {
42
43
fastify . cache . get ( 'one' , ( err , obj ) => {
43
- t . error ( err )
44
- t . same ( obj . item , { one : true } )
45
-
44
+ t . assert . ifError ( err )
45
+ t . assert . strictEqual ( obj . item , { one : true } )
46
46
reply . send ( )
47
47
} )
48
48
} )
@@ -72,13 +72,13 @@ test('cache is usable with function as plugin default options input', async (t)
72
72
const fastify = Fastify ( )
73
73
await fastify . register ( async ( instance , options ) => {
74
74
instance . addHook ( 'onRequest' , async function ( req , reply ) {
75
- t . notOk ( instance [ Symbol . for ( 'fastify-caching.registered' ) ] )
75
+ t . assert . failure ( instance [ Symbol . for ( 'fastify-caching.registered' ) ] )
76
76
} )
77
77
} )
78
- await fastify . register ( plugin , ( ) => ( ) => { } )
78
+ await fastify . register ( plugin , ( ) => ( ) => { } )
79
79
80
80
fastify . addHook ( 'onRequest' , async function ( req , reply ) {
81
- t . equal ( this [ Symbol . for ( 'fastify-caching.registered' ) ] , true )
81
+ t . assert . strictEqual ( this [ Symbol . for ( 'fastify-caching.registered' ) ] , true )
82
82
} )
83
83
84
84
fastify . get ( '/one' , ( req , reply ) => {
@@ -90,8 +90,8 @@ test('cache is usable with function as plugin default options input', async (t)
90
90
91
91
fastify . get ( '/two' , ( req , reply ) => {
92
92
fastify . cache . get ( 'one' , ( err , obj ) => {
93
- t . error ( err )
94
- t . same ( obj . item , { one : true } )
93
+ t . assert . ifError ( err )
94
+ t . assert . strictEqual ( obj . item , { one : true } )
95
95
96
96
reply . send ( )
97
97
} )
@@ -130,16 +130,14 @@ test('getting cache item with error returns error', async (t) => {
130
130
fastify . get ( '/one' , ( req , reply ) => {
131
131
fastify . cache . set ( 'one' , { one : true } , 1000 , ( err ) => {
132
132
if ( err ) return reply . send ( err )
133
- return reply
134
- . etag ( '123456' )
135
- . send ( { hello : 'world' } )
133
+ return reply . etag ( '123456' ) . send ( { hello : 'world' } )
136
134
} )
137
135
} )
138
136
139
137
fastify . get ( '/two' , ( req , reply ) => {
140
138
fastify . cache . get ( 'one' , ( err , obj ) => {
141
- t . notOk ( err )
142
- t . notOk ( obj )
139
+ t . assert . failure ( err )
140
+ t . assert . failure ( obj )
143
141
} )
144
142
} )
145
143
@@ -157,7 +155,7 @@ test('getting cache item with error returns error', async (t) => {
157
155
'if-none-match' : '123456'
158
156
}
159
157
} )
160
- t . equal ( response . statusCode , 500 )
158
+ t . assert . strictEqual ( response . statusCode , 500 )
161
159
} )
162
160
163
161
test ( 'etags get stored in cache' , async ( t ) => {
@@ -167,9 +165,7 @@ test('etags get stored in cache', async (t) => {
167
165
await fastify . register ( plugin )
168
166
169
167
fastify . get ( '/one' , ( req , reply ) => {
170
- reply
171
- . etag ( '123456' )
172
- . send ( { hello : 'world' } )
168
+ reply . etag ( '123456' ) . send ( { hello : 'world' } )
173
169
} )
174
170
175
171
await fastify . ready ( )
@@ -186,14 +182,14 @@ test('etags get stored in cache', async (t) => {
186
182
'if-none-match' : '123456'
187
183
}
188
184
} )
189
- t . equal ( response . statusCode , 304 )
185
+ t . assert . strictEqual ( response . statusCode , 304 )
190
186
} )
191
187
192
- test ( 'etag cache life is customizable' , ( t ) => {
188
+ test ( 'etag cache life is customizable' , async ( t ) => {
193
189
t . plan ( 4 )
194
190
195
191
const fastify = Fastify ( )
196
- fastify . register ( plugin )
192
+ await fastify . register ( plugin )
197
193
198
194
fastify . get ( '/one' , function ( req , reply ) {
199
195
reply
@@ -203,29 +199,36 @@ test('etag cache life is customizable', (t) => {
203
199
} )
204
200
205
201
fastify . ready ( ( err ) => {
206
- t . error ( err )
207
-
208
- fastify . inject ( {
209
- method : 'GET' ,
210
- path : '/one'
211
- } , ( err , _response ) => {
212
- t . error ( err )
213
-
214
- // We wait 70 milliseconds that the cache expires
215
- setTimeout ( ( ) => {
216
- fastify . inject ( {
217
- method : 'GET' ,
218
- path : '/one' ,
219
- headers : {
220
- 'if-none-match' : '123456'
221
- }
222
- } , ( err , response ) => {
223
- t . error ( err )
224
- t . equal ( response . statusCode , 200 )
225
- } )
226
- } , 70 )
227
- } )
202
+ t . assert . ifError ( err )
203
+
204
+ fastify . inject (
205
+ {
206
+ method : 'GET' ,
207
+ path : '/one'
208
+ } ,
209
+ ( err , _response ) => {
210
+ t . assert . ifError ( err )
211
+
212
+ // We wait 70 milliseconds that the cache expires
213
+ setTimeout ( ( ) => {
214
+ fastify . inject (
215
+ {
216
+ method : 'GET' ,
217
+ path : '/one' ,
218
+ headers : {
219
+ 'if-none-match' : '123456'
220
+ }
221
+ } ,
222
+ ( err , response ) => {
223
+ t . assert . ifError ( err )
224
+ t . assert . strictEqual ( response . statusCode , 200 )
225
+ }
226
+ )
227
+ } , 70 )
228
+ }
229
+ )
228
230
} )
231
+ await sleep ( 100 )
229
232
} )
230
233
231
234
test ( 'returns response payload' , async ( t ) => {
@@ -235,9 +238,7 @@ test('returns response payload', async (t) => {
235
238
await fastify . register ( plugin )
236
239
237
240
fastify . get ( '/one' , ( req , reply ) => {
238
- reply
239
- . etag ( '123456' , 300 )
240
- . send ( { hello : 'world' } )
241
+ reply . etag ( '123456' , 300 ) . send ( { hello : 'world' } )
241
242
} )
242
243
243
244
await fastify . ready ( )
@@ -252,5 +253,5 @@ test('returns response payload', async (t) => {
252
253
path : '/one'
253
254
} )
254
255
255
- t . same ( JSON . parse ( response . payload ) , { hello : 'world' } )
256
+ t . assert . deepStrictEqual ( JSON . parse ( response . payload ) , { hello : 'world' } )
256
257
} )
0 commit comments