-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathtmp_upstream_apiClient.test.js
More file actions
279 lines (232 loc) · 7.99 KB
/
Copy pathtmp_upstream_apiClient.test.js
File metadata and controls
279 lines (232 loc) · 7.99 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from "vitest";
import {
captureAdapter,
mockCustomError,
mockErrorResponse,
mockNetworkFailure,
mockSuccessfulResponse,
restoreAdapter,
} from "./helpers/axiosAdapterMocks.js";
const DEFAULT_MESSAGE = "An unexpected error occurred. Please try again.";
const OFFLINE_MESSAGE =
"Network offline. Please check your internet connection.";
const ONLINE_NETWORK_MESSAGE =
"Unable to reach the server. This may be a network issue or a CORS policy restriction.";
describe("apiClient interceptors", () => {
let apiClient;
let setClerkTokenGetter;
let rejectInterceptor;
beforeAll(async () => {
({ default: apiClient, setClerkTokenGetter } =
await import("../apiClient.js"));
rejectInterceptor = apiClient.interceptors.response.handlers[0].rejected;
});
beforeEach(() => {
vi.clearAllMocks();
setClerkTokenGetter(null);
});
it("attaches credentials and Clerk Bearer token on requests", async () => {
setClerkTokenGetter(async () => "clerk_test_token");
const config = await apiClient.interceptors.request.handlers[0].fulfilled({
headers: {},
});
expect(config.withCredentials).toBe(true);
expect(config.headers.Authorization).toBe("Bearer clerk_test_token");
expect(config.headers["X-CSRF-Token"]).toBeUndefined();
});
it("preserves an Authorization header already set by the caller", async () => {
setClerkTokenGetter(async () => "getter_token");
const config = await apiClient.interceptors.request.handlers[0].fulfilled({
headers: { Authorization: "Bearer explicit_token" },
});
expect(config.headers.Authorization).toBe("Bearer explicit_token");
});
it("continues without Authorization when Clerk token getter is unset", async () => {
const config = await apiClient.interceptors.request.handlers[0].fulfilled({
headers: {},
});
expect(config.withCredentials).toBe(true);
expect(config.headers.Authorization).toBeUndefined();
});
it("maps 401 responses to a friendly session message", async () => {
await expect(
rejectInterceptor({
config: { headers: {} },
response: {
status: 401,
data: {},
},
}),
).rejects.toMatchObject({
message: "Session expired. Please log in again.",
});
});
describe("response interceptor via adapter", () => {
let originalAdapter;
let originalOnLine;
beforeEach(() => {
originalAdapter = captureAdapter(apiClient);
originalOnLine = navigator.onLine;
});
afterEach(() => {
restoreAdapter(apiClient, originalAdapter);
Object.defineProperty(navigator, "onLine", {
configurable: true,
value: originalOnLine,
});
});
it("returns successful responses unchanged", async () => {
mockSuccessfulResponse(apiClient, { ok: true });
await expect(apiClient.get("/test")).resolves.toMatchObject({
data: { ok: true },
status: 200,
});
});
it("handles 401 Unauthorized", async () => {
mockErrorResponse(apiClient, { status: 401, data: {} });
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "Session expired. Please log in again.",
});
});
it("handles 403 Forbidden", async () => {
mockErrorResponse(apiClient, { status: 403, data: {} });
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "You do not have permission to perform this action.",
});
});
it("handles 404 Not Found", async () => {
mockErrorResponse(apiClient, { status: 404, data: {} });
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "The requested resource was not found.",
});
});
it("handles server errors (500)", async () => {
mockErrorResponse(apiClient, { status: 500, data: {} });
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "Server unavailable. Please try again later.",
});
});
it("adds a short request reference to unexpected server errors", async () => {
mockErrorResponse(apiClient, {
status: 500,
data: {
message: "Internal Server Error",
requestId: "7e4de5f1-1234-4567-8901-abcdefabcdef",
},
});
await expect(apiClient.get("/test")).rejects.toMatchObject({
message:
"Server unavailable. Please try again later. Reference: 7e4de5f1-123",
response: {
data: {
requestId: "7e4de5f1-1234-4567-8901-abcdefabcdef",
},
},
});
});
it("does not append a request reference to expected authorization errors", async () => {
mockErrorResponse(apiClient, {
status: 403,
data: {
message: "You do not have permission to perform this action.",
requestId: "authorization-request-id",
},
});
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "You do not have permission to perform this action.",
});
});
it("handles network errors when offline", async () => {
Object.defineProperty(navigator, "onLine", {
configurable: true,
value: false,
});
mockNetworkFailure(apiClient);
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: OFFLINE_MESSAGE,
response: {
data: { message: OFFLINE_MESSAGE },
status: 0,
},
});
});
it("handles network errors when online", async () => {
Object.defineProperty(navigator, "onLine", {
configurable: true,
value: true,
});
mockNetworkFailure(apiClient);
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: ONLINE_NETWORK_MESSAGE,
});
});
});
describe("edge-case error handling (#422)", () => {
let originalAdapter;
let originalOnLine;
beforeEach(() => {
originalAdapter = captureAdapter(apiClient);
originalOnLine = navigator.onLine;
Object.defineProperty(navigator, "onLine", {
configurable: true,
value: true,
});
});
afterEach(() => {
restoreAdapter(apiClient, originalAdapter);
Object.defineProperty(navigator, "onLine", {
configurable: true,
value: originalOnLine,
});
});
it("handles missing response as an online network failure", async () => {
mockCustomError(apiClient, { message: "Network Error" });
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: ONLINE_NETWORK_MESSAGE,
});
});
it("handles null response.data by creating a data payload", async () => {
mockCustomError(apiClient, {
response: { status: 404, data: null },
});
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "The requested resource was not found.",
});
});
it("handles an undefined error object without throwing", async () => {
await expect(rejectInterceptor(undefined)).rejects.toMatchObject({
message: DEFAULT_MESSAGE,
});
});
it("handles a null error object without throwing", async () => {
await expect(rejectInterceptor(null)).rejects.toMatchObject({
message: DEFAULT_MESSAGE,
});
});
it("prefers custom backend messages on 401 responses", async () => {
mockErrorResponse(apiClient, {
status: 401,
data: { message: "Account locked. Contact an administrator." },
});
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "Account locked. Contact an administrator.",
});
});
it("maps 502/503/504 to the server unavailable message", async () => {
for (const status of [502, 503, 504]) {
mockErrorResponse(apiClient, { status, data: {} });
await expect(apiClient.get("/test")).rejects.toMatchObject({
message: "Server unavailable. Please try again later.",
});
}
});
});
});