|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("astro:middleware", () => ({ |
| 4 | + defineMiddleware: (handler: unknown) => handler, |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock( |
| 8 | + "virtual:emdash/config", |
| 9 | + () => ({ |
| 10 | + default: { |
| 11 | + database: { config: {} }, |
| 12 | + auth: { mode: "none" }, |
| 13 | + }, |
| 14 | + }), |
| 15 | + { virtual: true }, |
| 16 | +); |
| 17 | + |
| 18 | +vi.mock( |
| 19 | + "virtual:emdash/dialect", |
| 20 | + () => ({ |
| 21 | + createDialect: vi.fn(), |
| 22 | + isSessionEnabled: vi.fn().mockReturnValue(false), |
| 23 | + getD1Binding: vi.fn(), |
| 24 | + getDefaultConstraint: vi.fn().mockReturnValue("first-unconstrained"), |
| 25 | + getBookmarkCookieName: vi.fn().mockReturnValue("emdash-bookmark"), |
| 26 | + createSessionDialect: vi.fn(), |
| 27 | + }), |
| 28 | + { virtual: true }, |
| 29 | +); |
| 30 | + |
| 31 | +vi.mock("virtual:emdash/media-providers", () => ({ mediaProviders: [] }), { virtual: true }); |
| 32 | +vi.mock("virtual:emdash/plugins", () => ({ plugins: [] }), { virtual: true }); |
| 33 | +vi.mock( |
| 34 | + "virtual:emdash/sandbox-runner", |
| 35 | + () => ({ |
| 36 | + createSandboxRunner: null, |
| 37 | + sandboxEnabled: false, |
| 38 | + }), |
| 39 | + { virtual: true }, |
| 40 | +); |
| 41 | +vi.mock("virtual:emdash/sandboxed-plugins", () => ({ sandboxedPlugins: [] }), { virtual: true }); |
| 42 | +vi.mock("virtual:emdash/storage", () => ({ createStorage: null }), { virtual: true }); |
| 43 | + |
| 44 | +vi.mock("../../../src/loader.js", () => ({ |
| 45 | + getDb: vi.fn(async () => ({ |
| 46 | + selectFrom: () => ({ |
| 47 | + selectAll: () => ({ |
| 48 | + limit: () => ({ |
| 49 | + execute: async () => [], |
| 50 | + }), |
| 51 | + }), |
| 52 | + }), |
| 53 | + })), |
| 54 | +})); |
| 55 | + |
| 56 | +import { createSessionDialect, getD1Binding, isSessionEnabled } from "virtual:emdash/dialect"; |
| 57 | + |
| 58 | +import onRequest from "../../../src/astro/middleware.js"; |
| 59 | + |
| 60 | +describe("astro middleware prerendered routes", () => { |
| 61 | + it("does not access session on prerendered public runtime routes", async () => { |
| 62 | + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 63 | + try { |
| 64 | + vi.mocked(isSessionEnabled).mockReturnValue(true); |
| 65 | + vi.mocked(getD1Binding).mockReturnValue({ |
| 66 | + withSession: () => { |
| 67 | + throw new Error("withSession reached"); |
| 68 | + }, |
| 69 | + }); |
| 70 | + vi.mocked(createSessionDialect).mockReturnValue(undefined as never); |
| 71 | + |
| 72 | + const cookies = { |
| 73 | + get: vi.fn(() => undefined), |
| 74 | + }; |
| 75 | + |
| 76 | + const context: Record<string, unknown> = { |
| 77 | + request: new Request("https://example.com/robots.txt"), |
| 78 | + url: new URL("https://example.com/robots.txt"), |
| 79 | + cookies, |
| 80 | + locals: {}, |
| 81 | + redirect: vi.fn(), |
| 82 | + isPrerendered: true, |
| 83 | + }; |
| 84 | + |
| 85 | + Object.defineProperty(context, "session", { |
| 86 | + get() { |
| 87 | + throw new Error("session should not be accessed during prerender"); |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + await expect( |
| 92 | + onRequest(context as Parameters<typeof onRequest>[0], async () => new Response("ok")), |
| 93 | + ).rejects.toThrow("withSession reached"); |
| 94 | + } finally { |
| 95 | + consoleErrorSpy.mockRestore(); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not access session when prerendering public pages", async () => { |
| 100 | + const cookies = { |
| 101 | + get: vi.fn(() => undefined), |
| 102 | + }; |
| 103 | + const redirect = vi.fn( |
| 104 | + (location: string) => new Response(null, { status: 302, headers: { Location: location } }), |
| 105 | + ); |
| 106 | + |
| 107 | + const context: Record<string, unknown> = { |
| 108 | + request: new Request("https://example.com/"), |
| 109 | + url: new URL("https://example.com/"), |
| 110 | + cookies, |
| 111 | + locals: {}, |
| 112 | + redirect, |
| 113 | + isPrerendered: true, |
| 114 | + }; |
| 115 | + |
| 116 | + Object.defineProperty(context, "session", { |
| 117 | + get() { |
| 118 | + throw new Error("session should not be accessed during prerender"); |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + const response = await onRequest( |
| 123 | + context as Parameters<typeof onRequest>[0], |
| 124 | + async () => new Response("ok"), |
| 125 | + ); |
| 126 | + |
| 127 | + expect(response.status).toBe(200); |
| 128 | + expect(redirect).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments