@@ -65,6 +65,104 @@ test('cache is usable', (t) => {
6565 } )
6666} )
6767
68+ test ( 'cache is usable with function as plugin default options input' , ( t ) => {
69+ t . plan ( 3 )
70+ const instance = fastify ( )
71+ instance
72+ . register ( ( i , o , n ) => {
73+ i . addHook ( 'onRequest' , function ( req , reply , done ) {
74+ t . notOk ( i [ Symbol . for ( 'fastify-caching.registered' ) ] )
75+ done ( )
76+ } )
77+ n ( )
78+ } )
79+ . register ( plugin , ( ) => ( ) => { } )
80+
81+ instance . addHook ( 'preParsing' , function ( req , reply , payload , done ) {
82+ t . equal ( this [ Symbol . for ( 'fastify-caching.registered' ) ] , true )
83+ done ( null , payload )
84+ } )
85+
86+ instance . get ( '/one' , ( req , reply ) => {
87+ instance . cache . set ( 'one' , { one : true } , 100 , ( err ) => {
88+ if ( err ) return reply . send ( err )
89+ reply . redirect ( '/two' )
90+ } )
91+ } )
92+
93+ instance . get ( '/two' , ( req , reply ) => {
94+ instance . cache . get ( 'one' , ( err , obj ) => {
95+ if ( err ) t . threw ( err )
96+ t . same ( obj . item , { one : true } )
97+ reply . send ( )
98+ } )
99+ } )
100+
101+ instance . listen ( 0 , ( err ) => {
102+ if ( err ) t . threw ( err )
103+ instance . server . unref ( )
104+ const portNum = instance . server . address ( ) . port
105+ const address = `http://127.0.0.1:${ portNum } /one`
106+ http
107+ . get ( address , ( res ) => {
108+ if ( res . statusCode > 300 && res . statusCode < 400 && res . headers . location ) {
109+ http . get ( `http://127.0.0.1:${ portNum } ${ res . headers . location } ` , ( res ) => { } ) . on ( 'error' , t . threw )
110+ }
111+ } )
112+ . on ( 'error' , t . threw )
113+ } )
114+ } )
115+
116+ test ( 'getting cache item with error returns error' , ( t ) => {
117+ t . plan ( 1 )
118+ const mockCache = {
119+ get : ( info , callback ) => callback ( new Error ( 'cache.get always errors' ) ) ,
120+ set : ( key , value , ttl , callback ) => callback ( )
121+ }
122+
123+ const instance = fastify ( )
124+ instance . register ( plugin , { cache : mockCache } )
125+
126+ instance . get ( '/one' , ( req , reply ) => {
127+ instance . cache . set ( 'one' , { one : true } , 1000 , ( err ) => {
128+ if ( err ) return reply . send ( err )
129+ return reply
130+ . etag ( '123456' )
131+ . send ( { hello : 'world' } )
132+ } )
133+ } )
134+
135+ instance . get ( '/two' , ( req , reply ) => {
136+ instance . cache . get ( 'one' , ( err , obj ) => {
137+ t . notOk ( err )
138+ t . notOk ( obj )
139+ } )
140+ } )
141+
142+ instance . listen ( 0 , ( err ) => {
143+ if ( err ) t . threw ( err )
144+ instance . server . unref ( )
145+ const portNum = instance . server . address ( ) . port
146+ const address = `http://127.0.0.1:${ portNum } /one`
147+
148+ http
149+ . get ( address , ( res ) => {
150+ const opts = {
151+ host : '127.0.0.1' ,
152+ port : portNum ,
153+ path : '/two' ,
154+ headers : {
155+ 'if-none-match' : '123456'
156+ }
157+ }
158+ http . get ( opts , ( res ) => {
159+ t . equal ( res . statusCode , 500 )
160+ } ) . on ( 'error' , t . threw )
161+ } )
162+ . on ( 'error' , t . threw )
163+ } )
164+ } )
165+
68166test ( 'etags get stored in cache' , ( t ) => {
69167 t . plan ( 1 )
70168 const instance = fastify ( )
0 commit comments