-
Notifications
You must be signed in to change notification settings - Fork 235
/
response_test.ts
127 lines (115 loc) · 4.37 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
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import { test, assertEquals } from "./test_deps.ts";
import { Response } from "./response.ts";
const decoder = new TextDecoder();
test(function emptyResponse() {
const response = new Response();
const serverResponse = response.toServerResponse();
assertEquals(serverResponse.body, undefined);
assertEquals(serverResponse.status, 404);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
assertEquals(serverResponse.headers!.get("Content-Length"), "0");
});
test(function statusSet() {
const response = new Response();
response.status = 302;
const serverResponse = response.toServerResponse();
assertEquals(serverResponse.body, undefined);
assertEquals(serverResponse.status, 302);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
assertEquals(serverResponse.headers!.get("Content-Length"), "0");
});
test(function bodyText() {
const response = new Response();
response.body = "Hello world!";
const serverResponse = response.toServerResponse();
assertEquals(decoder.decode(serverResponse.body), "Hello world!");
assertEquals(serverResponse.status, 200);
assertEquals(
serverResponse.headers!.get("content-type"),
"text/plain; charset=utf-8"
);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
});
test(function bodyHtml() {
const response = new Response();
response.body = "<!DOCTYPE html><html><body>Hello world!</body></html>";
const serverResponse = response.toServerResponse();
assertEquals(
decoder.decode(serverResponse.body),
"<!DOCTYPE html><html><body>Hello world!</body></html>"
);
assertEquals(serverResponse.status, 200);
assertEquals(
serverResponse.headers!.get("content-type"),
"text/html; charset=utf-8"
);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
});
test(function bodyJson() {
const response = new Response();
response.body = { foo: "bar" };
const serverResponse = response.toServerResponse();
assertEquals(decoder.decode(serverResponse.body), `{"foo":"bar"}`);
assertEquals(serverResponse.status, 200);
assertEquals(
serverResponse.headers!.get("content-type"),
"application/json; charset=utf-8"
);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
});
test(function bodySymbol() {
const response = new Response();
response.body = Symbol("foo");
const serverResponse = response.toServerResponse();
assertEquals(decoder.decode(serverResponse.body), "Symbol(foo)");
assertEquals(serverResponse.status, 200);
assertEquals(
serverResponse.headers!.get("content-type"),
"text/plain; charset=utf-8"
);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
});
test(function bodyUint8Array() {
const response = new Response();
response.body = new TextEncoder().encode("Hello world!");
const serverResponse = response.toServerResponse();
assertEquals(decoder.decode(serverResponse.body), "Hello world!");
assertEquals(serverResponse.status, 200);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 0);
});
test(function typeDoesNotOverwrite() {
const response = new Response();
response.type = "js";
response.body = "console.log('hello world');";
const serverResponse = response.toServerResponse();
assertEquals(
decoder.decode(serverResponse.body),
"console.log('hello world');"
);
assertEquals(serverResponse.status, 200);
assertEquals(
serverResponse.headers!.get("content-type"),
"application/javascript; charset=utf-8"
);
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
});
test(function contentTypeDoesNotOverwrite() {
const response = new Response();
response.type = "js";
response.body = "console.log('hello world');";
response.headers.set("content-type", "text/plain");
const serverResponse = response.toServerResponse();
assertEquals(
decoder.decode(serverResponse.body),
"console.log('hello world');"
);
assertEquals(serverResponse.status, 200);
assertEquals(serverResponse.headers!.get("Content-Type"), "text/plain");
assertEquals(Array.from(serverResponse.headers!.entries()).length, 1);
});
test(function contentLengthSetsTo0() {
const response = new Response();
const serverResponse = response.toServerResponse();
assertEquals(serverResponse.headers!.get("Content-Length"), "0");
});