|
| 1 | +import assert from "assert"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import os from "os"; |
| 5 | +import { RoutesManifest, MiddlewareManifest } from "./interfaces.js"; |
| 6 | +const importOverrides = import("@apphosting/adapter-nextjs/dist/overrides.js"); |
| 7 | + |
| 8 | +describe("route overrides", () => { |
| 9 | + let tmpDir: string; |
| 10 | + let routesManifestPath: string; |
| 11 | + let middlewareManifestPath: string; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test-manifests-")); |
| 15 | + routesManifestPath = path.join(tmpDir, ".next", "routes-manifest.json"); |
| 16 | + middlewareManifestPath = path.join(tmpDir, ".next", "server", "middleware-manifest.json"); |
| 17 | + |
| 18 | + fs.mkdirSync(path.dirname(routesManifestPath), { recursive: true }); |
| 19 | + fs.mkdirSync(path.dirname(middlewareManifestPath), { recursive: true }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should add default fah headers to routes manifest", async () => { |
| 23 | + const { addRouteOverrides } = await importOverrides; |
| 24 | + const initialManifest: RoutesManifest = { |
| 25 | + version: 3, |
| 26 | + basePath: "", |
| 27 | + pages404: true, |
| 28 | + staticRoutes: [], |
| 29 | + dynamicRoutes: [], |
| 30 | + dataRoutes: [], |
| 31 | + headers: [ |
| 32 | + { |
| 33 | + source: "/existing", |
| 34 | + headers: [{ key: "X-Custom", value: "test" }], |
| 35 | + regex: "^/existing$", |
| 36 | + }, |
| 37 | + ], |
| 38 | + rewrites: [], |
| 39 | + redirects: [], |
| 40 | + }; |
| 41 | + |
| 42 | + fs.writeFileSync(routesManifestPath, JSON.stringify(initialManifest)); |
| 43 | + fs.writeFileSync( |
| 44 | + middlewareManifestPath, |
| 45 | + JSON.stringify({ version: 1, sortedMiddleware: [], middleware: {}, functions: {} }), |
| 46 | + ); |
| 47 | + |
| 48 | + await addRouteOverrides(tmpDir, ".next", { |
| 49 | + adapterPackageName: "@apphosting/adapter-nextjs", |
| 50 | + adapterVersion: "1.0.0", |
| 51 | + }); |
| 52 | + |
| 53 | + const updatedManifest = JSON.parse( |
| 54 | + fs.readFileSync(routesManifestPath, "utf-8"), |
| 55 | + ) as RoutesManifest; |
| 56 | + |
| 57 | + assert.strictEqual(updatedManifest.headers.length, 2); |
| 58 | + assert.deepStrictEqual(updatedManifest.headers[0], initialManifest.headers[0]); |
| 59 | + |
| 60 | + const firebaseHeaders = updatedManifest.headers[1]; |
| 61 | + assert.strictEqual(firebaseHeaders.source, "/:path*"); |
| 62 | + assert.strictEqual(firebaseHeaders.headers.length, 1); |
| 63 | + assert.strictEqual(firebaseHeaders.headers[0].key, "x-fah-adapter"); |
| 64 | + assert.strictEqual(firebaseHeaders.headers[0].value, "nextjs-1.0.0"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should add middleware header when middleware exists", async () => { |
| 68 | + const { addRouteOverrides } = await importOverrides; |
| 69 | + const initialManifest: RoutesManifest = { |
| 70 | + version: 3, |
| 71 | + basePath: "", |
| 72 | + pages404: true, |
| 73 | + staticRoutes: [], |
| 74 | + dynamicRoutes: [], |
| 75 | + dataRoutes: [], |
| 76 | + headers: [], |
| 77 | + rewrites: [], |
| 78 | + redirects: [], |
| 79 | + }; |
| 80 | + |
| 81 | + const middlewareManifest: MiddlewareManifest = { |
| 82 | + version: 1, |
| 83 | + sortedMiddleware: ["/"], |
| 84 | + middleware: { |
| 85 | + "/": { |
| 86 | + files: ["middleware.ts"], |
| 87 | + name: "middleware", |
| 88 | + page: "/", |
| 89 | + matchers: [ |
| 90 | + { |
| 91 | + regexp: "^/.*$", |
| 92 | + originalSource: "/:path*", |
| 93 | + }, |
| 94 | + ], |
| 95 | + }, |
| 96 | + }, |
| 97 | + functions: {}, |
| 98 | + }; |
| 99 | + |
| 100 | + fs.writeFileSync(routesManifestPath, JSON.stringify(initialManifest)); |
| 101 | + fs.writeFileSync(middlewareManifestPath, JSON.stringify(middlewareManifest)); |
| 102 | + |
| 103 | + await addRouteOverrides(tmpDir, ".next", { |
| 104 | + adapterPackageName: "@apphosting/adapter-nextjs", |
| 105 | + adapterVersion: "1.0.0", |
| 106 | + }); |
| 107 | + |
| 108 | + const updatedManifest = JSON.parse( |
| 109 | + fs.readFileSync(routesManifestPath, "utf-8"), |
| 110 | + ) as RoutesManifest; |
| 111 | + |
| 112 | + assert.strictEqual(updatedManifest.headers.length, 1); |
| 113 | + |
| 114 | + const headers = updatedManifest.headers[0]; |
| 115 | + assert.strictEqual(headers.source, "/:path*"); |
| 116 | + assert.strictEqual(headers.headers.length, 2); |
| 117 | + assert.strictEqual(headers.headers[0].key, "x-fah-adapter"); |
| 118 | + assert.strictEqual(headers.headers[0].value, "nextjs-1.0.0"); |
| 119 | + assert.strictEqual(headers.headers[1].key, "x-fah-middleware"); |
| 120 | + assert.strictEqual(headers.headers[1].value, "true"); |
| 121 | + }); |
| 122 | + |
| 123 | + afterEach(() => { |
| 124 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments