forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses-test.ts
96 lines (82 loc) · 2.64 KB
/
responses-test.ts
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
import type { TypedResponse } from "../index";
import { json, redirect } from "../index";
import { isEqual } from "./utils";
describe("json", () => {
it("sets the Content-Type header", () => {
let response = json({});
expect(response.headers.get("Content-Type")).toEqual(
"application/json; charset=utf-8"
);
});
it("preserves existing headers, including Content-Type", () => {
let response = json(
{},
{
headers: {
"Content-Type": "application/json; charset=iso-8859-1",
"X-Remix": "is awesome",
},
}
);
expect(response.headers.get("Content-Type")).toEqual(
"application/json; charset=iso-8859-1"
);
expect(response.headers.get("X-Remix")).toEqual("is awesome");
});
it("encodes the response body", async () => {
let response = json({ hello: "remix" });
expect(await response.json()).toEqual({ hello: "remix" });
});
it("accepts status as a second parameter", () => {
let response = json({}, 201);
expect(response.status).toEqual(201);
});
it("infers input type", async () => {
let response = json({ hello: "remix" });
isEqual<typeof response, TypedResponse<{ hello: string }>>(true);
let result = await response.json();
expect(result).toMatchObject({ hello: "remix" });
});
it("disallows unmatched typed responses", async () => {
let response = json("hello");
isEqual<TypedResponse<number>, typeof response>(false);
});
it("disallows unserializables", () => {
// @ts-expect-error
expect(() => json(124n)).toThrow();
// @ts-expect-error
expect(() => json({ field: 124n })).toThrow();
});
});
describe("redirect", () => {
it("sets the status to 302 by default", () => {
let response = redirect("/login");
expect(response.status).toEqual(302);
});
it("sets the status to 302 when only headers are given", () => {
let response = redirect("/login", {
headers: {
"X-Remix": "is awesome",
},
});
expect(response.status).toEqual(302);
});
it("sets the Location header", () => {
let response = redirect("/login");
expect(response.headers.get("Location")).toEqual("/login");
});
it("preserves existing headers, but not Location", () => {
let response = redirect("/login", {
headers: {
Location: "/",
"X-Remix": "is awesome",
},
});
expect(response.headers.get("Location")).toEqual("/login");
expect(response.headers.get("X-Remix")).toEqual("is awesome");
});
it("accepts status as a second parameter", () => {
let response = redirect("/profile", 301);
expect(response.status).toEqual(301);
});
});