-
Notifications
You must be signed in to change notification settings - Fork 235
/
response.test.ts
469 lines (438 loc) · 14.1 KB
/
response.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
import { assert, Status } from "./deps.ts";
import { assertEquals, assertThrows } from "./deps_test.ts";
import type { Request } from "./request.ts";
import { REDIRECT_BACK, Response } from "./response.ts";
import { isNode } from "./utils/type_guards.ts";
function createMockRequest({
headers,
accepts = (_contentType: string) => {
return true;
},
}: {
headers?: [string, string][];
accepts?: (contentType: string) => boolean;
} = {}): Request {
// deno-lint-ignore no-explicit-any
return { accepts, headers: new Headers(headers) } as any;
}
Deno.test({
name: "response empty",
async fn() {
const response = new Response(createMockRequest());
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.body, null);
assertEquals(nativeResponse.status, 404);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
assertEquals(nativeResponse.headers.get("Content-Length"), "0");
},
});
Deno.test({
name: "response.status set",
async fn() {
const response = new Response(createMockRequest());
response.status = 302;
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.body, null);
assertEquals(nativeResponse.status, 302);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
assertEquals(nativeResponse.headers.get("Content-Length"), "0");
},
});
Deno.test({
name: "response.body as text",
async fn() {
const response = new Response(createMockRequest());
response.body = "Hello world!";
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "Hello world!");
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"text/plain; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.body as HTML",
async fn() {
const response = new Response(createMockRequest());
response.body = "<!DOCTYPE html><html><body>Hello world!</body></html>";
const nativeResponse = await response.toDomResponse();
assertEquals(
await nativeResponse.text(),
"<!DOCTYPE html><html><body>Hello world!</body></html>",
);
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"text/html; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.body as JSON",
async fn() {
const response = new Response(createMockRequest());
response.body = { foo: "bar" };
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), `{"foo":"bar"}`);
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"application/json; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.body as symbol",
async fn() {
const response = new Response(createMockRequest());
response.body = Symbol("foo");
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "Symbol(foo)");
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"text/plain; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.body as Uint8Array",
async fn() {
const response = new Response(createMockRequest());
response.body = new TextEncoder().encode("Hello world!");
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "Hello world!");
assertEquals(nativeResponse.status, 200);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 0);
},
});
Deno.test({
name: "response.body as function",
async fn() {
const response = new Response(createMockRequest());
response.body = () => "Hello world!";
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "Hello world!");
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"text/plain; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.body as readable stream",
async fn() {
const response = new Response(createMockRequest());
response.body = new ReadableStream<string>({
start(controller) {
controller.enqueue("hello ");
controller.enqueue("deno");
controller.close();
},
});
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "hello deno");
assertEquals(nativeResponse.status, 200);
},
});
Deno.test({
name: "response.body as async iterable",
async fn() {
const response = new Response(createMockRequest());
response.body = {
async *[Symbol.asyncIterator]() {
yield "hello ";
yield "deno";
return;
},
};
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "hello deno");
assertEquals(nativeResponse.status, 200);
},
});
Deno.test({
name: "response.body as async function",
async fn() {
const response = new Response(createMockRequest());
response.body = () => Promise.resolve("Hello world!");
const nativeResponse = await response.toDomResponse();
assertEquals(await nativeResponse.text(), "Hello world!");
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"text/plain; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.body as FormData",
async fn() {
const response = new Response(createMockRequest());
const formData = new FormData();
formData.append("a", "value");
formData.append(
"b",
new Blob(["some string"], { type: "text/plain" }),
"file.txt",
);
response.body = formData;
const nativeResponse = await response.toDomResponse();
assert(
nativeResponse.headers.get("content-type")?.startsWith(
"multipart/form-data; boundary=",
),
);
},
});
Deno.test({
name: "response.type",
async fn() {
const response = new Response(createMockRequest());
response.type = "js";
response.body = "console.log('hello world');";
const nativeResponse = await response.toDomResponse();
assertEquals(
await nativeResponse.text(),
"console.log('hello world');",
);
assertEquals(nativeResponse.status, 200);
assertEquals(
nativeResponse.headers.get("content-type"),
"text/javascript; charset=UTF-8",
);
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "response.type does not overwrite",
async fn() {
const response = new Response(createMockRequest());
response.type = "js";
response.body = "console.log('hello world');";
response.headers.set("content-type", "text/plain");
const nativeResponse = await response.toDomResponse();
assertEquals(
await nativeResponse.text(),
"console.log('hello world');",
);
assertEquals(nativeResponse.status, 200);
assertEquals(nativeResponse.headers.get("Content-Type"), "text/plain");
assertEquals(Array.from(nativeResponse.headers.entries()).length, 1);
},
});
Deno.test({
name: "empty response sets contentLength to 0",
async fn() {
const response = new Response(createMockRequest());
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.headers.get("Content-Length"), "0");
},
});
Deno.test({
name: "response.redirect('./foo')",
async fn() {
const response = new Response(createMockRequest());
response.redirect("./foo");
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
await nativeResponse.text(),
`Redirecting to <a href="./foo">./foo</a>.`,
);
assertEquals(nativeResponse.headers.get("Location"), "./foo");
assertEquals(
nativeResponse.headers.get("Content-Type"),
"text/html; charset=UTF-8",
);
},
});
Deno.test({
name: "response.redirect(URL)",
async fn() {
const response = new Response(createMockRequest());
const url = new URL("https://example.com/foo");
response.redirect(url);
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
await nativeResponse.text(),
`Redirecting to <a href="https://example.com/foo">https://example.com/foo</a>.`,
);
assertEquals(
nativeResponse.headers.get("Location"),
"https://example.com/foo",
);
},
});
Deno.test({
name: "response.redirect(REDIRECT_BACK)",
async fn() {
const response = new Response(
createMockRequest({ headers: [["referer", "https://example.com/foo"]] }),
);
response.redirect(REDIRECT_BACK);
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
await nativeResponse.text(),
`Redirecting to <a href="https://example.com/foo">https://example.com/foo</a>.`,
);
assertEquals(
nativeResponse.headers.get("Location"),
"https://example.com/foo",
);
},
});
Deno.test({
name: "response.redirect(REDIRECT_BACK) no referrer, but alt",
async fn() {
const response = new Response(createMockRequest());
response.redirect(REDIRECT_BACK, new URL("https://example.com/foo"));
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
await nativeResponse.text(),
`Redirecting to <a href="https://example.com/foo">https://example.com/foo</a>.`,
);
assertEquals(
nativeResponse.headers.get("Location"),
"https://example.com/foo",
);
},
});
Deno.test({
name: "response.redirect(REDIRECT_BACK) no referrer, no alt",
async fn() {
const response = new Response(createMockRequest());
response.redirect(REDIRECT_BACK);
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
await nativeResponse.text(),
`Redirecting to <a href="/">/</a>.`,
);
assertEquals(nativeResponse.headers.get("Location"), "/");
},
});
Deno.test({
name: "response.redirect() no html",
async fn() {
const response = new Response(createMockRequest({
accepts(value) {
return value === "html" ? false : true;
},
}));
response.redirect("https://example.com/foo");
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
await nativeResponse.text(),
`Redirecting to https://example.com/foo.`,
);
assertEquals(
nativeResponse.headers.get("Location"),
"https://example.com/foo",
);
assertEquals(
nativeResponse.headers.get("Content-Type"),
"text/plain; charset=UTF-8",
);
},
});
Deno.test({
name: "response.redirect() with url on query string",
async fn() {
const response = new Response(createMockRequest());
response.redirect(
"https://example.com/foo?redirect=https%3A%2F%2Fdeno.land",
);
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.Found);
assertEquals(
nativeResponse.headers.get("Location"),
"https://example.com/foo?redirect=https%3A%2F%2Fdeno.land",
);
},
});
Deno.test({
name: "response.status reflects body state",
fn() {
const response = new Response(createMockRequest());
assertEquals(response.status, Status.NotFound);
response.body = "hello";
assertEquals(response.status, Status.OK);
response.body = null;
assertEquals(response.status, Status.NoContent);
response.status = Status.PartialContent;
assertEquals(response.status, Status.PartialContent);
},
});
Deno.test({
name: "response.toDomResponse() is a memo",
async fn() {
const response = new Response(createMockRequest());
const sr1 = await response.toDomResponse();
const sr2 = await response.toDomResponse();
assert(sr1 === sr2);
},
});
Deno.test({
name: "response.body cannot be set after server response",
async fn() {
const response = new Response(createMockRequest());
await response.toDomResponse();
assertThrows(() => {
response.body = "";
}, Error);
},
});
Deno.test({
name: "response.status cannot be set after server response",
async fn() {
const response = new Response(createMockRequest());
await response.toDomResponse();
assertThrows(() => {
response.status = Status.Found;
}, Error);
},
});
Deno.test({
name: "response.body handles null",
async fn() {
const response = new Response(createMockRequest());
response.body = null;
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.NoContent);
},
});
Deno.test({
name: "response.body handles falsy values",
async fn() {
const response = new Response(createMockRequest());
response.body = 0;
const nativeResponse = await response.toDomResponse();
assertEquals(nativeResponse.status, Status.OK);
},
});
Deno.test({
name: "Response - inspecting",
fn() {
assertEquals(
Deno.inspect(new Response(createMockRequest())),
isNode()
? `Response {\n body: undefined,\n headers: HeadersList {\n cookies: null,\n [Symbol(headers map)]: Map(0) {},\n [Symbol(headers map sorted)]: null\n },\n status: 404,\n type: undefined,\n writable: true\n}`
: `Response {\n body: undefined,\n headers: Headers {},\n status: 404,\n type: undefined,\n writable: true\n}`,
);
},
});