|
1 | | -import { describe, expect, it } from "bun:test"; |
| 1 | +import { afterEach, describe, expect, it, mock } from "bun:test"; |
2 | 2 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
3 | 3 | import { join } from "node:path"; |
4 | 4 | import { tmpdir } from "node:os"; |
@@ -189,3 +189,54 @@ describe("prepareRenderBody — validation", () => { |
189 | 189 | expect(input.outputResolution).toBe("landscape-4k"); |
190 | 190 | }); |
191 | 191 | }); |
| 192 | + |
| 193 | +describe("prepareRenderBody — previewUrl", () => { |
| 194 | + const realFetch = globalThis.fetch; |
| 195 | + const stubFetch = (impl: () => Promise<Response>) => { |
| 196 | + const spy = mock(impl); |
| 197 | + globalThis.fetch = spy as unknown as typeof fetch; |
| 198 | + return spy; |
| 199 | + }; |
| 200 | + afterEach(() => { |
| 201 | + globalThis.fetch = realFetch; |
| 202 | + }); |
| 203 | + |
| 204 | + it.each([ |
| 205 | + "http://169.254.169.254/latest/meta-data/", |
| 206 | + "https://169.254.169.254/latest/meta-data/", |
| 207 | + "https://localhost:8080/index.html", |
| 208 | + "https://[::1]/index.html", |
| 209 | + "file:///etc/passwd", |
| 210 | + ])("refuses %s without fetching it", async (previewUrl) => { |
| 211 | + const fetchSpy = stubFetch(async () => new Response("secret")); |
| 212 | + |
| 213 | + const result = await prepareRenderBody({ previewUrl }); |
| 214 | + |
| 215 | + expect(result).toHaveProperty("error"); |
| 216 | + expect((result as { error: string }).error).toContain("Failed to fetch previewUrl"); |
| 217 | + expect(fetchSpy).not.toHaveBeenCalled(); |
| 218 | + }); |
| 219 | + |
| 220 | + it("refuses a public URL that redirects to a metadata address", async () => { |
| 221 | + const fetchSpy = stubFetch( |
| 222 | + async () => |
| 223 | + new Response(null, { |
| 224 | + status: 302, |
| 225 | + headers: { location: "https://169.254.169.254/latest/meta-data/" }, |
| 226 | + }), |
| 227 | + ); |
| 228 | + |
| 229 | + const result = await prepareRenderBody({ previewUrl: "https://example.com/index.html" }); |
| 230 | + |
| 231 | + expect(result).toHaveProperty("error"); |
| 232 | + expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 233 | + }); |
| 234 | + |
| 235 | + it("renders the HTML of a public URL", async () => { |
| 236 | + stubFetch(async () => new Response("<html><body>ok</body></html>")); |
| 237 | + |
| 238 | + const result = await prepareRenderBody({ previewUrl: "https://example.com/index.html" }); |
| 239 | + |
| 240 | + expect(result).toHaveProperty("prepared"); |
| 241 | + }); |
| 242 | +}); |
0 commit comments