forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.ts
157 lines (145 loc) · 4.79 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
// Copyright 2018-2020 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({
name: "response empty",
fn() {
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({
name: "response.status set",
fn() {
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({
name: "response.body as text",
fn() {
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({
name: "response.body as HTML",
fn() {
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({
name: "response.body as JSON",
fn() {
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({
name: "response.body as symbol",
fn() {
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({
name: "response.body as Uint8Array",
fn() {
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({
name: "response.type",
fn() {
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({
name: "response.type does not overwrite",
fn() {
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({
name: "empty response sets contentLength to 0",
fn() {
const response = new Response();
const serverResponse = response.toServerResponse();
assertEquals(serverResponse.headers!.get("Content-Length"), "0");
},
});