1- const { AssertionError } = require ( 'assert' )
1+ 'use strict'
22
3- const listen = require ( 'test-listen' )
43const Keyv = require ( '@keyvhq/core' )
5- const micro = require ( 'micro' )
64const test = require ( 'ava' )
75const got = require ( 'got' )
86
9- const cacheableResponse = require ( '..' )
10-
11- const createServer = props => {
12- const server = cacheableResponse ( props )
13- const api = micro ( ( req , res ) => server ( { req, res } ) )
14- return listen ( api )
15- }
16-
17- const parseCacheControl = headers => {
18- const header = headers [ 'cache-control' ]
19- return header . split ( ', ' ) . reduce ( ( acc , rawKey ) => {
20- let value = true
21- let key = rawKey
22- if ( rawKey . includes ( '=' ) ) {
23- const [ parsedKey , parsedValue ] = rawKey . split ( '=' )
24- key = parsedKey
25- value = Number ( parsedValue )
26- }
27- return { ...acc , [ key ] : value }
28- } , { } )
29- }
30-
31- test ( '.get is required' , t => {
32- const error = t . throws ( ( ) => cacheableResponse ( { } ) )
33- t . true ( error instanceof AssertionError )
34- t . is ( error . message , '.get required' )
35- } )
36-
37- test ( '.send is required' , t => {
38- const error = t . throws ( ( ) => cacheableResponse ( { get : true } ) )
39- t . true ( error instanceof AssertionError )
40- t . is ( error . message , '.send required' )
41- } )
7+ const { parseCacheControl, createServer } = require ( './util' )
428
439test ( 'default ttl and revalidate' , async t => {
4410 const url = await createServer ( {
@@ -58,62 +24,6 @@ test('default ttl and revalidate', async t => {
5824 t . true ( [ 1439 , 1440 ] . includes ( cacheControl [ 'stale-if-error' ] ) )
5925} )
6026
61- test ( 'custom ttl' , async t => {
62- const url = await createServer ( {
63- get : ( { req, res } ) => ( { data : { foo : 'bar' } , ttl : 86400000 } ) ,
64- send : ( { data, headers, res, req, ...props } ) => {
65- res . end ( 'Welcome to Micro' )
66- }
67- } )
68-
69- const { headers } = await got ( `${ url } /kikobeats` )
70- const cacheControl = parseCacheControl ( headers )
71-
72- t . true ( cacheControl . public )
73- t . true ( cacheControl [ 'must-revalidate' ] )
74- t . true ( [ 86399 , 86400 ] . includes ( cacheControl [ 'max-age' ] ) )
75- t . true ( [ 17279 , 17280 ] . includes ( cacheControl [ 'stale-while-revalidate' ] ) )
76- t . true ( [ 17279 , 17280 ] . includes ( cacheControl [ 'stale-if-error' ] ) )
77- } )
78-
79- test ( 'custom revalidate' , async t => {
80- const url = await createServer ( {
81- revalidate : ttl => ttl * 0.1 ,
82- get : ( { req, res } ) => ( { data : { foo : 'bar' } , ttl : 86400000 } ) ,
83- send : ( { data, headers, res, req, ...props } ) => {
84- res . end ( 'Welcome to Micro' )
85- }
86- } )
87-
88- const { headers } = await got ( `${ url } /kikobeats` )
89- const cacheControl = parseCacheControl ( headers )
90-
91- t . true ( cacheControl . public )
92- t . true ( cacheControl [ 'must-revalidate' ] )
93- t . true ( [ 86399 , 86400 ] . includes ( cacheControl [ 'max-age' ] ) )
94- t . true ( [ 8639 , 8640 ] . includes ( cacheControl [ 'stale-while-revalidate' ] ) )
95- t . true ( [ 8639 , 8640 ] . includes ( cacheControl [ 'stale-if-error' ] ) )
96- } )
97-
98- test ( 'custom fixed revalidate' , async t => {
99- const url = await createServer ( {
100- revalidate : 300000 ,
101- get : ( { req, res } ) => ( { data : { foo : 'bar' } , ttl : 86400000 } ) ,
102- send : ( { data, headers, res, req, ...props } ) => {
103- res . end ( 'Welcome to Micro' )
104- }
105- } )
106-
107- const { headers } = await got ( `${ url } /kikobeats` )
108- const cacheControl = parseCacheControl ( headers )
109-
110- t . true ( cacheControl . public )
111- t . true ( cacheControl [ 'must-revalidate' ] )
112- t . true ( [ 86399 , 86400 ] . includes ( cacheControl [ 'max-age' ] ) )
113- t . true ( [ 299 , 300 ] . includes ( cacheControl [ 'stale-while-revalidate' ] ) )
114- t . true ( [ 299 , 300 ] . includes ( cacheControl [ 'stale-if-error' ] ) )
115- } )
116-
11727test ( 'disable revalidation' , async t => {
11828 const url = await createServer ( {
11929 revalidate : false ,
@@ -187,15 +97,13 @@ test('force query params to invalidate', async t => {
18797
18898 const { headers : headersOne } = await got ( `${ url } /kikobeats` )
18999 t . is ( headersOne [ 'x-cache-status' ] , 'MISS' )
190- // t.snapshot(parseCacheControl(headersOne))
191100
192101 const { headers : headersTwo } = await got ( `${ url } /kikobeats` )
193102 t . is ( headersTwo [ 'x-cache-status' ] , 'HIT' )
194103
195104 const { headers : headersThree } = await got ( `${ url } /kikobeats?force=true` )
196105 t . is ( headersThree [ 'x-cache-status' ] , 'BYPASS' )
197106 t . is ( headersThree [ 'x-cache-expired-at' ] , '0ms' )
198- // t.snapshot(parseCacheControl(headersThree))
199107
200108 const { headers : headersFour } = await got ( `${ url } /kikobeats` )
201109 t . is ( headersFour [ 'x-cache-status' ] , 'HIT' )
0 commit comments