|
1 | 1 | import { existsSync, promises as fs } from "fs"; |
2 | 2 | import path from "path"; |
3 | 3 | import test from "ava"; |
4 | | -import { Cache, CachedResponse, NoOpLog, Response } from "../../src"; |
| 4 | +import { |
| 5 | + Cache, |
| 6 | + CachedResponse, |
| 7 | + MiniflareError, |
| 8 | + NoOpLog, |
| 9 | + Response, |
| 10 | +} from "../../src"; |
5 | 11 | import { KVStorageFactory } from "../../src/kv/helpers"; |
6 | 12 | import { CacheModule } from "../../src/modules/cache"; |
7 | 13 | import { runInWorker, useTmp } from "../helpers"; |
@@ -217,3 +223,43 @@ test("buildSandbox: can delete from default cache", async (t) => { |
217 | 223 | existsSync(path.join(tmp, "default", "http___localhost_8787_test.json")) |
218 | 224 | ); |
219 | 225 | }); |
| 226 | +test("buildSandbox: namespaced cache is separate from default cache", async (t) => { |
| 227 | + const tmp = await useTmp(t); |
| 228 | + const cached = await runInWorker({ cachePersist: tmp }, async () => { |
| 229 | + const sandbox = self as any; |
| 230 | + |
| 231 | + // Store something at the same URLs in default and other caches |
| 232 | + const defaultCache = sandbox.caches.default as Cache; |
| 233 | + const otherCache = (await sandbox.caches.open("other")) as Cache; |
| 234 | + |
| 235 | + await defaultCache.put( |
| 236 | + "http://localhost:8787/test", |
| 237 | + new sandbox.Response("default", { |
| 238 | + headers: { "Cache-Control": "max-age=3600" }, |
| 239 | + }) |
| 240 | + ); |
| 241 | + await otherCache.put( |
| 242 | + "http://localhost:8787/test", |
| 243 | + new sandbox.Response("other", { |
| 244 | + headers: { "Cache-Control": "max-age=3600" }, |
| 245 | + }) |
| 246 | + ); |
| 247 | + |
| 248 | + const defaultCached = await defaultCache.match( |
| 249 | + "http://localhost:8787/test" |
| 250 | + ); |
| 251 | + const otherCached = await otherCache.match("http://localhost:8787/test"); |
| 252 | + |
| 253 | + return [await defaultCached?.text(), await otherCached?.text()]; |
| 254 | + }); |
| 255 | + t.deepEqual(cached, ["default", "other"]); |
| 256 | +}); |
| 257 | +test("buildSandbox: trying to open default cache throws", async (t) => { |
| 258 | + const tmp = await useTmp(t); |
| 259 | + const module = new CacheModule(new NoOpLog(), new KVStorageFactory(tmp)); |
| 260 | + const { caches } = module.buildSandbox({}); |
| 261 | + await t.throwsAsync(caches.open("default"), { |
| 262 | + instanceOf: MiniflareError, |
| 263 | + message: '"default" is a reserved cache name', |
| 264 | + }); |
| 265 | +}); |
0 commit comments