1+ import { Miniflare } from 'miniflare' ;
2+ import { describe , it , expect , vi } from 'vitest' ;
13import type { Fetcher } from '../src/jwk-fetcher' ;
24import { parseMaxAge , UrlKeyFetcher } from '../src/jwk-fetcher' ;
35import { WorkersKVStore } from '../src/key-store' ;
@@ -10,7 +12,12 @@ class HTTPMockFetcher implements Fetcher {
1012 }
1113}
1214
13- const { TEST_NAMESPACE } = getMiniflareBindings ( ) ;
15+ const nullScript = 'export default { fetch: () => new Response(null, { status: 404 }) };' ;
16+ const mf = new Miniflare ( {
17+ modules : true ,
18+ script : nullScript ,
19+ kvNamespaces : [ 'TEST_NAMESPACE' ] ,
20+ } ) ;
1421
1522const validResponseJSON = `{
1623 "keys": [
@@ -62,9 +69,10 @@ describe('UrlKeyFetcher', () => {
6269 } ,
6370 } )
6471 ) ;
72+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
6573 const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
6674
67- const httpFetcherSpy = jest . spyOn ( mockedFetcher , 'fetch' ) ;
75+ const httpFetcherSpy = vi . spyOn ( mockedFetcher , 'fetch' ) ;
6876
6977 // first call (no-cache in KV)
7078 const firstKeys = await urlKeyFetcher . fetchPublicKeys ( ) ;
@@ -92,9 +100,10 @@ describe('UrlKeyFetcher', () => {
92100 headers : { } ,
93101 } )
94102 ) ;
103+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
95104 const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
96105
97- const httpFetcherSpy = jest . spyOn ( mockedFetcher , 'fetch' ) ;
106+ const httpFetcherSpy = vi . spyOn ( mockedFetcher , 'fetch' ) ;
98107
99108 // first call (no-cache in KV)
100109 const firstKeys = await urlKeyFetcher . fetchPublicKeys ( ) ;
@@ -107,36 +116,38 @@ describe('UrlKeyFetcher', () => {
107116 expect ( httpFetcherSpy ) . toBeCalledTimes ( 2 ) ;
108117 } ) ;
109118
110- it ( 'internal server error fetch' , ( ) => {
119+ it ( 'internal server error fetch' , async ( ) => {
111120 const cacheKey = 'failed-fetch-flow-key' ;
112121 const internalServerMsg = 'Internal Server Error' ;
113122 const mockedFetcher = new HTTPMockFetcher (
114123 new Response ( internalServerMsg , {
115124 status : 500 ,
116125 } )
117126 ) ;
127+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
118128 const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
119129
120130 expect ( ( ) => urlKeyFetcher . fetchPublicKeys ( ) ) . rejects . toThrowError (
121131 'Error fetching public keys for Google certs: ' + internalServerMsg
122132 ) ;
123133 } ) ;
124134
125- it ( 'ok fetch but got text response' , ( ) => {
135+ it ( 'ok fetch but got text response' , async ( ) => {
126136 const cacheKey = 'ok-fetch-non-json-flow-key' ;
127137 const mockedFetcher = new HTTPMockFetcher (
128138 new Response ( '{}' , {
129139 status : 200 ,
130140 } )
131141 ) ;
142+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
132143 const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
133144
134145 expect ( ( ) => urlKeyFetcher . fetchPublicKeys ( ) ) . rejects . toThrowError ( 'The public keys are not an object or null:' ) ;
135146 } ) ;
136147} ) ;
137148
138149describe ( 'parseMaxAge' , ( ) => {
139- test . each ( [
150+ it . each ( [
140151 [ 'valid simple' , 'max-age=604800' , 604800 ] ,
141152 [ 'valid with other directives' , 'public, max-age=18793, must-revalidate, no-transform' , 18793 ] ,
142153 [ 'invalid cache-control header is null' , null , NaN ] ,
0 commit comments