|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { CACHE_CONTROL } from "~/http"; |
| 3 | + |
| 4 | +const { getRepoTags, getRepoDoc } = vi.hoisted(() => ({ |
| 5 | + getRepoTags: vi.fn<() => Promise<string[] | undefined>>(), |
| 6 | + getRepoDoc: |
| 7 | + vi.fn<(ref: string, slug: string) => Promise<{ md: string } | undefined>>(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("./index", () => ({ |
| 11 | + getRepoTags: () => getRepoTags(), |
| 12 | + getRepoDoc: (ref: string, slug: string) => getRepoDoc(ref, slug), |
| 13 | +})); |
| 14 | + |
| 15 | +import { handleMarkdownRequest } from "./markdown-request"; |
| 16 | + |
| 17 | +const LATEST = "7.15.1"; |
| 18 | +const MD = "---\ntitle: Installation\n---\n\n# Installation\n\nHello."; |
| 19 | + |
| 20 | +function call( |
| 21 | + pathname: string, |
| 22 | + { accept, method = "GET" }: { accept?: string; method?: string } = {}, |
| 23 | +): Promise<Response | undefined> { |
| 24 | + let url = new URL(`https://reactrouter.com${pathname}`); |
| 25 | + let headers = new Headers(); |
| 26 | + if (accept) headers.set("accept", accept); |
| 27 | + let request = new Request(url, { method, headers }); |
| 28 | + return handleMarkdownRequest( |
| 29 | + { request, url } as never, |
| 30 | + (async () => undefined) as never, |
| 31 | + ) as Promise<Response | undefined>; |
| 32 | +} |
| 33 | + |
| 34 | +describe("handleMarkdownRequest", () => { |
| 35 | + beforeEach(() => { |
| 36 | + getRepoTags.mockReset().mockResolvedValue([LATEST]); |
| 37 | + getRepoDoc.mockReset().mockResolvedValue({ md: MD }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("continues for normal browser requests", async () => { |
| 41 | + let res = await call("/start/installation", { |
| 42 | + accept: "text/html,application/xhtml+xml,*/*;q=0.8", |
| 43 | + }); |
| 44 | + expect(res).toBeUndefined(); |
| 45 | + expect(getRepoDoc).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("serves markdown when Accept prefers it", async () => { |
| 49 | + let res = await call("/start/installation", { accept: "text/markdown" }); |
| 50 | + expect(res).toBeInstanceOf(Response); |
| 51 | + expect(res!.status).toBe(200); |
| 52 | + expect(res!.headers.get("content-type")).toBe( |
| 53 | + "text/markdown; charset=utf-8", |
| 54 | + ); |
| 55 | + expect(res!.headers.get("cache-control")).toBe(CACHE_CONTROL.doc); |
| 56 | + expect(res!.headers.get("vary")).toBe("Accept"); |
| 57 | + expect(res!.headers.get("x-markdown-tokens")).toBe( |
| 58 | + String(Math.ceil(MD.length / 4)), |
| 59 | + ); |
| 60 | + expect(await res!.text()).toBe(MD); |
| 61 | + }); |
| 62 | + |
| 63 | + it("resolves ref + slug from the URL for content negotiation", async () => { |
| 64 | + await call("/main/start/installation", { accept: "text/markdown" }); |
| 65 | + expect(getRepoDoc).toHaveBeenCalledWith("main", "docs/start/installation"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("maps /home and /changelog to their special slugs", async () => { |
| 69 | + await call("/home", { accept: "text/markdown" }); |
| 70 | + expect(getRepoDoc).toHaveBeenCalledWith(LATEST, "docs/index"); |
| 71 | + |
| 72 | + getRepoDoc.mockClear(); |
| 73 | + await call("/changelog", { accept: "text/markdown" }); |
| 74 | + expect(getRepoDoc).toHaveBeenCalledWith(LATEST, "CHANGELOG"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("serves markdown for the .md suffix without Vary", async () => { |
| 78 | + let res = await call("/start/installation.md"); |
| 79 | + expect(res).toBeInstanceOf(Response); |
| 80 | + expect(res!.headers.get("content-type")).toBe( |
| 81 | + "text/markdown; charset=utf-8", |
| 82 | + ); |
| 83 | + expect(res!.headers.get("vary")).toBeNull(); |
| 84 | + expect(getRepoDoc).toHaveBeenCalledWith(LATEST, "docs/start/installation"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("returns no body for HEAD but keeps the headers", async () => { |
| 88 | + let res = await call("/start/installation", { |
| 89 | + accept: "text/markdown", |
| 90 | + method: "HEAD", |
| 91 | + }); |
| 92 | + expect(res!.headers.get("content-type")).toBe( |
| 93 | + "text/markdown; charset=utf-8", |
| 94 | + ); |
| 95 | + expect(await res!.text()).toBe(""); |
| 96 | + }); |
| 97 | + |
| 98 | + it("continues when the doc does not exist", async () => { |
| 99 | + getRepoDoc.mockResolvedValue(undefined); |
| 100 | + let res = await call("/nope", { accept: "text/markdown" }); |
| 101 | + expect(res).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("continues when GitHub tags are unavailable", async () => { |
| 105 | + getRepoTags.mockResolvedValue(undefined); |
| 106 | + let res = await call("/start/installation", { accept: "text/markdown" }); |
| 107 | + expect(res).toBeUndefined(); |
| 108 | + expect(getRepoDoc).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("continues when GitHub tags reject", async () => { |
| 112 | + getRepoTags.mockRejectedValue(new Error("boom")); |
| 113 | + let res = await call("/start/installation", { accept: "text/markdown" }); |
| 114 | + expect(res).toBeUndefined(); |
| 115 | + expect(getRepoDoc).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("continues when the doc lookup throws", async () => { |
| 119 | + getRepoDoc.mockRejectedValue(new Error("boom")); |
| 120 | + let res = await call("/start/installation", { accept: "text/markdown" }); |
| 121 | + expect(res).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("ignores non-GET/HEAD methods", async () => { |
| 125 | + let res = await call("/start/installation", { |
| 126 | + accept: "text/markdown", |
| 127 | + method: "POST", |
| 128 | + }); |
| 129 | + expect(res).toBeUndefined(); |
| 130 | + expect(getRepoTags).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments