Spaces:
Configuration error
Configuration error
File size: 16,443 Bytes
5641073 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
import test from 'ava'
import { mockRequestScope, mockGlobalScope, getEvent, sleep, mockKV, mockManifest } from '../mocks'
mockGlobalScope()
import { getAssetFromKV, mapRequestToAsset } from '../index'
import { KVError } from '../types'
test('getAssetFromKV return correct val from KV and default caching', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/key1.txt'))
const res = await getAssetFromKV(event)
if (res) {
t.is(res.headers.get('cache-control'), null)
t.is(res.headers.get('cf-cache-status'), 'MISS')
t.is(await res.text(), 'val1')
t.true(res.headers.get('content-type').includes('text'))
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV evaluated the file matching the extensionless path first /client/ -> client', async (t) => {
mockRequestScope()
const event = getEvent(new Request(`https://foo.com/client/`))
const res = await getAssetFromKV(event)
t.is(await res.text(), 'important file')
t.true(res.headers.get('content-type').includes('text'))
})
test('getAssetFromKV evaluated the file matching the extensionless path first /client -> client', async (t) => {
mockRequestScope()
const event = getEvent(new Request(`https://foo.com/client`))
const res = await getAssetFromKV(event)
t.is(await res.text(), 'important file')
t.true(res.headers.get('content-type').includes('text'))
})
test('getAssetFromKV if not in asset manifest still returns nohash.txt', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/nohash.txt'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'no hash but still got some result')
t.true(res.headers.get('content-type').includes('text'))
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV if no asset manifest /client -> client fails', async (t) => {
mockRequestScope()
const event = getEvent(new Request(`https://foo.com/client`))
const error: KVError = await t.throwsAsync(getAssetFromKV(event, { ASSET_MANIFEST: {} }))
t.is(error.status, 404)
})
test('getAssetFromKV if sub/ -> sub/index.html served', async (t) => {
mockRequestScope()
const event = getEvent(new Request(`https://foo.com/sub`))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'picturedis')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV gets index.html by default for / requests', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'index.html')
t.true(res.headers.get('content-type').includes('html'))
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV non ASCII path support', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/测试.html'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'My filename is non-ascii')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV supports browser percent encoded URLs', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://example.com/%not-really-percent-encoded.html'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'browser percent encoded')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV supports user percent encoded URLs', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/%2F.html'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'user percent encoded')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV only decode URL when necessary', async (t) => {
mockRequestScope()
const event1 = getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD.html'))
const event2 = getEvent(new Request('https://blah.com/你好.html'))
const res1 = await getAssetFromKV(event1)
const res2 = await getAssetFromKV(event2)
if (res1 && res2) {
t.is(await res1.text(), 'Im important')
t.is(await res2.text(), 'Im important')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV Support for user decode url path', async (t) => {
mockRequestScope()
const event1 = getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD/'))
const event2 = getEvent(new Request('https://blah.com/你好/'))
const res1 = await getAssetFromKV(event1)
const res2 = await getAssetFromKV(event2)
if (res1 && res2) {
t.is(await res1.text(), 'My path is non-ascii')
t.is(await res2.text(), 'My path is non-ascii')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV custom key modifier', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/docs/sub/blah.png'))
const customRequestMapper = (request: Request) => {
let defaultModifiedRequest = mapRequestToAsset(request)
let url = new URL(defaultModifiedRequest.url)
url.pathname = url.pathname.replace('/docs', '')
return new Request(url.toString(), request)
}
const res = await getAssetFromKV(event, { mapRequestToAsset: customRequestMapper })
if (res) {
t.is(await res.text(), 'picturedis')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV request override with existing manifest file', async (t) => {
// see https://github.com/cloudflare/kv-asset-handler/pull/159 for more info
mockRequestScope()
const event = getEvent(new Request('https://blah.com/image.png')) // real file in manifest
const customRequestMapper = (request: Request) => {
let defaultModifiedRequest = mapRequestToAsset(request)
let url = new URL(defaultModifiedRequest.url)
url.pathname = '/image.webp' // other different file in manifest
return new Request(url.toString(), request)
}
const res = await getAssetFromKV(event, { mapRequestToAsset: customRequestMapper })
if (res) {
t.is(await res.text(), 'imagewebp')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV when setting browser caching', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/'))
const res = await getAssetFromKV(event, { cacheControl: { browserTTL: 22 } })
if (res) {
t.is(res.headers.get('cache-control'), 'max-age=22')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV when setting custom cache setting', async (t) => {
mockRequestScope()
const event1 = getEvent(new Request('https://blah.com/'))
const event2 = getEvent(new Request('https://blah.com/key1.png?blah=34'))
const cacheOnlyPngs = (req: Request) => {
if (new URL(req.url).pathname.endsWith('.png'))
return {
browserTTL: 720,
edgeTTL: 720,
}
else
return {
bypassCache: true,
}
}
const res1 = await getAssetFromKV(event1, { cacheControl: cacheOnlyPngs })
const res2 = await getAssetFromKV(event2, { cacheControl: cacheOnlyPngs })
if (res1 && res2) {
t.is(res1.headers.get('cache-control'), null)
t.true(res2.headers.get('content-type').includes('png'))
t.is(res2.headers.get('cache-control'), 'max-age=720')
t.is(res2.headers.get('cf-cache-status'), 'MISS')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV caches on two sequential requests', async (t) => {
mockRequestScope()
const resourceKey = 'cache.html'
const resourceVersion = JSON.parse(mockManifest())[resourceKey]
const event1 = getEvent(new Request(`https://blah.com/${resourceKey}`))
const event2 = getEvent(
new Request(`https://blah.com/${resourceKey}`, {
headers: {
'if-none-match': `"${resourceVersion}"`,
},
}),
)
const res1 = await getAssetFromKV(event1, { cacheControl: { edgeTTL: 720, browserTTL: 720 } })
await sleep(1)
const res2 = await getAssetFromKV(event2)
if (res1 && res2) {
t.is(res1.headers.get('cf-cache-status'), 'MISS')
t.is(res1.headers.get('cache-control'), 'max-age=720')
t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV does not store max-age on two sequential requests', async (t) => {
mockRequestScope()
const resourceKey = 'cache.html'
const resourceVersion = JSON.parse(mockManifest())[resourceKey]
const event1 = getEvent(new Request(`https://blah.com/${resourceKey}`))
const event2 = getEvent(
new Request(`https://blah.com/${resourceKey}`, {
headers: {
'if-none-match': `"${resourceVersion}"`,
},
}),
)
const res1 = await getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } })
await sleep(100)
const res2 = await getAssetFromKV(event2)
if (res1 && res2) {
t.is(res1.headers.get('cf-cache-status'), 'MISS')
t.is(res1.headers.get('cache-control'), null)
t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED')
t.is(res2.headers.get('cache-control'), null)
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV does not cache on Cloudflare when bypass cache set', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/'))
const res = await getAssetFromKV(event, { cacheControl: { bypassCache: true } })
if (res) {
t.is(res.headers.get('cache-control'), null)
t.is(res.headers.get('cf-cache-status'), null)
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV with no trailing slash on root', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'index.html')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV with no trailing slash on a subdirectory', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/sub/blah.png'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'picturedis')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV no result throws an error', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/random'))
const error: KVError = await t.throwsAsync(getAssetFromKV(event))
t.is(error.status, 404)
})
test('getAssetFromKV TTls set to null should not cache on browser or edge', async (t) => {
mockRequestScope()
const event = getEvent(new Request('https://blah.com/'))
const res1 = await getAssetFromKV(event, { cacheControl: { browserTTL: null, edgeTTL: null } })
await sleep(100)
const res2 = await getAssetFromKV(event, { cacheControl: { browserTTL: null, edgeTTL: null } })
if (res1 && res2) {
t.is(res1.headers.get('cf-cache-status'), null)
t.is(res1.headers.get('cache-control'), null)
t.is(res2.headers.get('cf-cache-status'), null)
t.is(res2.headers.get('cache-control'), null)
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV passing in a custom NAMESPACE serves correct asset', async (t) => {
mockRequestScope()
let CUSTOM_NAMESPACE = mockKV({
'key1.123HASHBROWN.txt': 'val1',
})
Object.assign(global, { CUSTOM_NAMESPACE })
const event = getEvent(new Request('https://blah.com/'))
const res = await getAssetFromKV(event)
if (res) {
t.is(await res.text(), 'index.html')
t.true(res.headers.get('content-type').includes('html'))
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV when custom namespace without the asset should fail', async (t) => {
mockRequestScope()
let CUSTOM_NAMESPACE = mockKV({
'key5.123HASHBROWN.txt': 'customvalu',
})
const event = getEvent(new Request('https://blah.com'))
const error: KVError = await t.throwsAsync(
getAssetFromKV(event, { ASSET_NAMESPACE: CUSTOM_NAMESPACE }),
)
t.is(error.status, 404)
})
test('getAssetFromKV when namespace not bound fails', async (t) => {
mockRequestScope()
var MY_CUSTOM_NAMESPACE = undefined
Object.assign(global, { MY_CUSTOM_NAMESPACE })
const event = getEvent(new Request('https://blah.com/'))
const error: KVError = await t.throwsAsync(
getAssetFromKV(event, { ASSET_NAMESPACE: MY_CUSTOM_NAMESPACE }),
)
t.is(error.status, 500)
})
test('getAssetFromKV when if-none-match === active resource version, should revalidate', async (t) => {
mockRequestScope()
const resourceKey = 'key1.png'
const resourceVersion = JSON.parse(mockManifest())[resourceKey]
const event1 = getEvent(new Request(`https://blah.com/${resourceKey}`))
const event2 = getEvent(
new Request(`https://blah.com/${resourceKey}`, {
headers: {
'if-none-match': `W/"${resourceVersion}"`,
},
}),
)
const res1 = await getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } })
await sleep(100)
const res2 = await getAssetFromKV(event2)
if (res1 && res2) {
t.is(res1.headers.get('cf-cache-status'), 'MISS')
t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV when if-none-match equals etag of stale resource then should bypass cache', async (t) => {
mockRequestScope()
const resourceKey = 'key1.png'
const resourceVersion = JSON.parse(mockManifest())[resourceKey]
const req1 = new Request(`https://blah.com/${resourceKey}`, {
headers: {
'if-none-match': `"${resourceVersion}"`,
},
})
const req2 = new Request(`https://blah.com/${resourceKey}`, {
headers: {
'if-none-match': `"${resourceVersion}-another-version"`,
},
})
const event = getEvent(req1)
const event2 = getEvent(req2)
const res1 = await getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } })
const res2 = await getAssetFromKV(event)
const res3 = await getAssetFromKV(event2)
if (res1 && res2 && res3) {
t.is(res1.headers.get('cf-cache-status'), 'MISS')
t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`)
t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED')
t.not(res3.headers.get('etag'), req2.headers.get('if-none-match'))
t.is(res3.headers.get('cf-cache-status'), 'MISS')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV when resource in cache, etag should be weakened before returned to eyeball', async (t) => {
mockRequestScope()
const resourceKey = 'key1.png'
const resourceVersion = JSON.parse(mockManifest())[resourceKey]
const req1 = new Request(`https://blah.com/${resourceKey}`, {
headers: {
'if-none-match': `"${resourceVersion}"`,
},
})
const event = getEvent(req1)
const res1 = await getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } })
const res2 = await getAssetFromKV(event)
if (res1 && res2) {
t.is(res1.headers.get('cf-cache-status'), 'MISS')
t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`)
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV if-none-match not sent but resource in cache, should return cache hit 200 OK', async (t) => {
const resourceKey = 'cache.html'
const event = getEvent(new Request(`https://blah.com/${resourceKey}`))
const res1 = await getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } })
await sleep(1)
const res2 = await getAssetFromKV(event)
if (res1 && res2) {
t.is(res1.headers.get('cf-cache-status'), 'MISS')
t.is(res1.headers.get('cache-control'), null)
t.is(res2.status, 200)
t.is(res2.headers.get('cf-cache-status'), 'HIT')
} else {
t.fail('Response was undefined')
}
})
test('getAssetFromKV if range request submitted and resource in cache, request fulfilled', async (t) => {
const resourceKey = 'cache.html'
const event1 = getEvent(new Request(`https://blah.com/${resourceKey}`))
const event2 = getEvent(
new Request(`https://blah.com/${resourceKey}`, { headers: { range: 'bytes=0-10' } }),
)
const res1 = getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } })
await res1
await sleep(2)
const res2 = await getAssetFromKV(event2)
if (res2.headers.has('content-range')) {
t.is(res2.status, 206)
} else {
t.fail('Response was undefined')
}
})
test.todo('getAssetFromKV when body not empty, should invoke .cancel()')
|