forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-test.ts
380 lines (347 loc) · 11.5 KB
/
server-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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import fsp from "fs/promises";
import path from "path";
import lambdaTester from "lambda-tester";
import type { APIGatewayProxyEventV2 } from "aws-lambda";
import {
createRequestHandler as createRemixRequestHandler,
Response as NodeResponse,
} from "@remix-run/node";
import {
createRequestHandler,
createRemixHeaders,
createRemixRequest,
sendRemixResponse,
} from "../server";
// We don't want to test that the remix server works here (that's what the
// puppetteer tests do), we just want to test the architect adapter
jest.mock("@remix-run/node", () => {
let original = jest.requireActual("@remix-run/node");
return {
...original,
createRequestHandler: jest.fn(),
};
});
let mockedCreateRequestHandler =
createRemixRequestHandler as jest.MockedFunction<
typeof createRemixRequestHandler
>;
function createMockEvent(event: Partial<APIGatewayProxyEventV2> = {}) {
let now = new Date();
return {
headers: {
host: "localhost:3333",
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"upgrade-insecure-requests": "1",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15",
"accept-language": "en-US,en;q=0.9",
"accept-encoding": "gzip, deflate",
...event.headers,
},
isBase64Encoded: false,
rawPath: "/",
rawQueryString: "",
requestContext: {
http: {
method: "GET",
path: "/",
protocol: "HTTP/1.1",
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15",
sourceIp: "127.0.0.1",
...event.requestContext?.http,
},
routeKey: "ANY /{proxy+}",
accountId: "accountId",
requestId: "requestId",
apiId: "apiId",
domainName: "id.execute-api.us-east-1.amazonaws.com",
domainPrefix: "id",
stage: "test",
time: now.toISOString(),
timeEpoch: now.getTime(),
...event.requestContext,
},
routeKey: "foo",
version: "2.0",
...event,
};
}
describe("architect createRequestHandler", () => {
describe("basic requests", () => {
afterEach(() => {
mockedCreateRequestHandler.mockReset();
});
afterAll(() => {
jest.restoreAllMocks();
});
it("handles requests", async () => {
mockedCreateRequestHandler.mockImplementation(() => async (req) => {
return new Response(`URL: ${new URL(req.url).pathname}`);
});
// We don't have a real app to test, but it doesn't matter. We won't ever
// call through to the real createRequestHandler
// @ts-expect-error
await lambdaTester(createRequestHandler({ build: undefined }))
.event(createMockEvent({ rawPath: "/foo/bar" }))
.expectResolve((res) => {
expect(res.statusCode).toBe(200);
expect(res.body).toBe("URL: /foo/bar");
});
});
it("handles root // requests", async () => {
mockedCreateRequestHandler.mockImplementation(() => async (req) => {
return new Response(`URL: ${new URL(req.url).pathname}`);
});
// We don't have a real app to test, but it doesn't matter. We won't ever
// call through to the real createRequestHandler
// @ts-expect-error
await lambdaTester(createRequestHandler({ build: undefined }))
.event(createMockEvent({ rawPath: "//" }))
.expectResolve((res) => {
expect(res.statusCode).toBe(200);
expect(res.body).toBe("URL: //");
});
});
it("handles nested // requests", async () => {
mockedCreateRequestHandler.mockImplementation(() => async (req) => {
return new Response(`URL: ${new URL(req.url).pathname}`);
});
// We don't have a real app to test, but it doesn't matter. We won't ever
// call through to the real createRequestHandler
// @ts-expect-error
await lambdaTester(createRequestHandler({ build: undefined }))
.event(createMockEvent({ rawPath: "//foo//bar" }))
.expectResolve((res) => {
expect(res.statusCode).toBe(200);
expect(res.body).toBe("URL: //foo//bar");
});
});
it("handles null body", async () => {
mockedCreateRequestHandler.mockImplementation(() => async () => {
return new Response(null, { status: 200 });
});
// We don't have a real app to test, but it doesn't matter. We won't ever
// call through to the real createRequestHandler
// @ts-expect-error
await lambdaTester(createRequestHandler({ build: undefined }))
.event(createMockEvent({ rawPath: "/foo/bar" }))
.expectResolve((res) => {
expect(res.statusCode).toBe(200);
});
});
it("handles status codes", async () => {
mockedCreateRequestHandler.mockImplementation(() => async () => {
return new Response(null, { status: 204 });
});
// We don't have a real app to test, but it doesn't matter. We won't ever
// call through to the real createRequestHandler
// @ts-expect-error
await lambdaTester(createRequestHandler({ build: undefined }))
.event(createMockEvent({ rawPath: "/foo/bar" }))
.expectResolve((res) => {
expect(res.statusCode).toBe(204);
});
});
it("sets headers", async () => {
mockedCreateRequestHandler.mockImplementation(() => async () => {
let headers = new Headers();
headers.append("X-Time-Of-Year", "most wonderful");
headers.append(
"Set-Cookie",
"first=one; Expires=0; Path=/; HttpOnly; Secure; SameSite=Lax"
);
headers.append(
"Set-Cookie",
"second=two; MaxAge=1209600; Path=/; HttpOnly; Secure; SameSite=Lax"
);
headers.append(
"Set-Cookie",
"third=three; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/; HttpOnly; Secure; SameSite=Lax"
);
return new Response(null, { headers });
});
// We don't have a real app to test, but it doesn't matter. We won't ever
// call through to the real createRequestHandler
// @ts-expect-error
await lambdaTester(createRequestHandler({ build: undefined }))
.event(createMockEvent({ rawPath: "/" }))
.expectResolve((res) => {
expect(res.statusCode).toBe(200);
expect(res.headers["x-time-of-year"]).toBe("most wonderful");
expect(res.cookies).toEqual([
"first=one; Expires=0; Path=/; HttpOnly; Secure; SameSite=Lax",
"second=two; MaxAge=1209600; Path=/; HttpOnly; Secure; SameSite=Lax",
"third=three; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/; HttpOnly; Secure; SameSite=Lax",
]);
});
});
});
});
describe("architect createRemixHeaders", () => {
describe("creates fetch headers from architect headers", () => {
it("handles empty headers", () => {
expect(createRemixHeaders({}, undefined)).toMatchInlineSnapshot(`
Headers {
Symbol(query): Array [],
Symbol(context): null,
}
`);
});
it("handles simple headers", () => {
expect(createRemixHeaders({ "x-foo": "bar" }, undefined))
.toMatchInlineSnapshot(`
Headers {
Symbol(query): Array [
"x-foo",
"bar",
],
Symbol(context): null,
}
`);
});
it("handles multiple headers", () => {
expect(createRemixHeaders({ "x-foo": "bar", "x-bar": "baz" }, undefined))
.toMatchInlineSnapshot(`
Headers {
Symbol(query): Array [
"x-foo",
"bar",
"x-bar",
"baz",
],
Symbol(context): null,
}
`);
});
it("handles headers with multiple values", () => {
expect(createRemixHeaders({ "x-foo": "bar, baz" }, undefined))
.toMatchInlineSnapshot(`
Headers {
Symbol(query): Array [
"x-foo",
"bar, baz",
],
Symbol(context): null,
}
`);
});
it("handles headers with multiple values and multiple headers", () => {
expect(
createRemixHeaders({ "x-foo": "bar, baz", "x-bar": "baz" }, undefined)
).toMatchInlineSnapshot(`
Headers {
Symbol(query): Array [
"x-foo",
"bar, baz",
"x-bar",
"baz",
],
Symbol(context): null,
}
`);
});
it("handles cookies", () => {
expect(
createRemixHeaders({ "x-something-else": "true" }, [
"__session=some_value",
"__other=some_other_value",
])
).toMatchInlineSnapshot(`
Headers {
Symbol(query): Array [
"x-something-else",
"true",
"cookie",
"__session=some_value; __other=some_other_value",
],
Symbol(context): null,
}
`);
});
});
});
describe("architect createRemixRequest", () => {
it("creates a request with the correct headers", () => {
expect(
createRemixRequest(
createMockEvent({
cookies: ["__session=value"],
})
)
).toMatchInlineSnapshot(`
NodeRequest {
"agent": undefined,
"compress": true,
"counter": 0,
"follow": 20,
"highWaterMark": 16384,
"insecureHTTPParser": false,
"size": 0,
Symbol(Body internals): Object {
"body": null,
"boundary": null,
"disturbed": false,
"error": null,
"size": 0,
"type": null,
},
Symbol(Request internals): Object {
"credentials": "same-origin",
"headers": Headers {
Symbol(query): Array [
"accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-encoding",
"gzip, deflate",
"accept-language",
"en-US,en;q=0.9",
"cookie",
"__session=value",
"host",
"localhost:3333",
"upgrade-insecure-requests",
"1",
"user-agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15",
],
Symbol(context): null,
},
"method": "GET",
"parsedURL": "https://localhost:3333/",
"redirect": "follow",
"signal": AbortSignal {},
},
}
`);
});
});
describe("sendRemixResponse", () => {
it("handles regular responses", async () => {
let response = new NodeResponse("anything");
let result = await sendRemixResponse(response);
expect(result.body).toBe("anything");
});
it("handles resource routes with regular data", async () => {
let json = JSON.stringify({ foo: "bar" });
let response = new NodeResponse(json, {
headers: {
"Content-Type": "application/json",
"content-length": json.length.toString(),
},
});
let result = await sendRemixResponse(response);
expect(result.body).toMatch(json);
});
it("handles resource routes with binary data", async () => {
let image = await fsp.readFile(path.join(__dirname, "554828.jpeg"));
let response = new NodeResponse(image, {
headers: {
"content-type": "image/jpeg",
"content-length": image.length.toString(),
},
});
let result = await sendRemixResponse(response);
expect(result.body).toMatch(image.toString("base64"));
});
});