|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import worker, { |
| 4 | + DOCKER_COMPOSE_UPSTREAM, |
| 5 | + proxyDockerCompose, |
| 6 | + type SiteEnv, |
| 7 | +} from "./index.ts"; |
| 8 | + |
| 9 | +const composeBody = "services:\n rustfs:\n image: rustfs/rustfs:latest\n"; |
| 10 | + |
| 11 | +function assetsEnv(handler: (request: Request) => Promise<Response> | Response): SiteEnv { |
| 12 | + return { |
| 13 | + ASSETS: { |
| 14 | + fetch: async (request) => handler(request), |
| 15 | + }, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +test("proxies GET docker-compose.yml from rustfs/rustfs and keeps the filename", async () => { |
| 20 | + const response = await proxyDockerCompose( |
| 21 | + new Request("https://rustfs.com/docker-compose.yml"), |
| 22 | + async (input, init) => { |
| 23 | + assert.equal(input, DOCKER_COMPOSE_UPSTREAM); |
| 24 | + assert.equal(init?.method, "GET"); |
| 25 | + assert.equal( |
| 26 | + (init?.headers as Record<string, string>)["User-Agent"], |
| 27 | + "rustfs.com-docker-compose-proxy", |
| 28 | + ); |
| 29 | + return new Response(composeBody, { |
| 30 | + status: 200, |
| 31 | + headers: { ETag: '"abc"', "Content-Type": "text/plain" }, |
| 32 | + }); |
| 33 | + }, |
| 34 | + ); |
| 35 | + |
| 36 | + assert.equal(response.status, 200); |
| 37 | + assert.equal(await response.text(), composeBody); |
| 38 | + assert.equal(response.headers.get("Content-Type"), "text/yaml; charset=utf-8"); |
| 39 | + assert.equal( |
| 40 | + response.headers.get("Content-Disposition"), |
| 41 | + 'inline; filename="docker-compose.yml"', |
| 42 | + ); |
| 43 | + assert.equal(response.headers.get("ETag"), '"abc"'); |
| 44 | +}); |
| 45 | + |
| 46 | +test("supports HEAD for curl -I without a body", async () => { |
| 47 | + const response = await proxyDockerCompose( |
| 48 | + new Request("https://rustfs.com/docker-compose.yml", { method: "HEAD" }), |
| 49 | + async (_input, init) => { |
| 50 | + assert.equal(init?.method, "HEAD"); |
| 51 | + return new Response(composeBody, { status: 200 }); |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + assert.equal(response.status, 200); |
| 56 | + assert.equal(await response.text(), ""); |
| 57 | + assert.equal( |
| 58 | + response.headers.get("Content-Disposition"), |
| 59 | + 'inline; filename="docker-compose.yml"', |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +test("rejects non-GET methods", async () => { |
| 64 | + const response = await proxyDockerCompose( |
| 65 | + new Request("https://rustfs.com/docker-compose.yml", { method: "POST" }), |
| 66 | + async () => { |
| 67 | + throw new Error("must not fetch upstream"); |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + assert.equal(response.status, 405); |
| 72 | + assert.equal(response.headers.get("Allow"), "GET, HEAD"); |
| 73 | +}); |
| 74 | + |
| 75 | +test("returns 502 when upstream is unavailable", async () => { |
| 76 | + const response = await proxyDockerCompose( |
| 77 | + new Request("https://rustfs.com/docker-compose.yml"), |
| 78 | + async () => new Response("missing", { status: 404 }), |
| 79 | + ); |
| 80 | + |
| 81 | + assert.equal(response.status, 502); |
| 82 | + assert.match(await response.text(), /Failed to fetch docker-compose.yml/); |
| 83 | +}); |
| 84 | + |
| 85 | +test("does not forward visitor cookies to GitHub", async () => { |
| 86 | + const response = await proxyDockerCompose( |
| 87 | + new Request("https://rustfs.com/docker-compose.yml", { |
| 88 | + headers: { Cookie: "session=secret" }, |
| 89 | + }), |
| 90 | + async (_input, init) => { |
| 91 | + const headers = new Headers(init?.headers); |
| 92 | + assert.equal(headers.get("Cookie"), null); |
| 93 | + return new Response(composeBody, { status: 200 }); |
| 94 | + }, |
| 95 | + ); |
| 96 | + |
| 97 | + assert.equal(response.status, 200); |
| 98 | +}); |
| 99 | + |
| 100 | +test("routes only /docker-compose.yml through the proxy", async () => { |
| 101 | + const originalFetch = globalThis.fetch; |
| 102 | + let proxied = false; |
| 103 | + globalThis.fetch = (async () => { |
| 104 | + proxied = true; |
| 105 | + return new Response(composeBody, { status: 200 }); |
| 106 | + }) as typeof fetch; |
| 107 | + |
| 108 | + try { |
| 109 | + const env = assetsEnv(async (request) => new Response(`asset:${new URL(request.url).pathname}`)); |
| 110 | + const compose = await worker.fetch(new Request("https://rustfs.com/docker-compose.yml"), env); |
| 111 | + const other = await worker.fetch(new Request("https://rustfs.com/install_rustfs.sh"), env); |
| 112 | + |
| 113 | + assert.equal(proxied, true); |
| 114 | + assert.equal(await compose.text(), composeBody); |
| 115 | + assert.equal(await other.text(), "asset:/install_rustfs.sh"); |
| 116 | + } finally { |
| 117 | + globalThis.fetch = originalFetch; |
| 118 | + } |
| 119 | +}); |
0 commit comments