|
1 | 1 | // @vitest-environment node |
2 | | -import { expect, it } from "vitest"; |
| 2 | +import { afterEach, beforeEach, expect, it, vi } from "vitest"; |
3 | 3 | import { NextRequest } from "next/server"; |
4 | 4 | import { unstable_doesMiddlewareMatch } from "next/experimental/testing/server"; |
5 | 5 | import { config, proxy } from "@/proxy"; |
| 6 | +import { app } from "@/lib/app"; |
6 | 7 | import { createOrganization } from "@/tests/unit/testUtils"; |
7 | 8 |
|
| 9 | +// Stub only HTTP transport; tenant resolution still runs through the real API and DB. |
| 10 | +beforeEach(() => { |
| 11 | + vi.stubGlobal("fetch", (url: URL, init?: RequestInit) => |
| 12 | + app.request(url.href, init), |
| 13 | + ); |
| 14 | +}); |
| 15 | +afterEach(() => vi.unstubAllGlobals()); |
| 16 | + |
| 17 | +it("does not report a missing tenant when its lookup API is unavailable", async () => { |
| 18 | + vi.stubGlobal("fetch", () => |
| 19 | + Promise.resolve(new Response(null, { status: 500 })), |
| 20 | + ); |
| 21 | + const response = await proxy( |
| 22 | + new NextRequest("http://unavailable.lvh.me/login", { |
| 23 | + headers: { host: "unavailable.lvh.me" }, |
| 24 | + }), |
| 25 | + ); |
| 26 | + expect(response.status).toBe(503); |
| 27 | + expect(response.headers.get("x-middleware-rewrite")).toBeNull(); |
| 28 | +}); |
| 29 | + |
| 30 | +it("returns a service error when the tenant lookup cannot connect", async () => { |
| 31 | + vi.stubGlobal("fetch", () => |
| 32 | + Promise.reject(new TypeError("Connection failed")), |
| 33 | + ); |
| 34 | + const response = await proxy( |
| 35 | + new NextRequest("http://unavailable.lvh.me/login", { |
| 36 | + headers: { host: "unavailable.lvh.me" }, |
| 37 | + }), |
| 38 | + ); |
| 39 | + expect(response.status).toBe(503); |
| 40 | +}); |
| 41 | + |
8 | 42 | it("admits existing tenant auth pages", async () => { |
9 | 43 | await createOrganization("existing-tenant"); |
10 | 44 | const response = await proxy( |
|
0 commit comments