Skip to content

Commit

Permalink
Add unit tests for header mutual exclusivity
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jul 11, 2023
1 parent b89efb4 commit 4321b36
Showing 1 changed file with 172 additions and 2 deletions.
174 changes: 172 additions & 2 deletions packages/remix-server-runtime/__tests__/data-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ServerBuild } from "../build";
import { defer } from "../responses";
import { createRequestHandler } from "../server";

describe("loaders", () => {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 4321b36

Please sign in to comment.