-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.test.ts
More file actions
158 lines (134 loc) · 4.83 KB
/
Copy pathhttp.test.ts
File metadata and controls
158 lines (134 loc) · 4.83 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { afterEach, describe, expect, it, vi } from "vitest";
import {
apiRequest,
isRateLimited,
joinUrl,
jsonBody,
safeJson,
} from "./http.js";
afterEach(() => {
vi.unstubAllGlobals();
});
describe("joinUrl", () => {
it.each([
["https://auth.example.com", "/whoami", "https://auth.example.com/whoami"],
["https://auth.example.com", "whoami", "https://auth.example.com/whoami"],
["https://auth.example.com/base", "/x", "https://auth.example.com/base/x"],
])("joins %s with %s", (base, path, expected) => {
expect(joinUrl(base, path)).toBe(expected);
});
// The portal client refreshes against its own auth host while calling the control
// plane, so an absolute path has to survive being "joined" to a different base.
it.each([
"https://api.seamlessauth.com/applications",
"http://localhost:4000/applications",
"HTTPS://Api.Example.com/x",
])("passes %s through untouched", (absolute) => {
expect(joinUrl("https://auth.example.com", absolute)).toBe(absolute);
});
});
describe("safeJson", () => {
it("parses a JSON body", async () => {
expect(await safeJson(new Response('{"a":1}'))).toEqual({ a: 1 });
});
it("returns null for an empty body", async () => {
expect(await safeJson(new Response(""))).toBeNull();
});
// A 502 from a proxy commonly answers HTML. Callers branch on status, so this has
// to be null rather than a thrown SyntaxError.
it("returns null rather than throwing on a non-JSON body", async () => {
expect(await safeJson(new Response("<html>502</html>"))).toBeNull();
});
it("parses a non-object JSON body", async () => {
expect(await safeJson(new Response('"just a string"'))).toBe("just a string");
});
});
describe("apiRequest", () => {
it("reports ok, status, data, and headers", async () => {
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response('{"message":"ok"}', {
status: 200,
headers: { "content-type": "application/json", "x-trace": "abc" },
}),
),
);
const res = await apiRequest<{ message: string }>("https://x.example.com/y");
expect(res.ok).toBe(true);
expect(res.status).toBe(200);
expect(res.data).toEqual({ message: "ok" });
expect(res.headers.get("x-trace")).toBe("abc");
});
it("reports a failure status with a null body rather than throwing", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response("", { status: 500 })),
);
const res = await apiRequest("https://x.example.com/y");
expect(res.ok).toBe(false);
expect(res.status).toBe(500);
expect(res.data).toBeNull();
});
it("passes the init through to fetch", async () => {
const fetchMock = vi.fn(async () => new Response("{}"));
vi.stubGlobal("fetch", fetchMock);
const init = jsonBody("POST", { name: "x" });
await apiRequest("https://x.example.com/y", init);
expect(fetchMock).toHaveBeenCalledWith("https://x.example.com/y", init);
});
// fetch rejects on a connection failure rather than resolving. Callers catch that
// themselves to name the unreachable host, so it must not be swallowed here.
it("lets a network rejection propagate", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new TypeError("fetch failed");
}),
);
await expect(apiRequest("https://x.example.com/y")).rejects.toThrow(
/fetch failed/,
);
});
});
describe("isRateLimited", () => {
it.each([
[429, true],
[200, false],
[403, false],
[503, false],
])("reports %i as %s", (status, expected) => {
expect(isRateLimited({ status })).toBe(expected);
});
});
describe("jsonBody", () => {
it("sets the content type and serializes the body", () => {
const init = jsonBody("POST", { a: 1 });
expect(init.method).toBe("POST");
expect(init.headers).toEqual({ "Content-Type": "application/json" });
expect(init.body).toBe('{"a":1}');
});
it("omits the body and content type when there is no body", () => {
const init = jsonBody("GET");
expect(init.body).toBeUndefined();
expect(init.headers).toEqual({});
});
it("keeps caller headers alongside the content type", () => {
const init = jsonBody("POST", { a: 1 }, { Authorization: "Bearer t" });
expect(init.headers).toEqual({
Authorization: "Bearer t",
"Content-Type": "application/json",
});
});
// The caller's object must not gain a Content-Type as a side effect, since the
// same headers object is reused across the retry in authClient.
it("does not mutate the headers it was given", () => {
const headers = { Authorization: "Bearer t" };
jsonBody("POST", { a: 1 }, headers);
expect(headers).toEqual({ Authorization: "Bearer t" });
});
it("serializes an explicit null body", () => {
expect(jsonBody("POST", null).body).toBe("null");
});
});