|
| 1 | +import { SELF } from "cloudflare:test"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { applyConfigurationDefaults } from "../../packages/workers-shared/asset-worker/src/configuration"; |
| 4 | +import Worker from "../../packages/workers-shared/asset-worker/src/index"; |
| 5 | +import { getAssetWithMetadataFromKV } from "../../packages/workers-shared/asset-worker/src/utils/kv"; |
| 6 | +import type { AssetMetadata } from "../../packages/workers-shared/asset-worker/src/utils/kv"; |
| 7 | + |
| 8 | +const IncomingRequest = Request<unknown, IncomingRequestCfProperties>; |
| 9 | + |
| 10 | +vi.mock("../../packages/workers-shared/asset-worker/src/utils/kv.ts"); |
| 11 | +vi.mock("../../packages/workers-shared/asset-worker/src/configuration"); |
| 12 | +const existsMock = (fileList: Set<string>) => { |
| 13 | + vi.spyOn(Worker.prototype, "unstable_exists").mockImplementation( |
| 14 | + async (pathname: string) => { |
| 15 | + if (fileList.has(pathname)) { |
| 16 | + return pathname; |
| 17 | + } |
| 18 | + } |
| 19 | + ); |
| 20 | +}; |
| 21 | +const BASE_URL = "http://example.com"; |
| 22 | + |
| 23 | +describe("[Asset Worker] `test redirects`", () => { |
| 24 | + afterEach(() => { |
| 25 | + vi.mocked(getAssetWithMetadataFromKV).mockRestore(); |
| 26 | + }); |
| 27 | + beforeEach(() => { |
| 28 | + vi.mocked(getAssetWithMetadataFromKV).mockImplementation( |
| 29 | + () => |
| 30 | + Promise.resolve({ |
| 31 | + value: "no-op", |
| 32 | + metadata: { |
| 33 | + contentType: "no-op", |
| 34 | + }, |
| 35 | + }) as unknown as Promise< |
| 36 | + KVNamespaceGetWithMetadataResult<ReadableStream, AssetMetadata> |
| 37 | + > |
| 38 | + ); |
| 39 | + |
| 40 | + vi.mocked(applyConfigurationDefaults).mockImplementation(() => { |
| 41 | + return { |
| 42 | + html_handling: "none", |
| 43 | + not_found_handling: "none", |
| 44 | + }; |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns 404 for non matched encoded url", async () => { |
| 49 | + const files = ["/christmas/starts/november/first.html"]; |
| 50 | + const requestPath = "/%2f%2fbad.example.com%2f"; |
| 51 | + |
| 52 | + existsMock(new Set(files)); |
| 53 | + const request = new IncomingRequest(BASE_URL + requestPath); |
| 54 | + let response = await SELF.fetch(request, { redirect: "manual" }); |
| 55 | + expect(response.status).toBe(404); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns 200 for matched non encoded url", async () => { |
| 59 | + const files = ["/you/lost/the/game.bin"]; |
| 60 | + const requestPath = "/you/lost/the/game.bin"; |
| 61 | + |
| 62 | + existsMock(new Set(files)); |
| 63 | + const request = new IncomingRequest(BASE_URL + requestPath); |
| 64 | + let response = await SELF.fetch(request, { redirect: "manual" }); |
| 65 | + expect(response.status).toBe(200); |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns redirect for matched encoded url", async () => { |
| 69 | + const files = ["/awesome/file.bin"]; |
| 70 | + const requestPath = "/awesome/file%2ebin"; |
| 71 | + const finalPath = "/awesome/file.bin"; |
| 72 | + |
| 73 | + existsMock(new Set(files)); |
| 74 | + const request = new IncomingRequest(BASE_URL + requestPath); |
| 75 | + let response = await SELF.fetch(request, { redirect: "manual" }); |
| 76 | + expect(response.status).toBe(307); |
| 77 | + expect(response.headers.get("location")).toBe(finalPath); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns 200 for matched non encoded url", async () => { |
| 81 | + const files = ["/mylittlepony.png"]; |
| 82 | + const requestPath = "/mylittlepony.png"; |
| 83 | + |
| 84 | + existsMock(new Set(files)); |
| 85 | + const request = new IncomingRequest(BASE_URL + requestPath); |
| 86 | + let response = await SELF.fetch(request, { redirect: "manual" }); |
| 87 | + expect(response.status).toBe(200); |
| 88 | + }); |
| 89 | +}); |
0 commit comments