1
+ import { Miniflare } from 'miniflare' ;
2
+ import { describe , it , expect , vi } from 'vitest' ;
1
3
import type { Fetcher } from '../src/jwk-fetcher' ;
2
4
import { parseMaxAge , UrlKeyFetcher } from '../src/jwk-fetcher' ;
3
5
import { WorkersKVStore } from '../src/key-store' ;
@@ -10,7 +12,12 @@ class HTTPMockFetcher implements Fetcher {
10
12
}
11
13
}
12
14
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
+ } ) ;
14
21
15
22
const validResponseJSON = `{
16
23
"keys": [
@@ -62,9 +69,10 @@ describe('UrlKeyFetcher', () => {
62
69
} ,
63
70
} )
64
71
) ;
72
+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
65
73
const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
66
74
67
- const httpFetcherSpy = jest . spyOn ( mockedFetcher , 'fetch' ) ;
75
+ const httpFetcherSpy = vi . spyOn ( mockedFetcher , 'fetch' ) ;
68
76
69
77
// first call (no-cache in KV)
70
78
const firstKeys = await urlKeyFetcher . fetchPublicKeys ( ) ;
@@ -92,9 +100,10 @@ describe('UrlKeyFetcher', () => {
92
100
headers : { } ,
93
101
} )
94
102
) ;
103
+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
95
104
const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
96
105
97
- const httpFetcherSpy = jest . spyOn ( mockedFetcher , 'fetch' ) ;
106
+ const httpFetcherSpy = vi . spyOn ( mockedFetcher , 'fetch' ) ;
98
107
99
108
// first call (no-cache in KV)
100
109
const firstKeys = await urlKeyFetcher . fetchPublicKeys ( ) ;
@@ -107,36 +116,38 @@ describe('UrlKeyFetcher', () => {
107
116
expect ( httpFetcherSpy ) . toBeCalledTimes ( 2 ) ;
108
117
} ) ;
109
118
110
- it ( 'internal server error fetch' , ( ) => {
119
+ it ( 'internal server error fetch' , async ( ) => {
111
120
const cacheKey = 'failed-fetch-flow-key' ;
112
121
const internalServerMsg = 'Internal Server Error' ;
113
122
const mockedFetcher = new HTTPMockFetcher (
114
123
new Response ( internalServerMsg , {
115
124
status : 500 ,
116
125
} )
117
126
) ;
127
+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
118
128
const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
119
129
120
130
expect ( ( ) => urlKeyFetcher . fetchPublicKeys ( ) ) . rejects . toThrowError (
121
131
'Error fetching public keys for Google certs: ' + internalServerMsg
122
132
) ;
123
133
} ) ;
124
134
125
- it ( 'ok fetch but got text response' , ( ) => {
135
+ it ( 'ok fetch but got text response' , async ( ) => {
126
136
const cacheKey = 'ok-fetch-non-json-flow-key' ;
127
137
const mockedFetcher = new HTTPMockFetcher (
128
138
new Response ( '{}' , {
129
139
status : 200 ,
130
140
} )
131
141
) ;
142
+ const TEST_NAMESPACE = await mf . getKVNamespace ( 'TEST_NAMESPACE' ) ;
132
143
const urlKeyFetcher = new UrlKeyFetcher ( mockedFetcher , new WorkersKVStore ( cacheKey , TEST_NAMESPACE ) ) ;
133
144
134
145
expect ( ( ) => urlKeyFetcher . fetchPublicKeys ( ) ) . rejects . toThrowError ( 'The public keys are not an object or null:' ) ;
135
146
} ) ;
136
147
} ) ;
137
148
138
149
describe ( 'parseMaxAge' , ( ) => {
139
- test . each ( [
150
+ it . each ( [
140
151
[ 'valid simple' , 'max-age=604800' , 604800 ] ,
141
152
[ 'valid with other directives' , 'public, max-age=18793, must-revalidate, no-transform' , 18793 ] ,
142
153
[ 'invalid cache-control header is null' , null , NaN ] ,
0 commit comments