From 4321b36c97b16bf0e139924df82ddc4520028b9c Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 11 Jul 2023 15:23:52 -0400 Subject: [PATCH] Add unit tests for header mutual exclusivity --- .../__tests__/data-test.ts | 174 +++++++++++++++++- 1 file changed, 172 insertions(+), 2 deletions(-) diff --git a/packages/remix-server-runtime/__tests__/data-test.ts b/packages/remix-server-runtime/__tests__/data-test.ts index 5b64f3c7118..01eabf1781e 100644 --- a/packages/remix-server-runtime/__tests__/data-test.ts +++ b/packages/remix-server-runtime/__tests__/data-test.ts @@ -1,4 +1,5 @@ import type { ServerBuild } from "../build"; +import { defer } from "../responses"; import { createRequestHandler } from "../server"; describe("loaders", () => { @@ -39,7 +40,133 @@ describe("loaders", () => { expect(await res.json()).toMatchInlineSnapshot(`"?foo=bar"`); }); - it("sets header for throw responses", async () => { + it("sets X-Remix-Response header for returned 2xx response", async () => { + let routeId = "routes/random"; + let build = { + routes: { + [routeId]: { + id: routeId, + path: "/random", + module: { + async loader() { + return new Response("text", { + status: 200, + headers: { "Content-Type": "text/plain" }, + }); + }, + }, + }, + }, + entry: { + module: { + handleError() {}, + }, + }, + future: {}, + } as unknown as ServerBuild; + + let handler = createRequestHandler(build); + + let request = new Request( + "http://example.com/random?_data=routes/random&foo=bar", + { + headers: { + "Content-Type": "application/json", + }, + } + ); + + let res = await handler(request); + expect(res.headers.get("X-Remix-Response")).toBeTruthy(); + expect(res.headers.get("X-Remix-Error")).toBeNull(); + expect(res.headers.get("X-Remix-Catch")).toBeNull(); + expect(res.headers.get("X-Remix-Redirect")).toBeNull(); + }); + + it("sets X-Remix-Response header for returned 2xx defer response", async () => { + let routeId = "routes/random"; + let build = { + routes: { + [routeId]: { + id: routeId, + path: "/random", + module: { + async loader() { + return defer({ lazy: Promise.resolve("hey!") }); + }, + }, + }, + }, + entry: { + module: { + handleError() {}, + }, + }, + future: {}, + } as unknown as ServerBuild; + + let handler = createRequestHandler(build); + + let request = new Request( + "http://example.com/random?_data=routes/random&foo=bar", + { + headers: { + "Content-Type": "application/json", + }, + } + ); + + let res = await handler(request); + expect(res.headers.get("X-Remix-Response")).toBeTruthy(); + expect(res.headers.get("X-Remix-Error")).toBeNull(); + expect(res.headers.get("X-Remix-Catch")).toBeNull(); + expect(res.headers.get("X-Remix-Redirect")).toBeNull(); + }); + + it("sets X-Remix-Redirect header for returned 3xx redirect", async () => { + let routeId = "routes/random"; + let build = { + routes: { + [routeId]: { + id: routeId, + path: "/random", + module: { + async loader() { + return new Response("text", { + status: 302, + headers: { Location: "https://remix.run" }, + }); + }, + }, + }, + }, + entry: { + module: { + handleError() {}, + }, + }, + future: {}, + } as unknown as ServerBuild; + + let handler = createRequestHandler(build); + + let request = new Request( + "http://example.com/random?_data=routes/random&foo=bar", + { + headers: { + "Content-Type": "application/json", + }, + } + ); + + let res = await handler(request); + expect(res.headers.get("X-Remix-Redirect")).toBeTruthy(); + expect(res.headers.get("X-Remix-Error")).toBeNull(); + expect(res.headers.get("X-Remix-Catch")).toBeNull(); + expect(res.headers.get("X-Remix-Response")).toBeNull(); + }); + + it("sets X-Remix-Catch header for throw responses", async () => { let loader = async ({ request }) => { throw new Response("null", { headers: { @@ -75,7 +202,50 @@ describe("loaders", () => { ); let res = await handler(request); - expect(await res.headers.get("X-Remix-Catch")).toBeTruthy(); + expect(res.headers.get("X-Remix-Catch")).toBeTruthy(); + expect(res.headers.get("X-Remix-Error")).toBeNull(); + expect(res.headers.get("X-Remix-Redirect")).toBeNull(); + expect(res.headers.get("X-Remix-Response")).toBeNull(); + }); + + it("sets X-Remix-Error header for throw error", async () => { + let routeId = "routes/random"; + let build = { + routes: { + [routeId]: { + id: routeId, + path: "/random", + module: { + async loader() { + throw new Error("broke!"); + }, + }, + }, + }, + entry: { + module: { + handleError() {}, + }, + }, + future: {}, + } as unknown as ServerBuild; + + let handler = createRequestHandler(build); + + let request = new Request( + "http://example.com/random?_data=routes/random&foo=bar", + { + headers: { + "Content-Type": "application/json", + }, + } + ); + + let res = await handler(request); + expect(res.headers.get("X-Remix-Error")).toBeTruthy(); + expect(res.headers.get("X-Remix-Catch")).toBeNull(); + expect(res.headers.get("X-Remix-Redirect")).toBeNull(); + expect(res.headers.get("X-Remix-Response")).toBeNull(); }); it("removes index from request.url", async () => {