๐Ÿ“ฆ cloudflare / vinext

๐Ÿ“„ draft-mode.test.ts ยท 149 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149/**
 * Next.js Compatibility Tests: draft-mode
 *
 * Ported from: https://github.com/vercel/next.js/blob/canary/test/e2e/app-dir/draft-mode
 *
 * Tests draftMode() API from next/headers in App Router route handlers:
 * - draftMode().enable() sets the bypass cookie
 * - draftMode().disable() clears the bypass cookie
 * - draftMode().isEnabled returns false by default
 * - draftMode().isEnabled returns true with bypass cookie
 *
 * Fixture routes live in:
 * - fixtures/app-basic/app/nextjs-compat/api/draft-enable/
 * - fixtures/app-basic/app/nextjs-compat/api/draft-disable/
 * - fixtures/app-basic/app/nextjs-compat/api/draft-status/
 */

import { describe, it, expect, beforeAll, afterAll } from "vitest";
import type { ViteDevServer } from "vite";
import { APP_FIXTURE_DIR, startFixtureServer, fetchJson } from "../helpers.js";

describe("Next.js compat: draft-mode", () => {
  let server: ViteDevServer;
  let baseUrl: string;

  beforeAll(async () => {
    ({ server, baseUrl } = await startFixtureServer(APP_FIXTURE_DIR, {
      appRouter: true,
    }));
    // Warm up
    await fetch(`${baseUrl}/`).catch(() => {});
  }, 60_000);

  afterAll(async () => {
    await server?.close();
  });

  // โ”€โ”€ draftMode().enable() โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  // Next.js: enabling draft mode should set the __prerender_bypass cookie
  // Source: https://github.com/vercel/next.js/blob/canary/test/e2e/app-dir/draft-mode

  it("draftMode().enable() sets bypass cookie", async () => {
    const res = await fetch(`${baseUrl}/nextjs-compat/api/draft-enable`);
    const setCookies = res.headers.getSetCookie();
    expect(
      setCookies.some((c) => c.includes("__prerender_bypass")),
    ).toBe(true);
  });

  // โ”€โ”€ draftMode().disable() โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  // Next.js: disabling draft mode should clear the __prerender_bypass cookie

  it("draftMode().disable() clears bypass cookie", async () => {
    const res = await fetch(`${baseUrl}/nextjs-compat/api/draft-disable`);
    const setCookies = res.headers.getSetCookie();
    const bypassCookie = setCookies.find((c) =>
      c.includes("__prerender_bypass"),
    );
    expect(bypassCookie).toBeDefined();
    // Clearing should set Max-Age=0 or an expired date or empty value
    expect(bypassCookie).toMatch(
      /Max-Age=0|expires=Thu, 01 Jan 1970|__prerender_bypass=;|__prerender_bypass=""/i,
    );
  });

  // โ”€โ”€ draftMode().isEnabled โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  // Next.js: isEnabled should reflect the presence of the bypass cookie

  it("draftMode().isEnabled returns false by default", async () => {
    const { data } = await fetchJson(
      baseUrl,
      "/nextjs-compat/api/draft-status",
    );
    expect(data).toEqual({ isEnabled: false });
  });

  it("draftMode().isEnabled returns false for arbitrary cookie values", async () => {
    // Arbitrary cookie values should NOT enable draft mode โ€” only the
    // server-generated secret is valid (prevents predictable bypass).
    const { data } = await fetchJson(
      baseUrl,
      "/nextjs-compat/api/draft-status",
      {
        headers: { Cookie: "__prerender_bypass=some-value" },
      },
    );
    expect(data).toEqual({ isEnabled: false });
  });

  it("draftMode().enable() cookie includes Secure flag in production", async () => {
    const {
      draftMode: draftModeFn,
      getDraftModeCookieHeader,
      runWithHeadersContext,
      headersContextFromRequest,
    } = await import("../../packages/vinext/src/shims/headers.js");

    const origEnv = process.env.NODE_ENV;
    process.env.NODE_ENV = "production";
    try {
      const ctx = headersContextFromRequest(
        new Request("http://localhost/test"),
      );
      await runWithHeadersContext(ctx, async () => {
        const dm = await draftModeFn();
        dm.enable();
        const cookieHeader = getDraftModeCookieHeader();
        expect(cookieHeader).toBeDefined();
        expect(cookieHeader).toMatch(/;\s*Secure/i);
      });
    } finally {
      process.env.NODE_ENV = origEnv;
    }
  });

  it("draftMode().enable() cookie omits Secure flag in development", async () => {
    const res = await fetch(`${baseUrl}/nextjs-compat/api/draft-enable`);
    const setCookies = res.headers.getSetCookie();
    const bypassCookie = setCookies.find((c) =>
      c.includes("__prerender_bypass"),
    );
    expect(bypassCookie).toBeDefined();
    // Dev server runs in development โ€” should NOT have Secure flag
    expect(bypassCookie).not.toMatch(/;\s*Secure/i);
  });

  it("draftMode().isEnabled returns true after enable() round-trip", async () => {
    // Enable draft mode and extract the Set-Cookie value
    const enableRes = await fetch(`${baseUrl}/nextjs-compat/api/draft-enable`);
    const setCookies = enableRes.headers.getSetCookie();
    const bypassCookie = setCookies.find((c) =>
      c.includes("__prerender_bypass"),
    );
    expect(bypassCookie).toBeDefined();
    // Extract raw cookie (name=value portion before first ;)
    const rawCookie = bypassCookie!.split(";")[0];

    // Send the valid cookie back to check isEnabled
    const { data } = await fetchJson(
      baseUrl,
      "/nextjs-compat/api/draft-status",
      {
        headers: { Cookie: rawCookie },
      },
    );
    expect(data).toEqual({ isEnabled: true });
  });
});