|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { mimeType, parseShellFetchResult, targetUrl, workspaceEgressPolicy } from "./egress.js"; |
| 4 | + |
| 5 | +describe("workspaceEgressPolicy", () => { |
| 6 | + it("blocks egress in none mode", () => { |
| 7 | + const gateway = vi.fn<() => Fetcher>(); |
| 8 | + |
| 9 | + expect(workspaceEgressPolicy("none", gateway)).toEqual({ mode: "none" }); |
| 10 | + expect(gateway).not.toHaveBeenCalled(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("uses direct egress in all mode", () => { |
| 14 | + const gateway = vi.fn<() => Fetcher>(); |
| 15 | + |
| 16 | + expect(workspaceEgressPolicy("all", gateway)).toEqual({ mode: "direct" }); |
| 17 | + expect(gateway).not.toHaveBeenCalled(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("uses the custom HTTP gateway in custom mode", () => { |
| 21 | + const fetcher = { fetch: vi.fn() } as unknown as Fetcher; |
| 22 | + const gateway = vi.fn(() => fetcher); |
| 23 | + |
| 24 | + expect(workspaceEgressPolicy("custom", gateway)).toEqual({ |
| 25 | + mode: "http-gateway", |
| 26 | + gateway: fetcher, |
| 27 | + revision: "v1", |
| 28 | + }); |
| 29 | + expect(gateway).toHaveBeenCalledOnce(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("rejects unsupported modes", () => { |
| 33 | + expect(() => workspaceEgressPolicy("invalid", vi.fn())).toThrow( |
| 34 | + 'EGRESS_MODE must be "none", "all", or "custom"; got "invalid"', |
| 35 | + ); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("mimeType", () => { |
| 40 | + it("removes content type parameters", () => { |
| 41 | + expect(mimeType("text/html; charset=UTF-8")).toBe("text/html"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns null for a missing content type", () => { |
| 45 | + expect(mimeType(null)).toBeNull(); |
| 46 | + expect(mimeType("")).toBeNull(); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("targetUrl", () => { |
| 51 | + it("accepts HTTP and HTTPS URLs", () => { |
| 52 | + expect(targetUrl("https://example.com/path")).toBe("https://example.com/path"); |
| 53 | + expect(targetUrl("http://example.com")).toBe("http://example.com/"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("rejects missing, malformed, and non-HTTP URLs", () => { |
| 57 | + expect(() => targetUrl(null)).toThrow("url query parameter is required"); |
| 58 | + expect(() => targetUrl("not a URL")).toThrow("url must be a valid HTTP or HTTPS URL"); |
| 59 | + expect(() => targetUrl("ftp://example.com/file")).toThrow( |
| 60 | + "url must be a valid HTTP or HTTPS URL", |
| 61 | + ); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("parseShellFetchResult", () => { |
| 66 | + it("returns the status and MIME type from curl output", () => { |
| 67 | + expect(parseShellFetchResult(0, "204\ntext/plain; charset=utf-8", "")).toEqual({ |
| 68 | + status: 204, |
| 69 | + mimeType: "text/plain", |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns an error when curl fails", () => { |
| 74 | + expect(parseShellFetchResult(7, "", "curl: network access denied\n")).toEqual({ |
| 75 | + error: "curl: network access denied", |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns an error for malformed curl output", () => { |
| 80 | + expect(parseShellFetchResult(0, "not-a-status\ntext/plain", "")).toEqual({ |
| 81 | + error: "curl returned an invalid status code", |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments