-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
serveio_test.ts
212 lines (200 loc) · 7.06 KB
/
serveio_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
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { runIfMain, test } from "./vendor/https/deno.land/std/testing/mod.ts";
import {
parseKeepAlive,
readRequest,
readResponse,
writeResponse
} from "./serveio.ts";
import {
assert,
assertEquals
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { StringReader } from "./vendor/https/deno.land/std/io/readers.ts";
import { encode } from "./vendor/https/deno.land/std/strings/encode.ts";
import Reader = Deno.Reader;
import Buffer = Deno.Buffer;
import copy = Deno.copy;
import { ServerResponse } from "./server.ts";
import { readString } from "./util.ts";
test(async function serveioReadRequestGet() {
const f = await Deno.open("./fixtures/request_get.txt");
const req = await readRequest(f);
assertEquals(req.method, "GET");
assertEquals(req.url, "/index.html?deno=land&msg=gogo");
assertEquals(req.proto, "HTTP/1.1");
assertEquals(req.headers.get("host"), "deno.land");
assertEquals(req.headers.get("content-type"), "text/plain");
assert(req.body === void 0);
assert(req.trailers === void 0);
f.close();
});
test(async function serveioReadRequestGetCapital() {
const f = await Deno.open("./fixtures/request_get_capital.txt");
const req = await readRequest(f);
assertEquals(req.method, "GET");
assertEquals(req.url, "/About/Index.html?deno=land&msg=gogo");
assertEquals(req.proto, "HTTP/1.1");
assertEquals(req.headers.get("host"), "deno.land");
assertEquals(req.headers.get("content-type"), "text/plain");
assert(req.body === void 0);
assert(req.trailers === void 0);
f.close();
});
test(async function serveioReadRequestEncoded() {
const f = await Deno.open("./fixtures/request_get_encoded.txt");
const req = await readRequest(f);
assertEquals(req.method, "GET");
assertEquals(
req.url,
"/%E3%81%A7%E3%81%AE%E3%81%8F%E3%81%AB?deno=%F0%9F%A6%95"
);
assertEquals(req.proto, "HTTP/1.1");
assertEquals(req.headers.get("host"), "deno.land");
assertEquals(req.headers.get("content-type"), "text/plain");
assert(req.body === void 0);
assert(req.trailers === void 0);
f.close();
});
test(async function serveioReadRequestPost() {
const f = await Deno.open("./fixtures/request_post.txt");
const req = await readRequest(f);
assertEquals(req.method, "POST");
assertEquals(req.url, "/index.html");
assertEquals(req.proto, "HTTP/1.1");
assertEquals(req.headers.get("host"), "deno.land");
assertEquals(req.headers.get("content-type"), "text/plain");
assertEquals(req.headers.get("content-length"), "69");
assertEquals(
await readString(req.body),
"A secure JavaScript/TypeScript runtime built with V8, Rust, and Tokio"
);
assert(req.trailers === void 0);
f.close();
});
test(async function serveioReadRequestPostChunked() {
const f = await Deno.open("./fixtures/request_post_chunked.txt");
const req = await readRequest(f);
assertEquals(req.method, "POST");
assertEquals(req.url, "/index.html");
assertEquals(req.proto, "HTTP/1.1");
assertEquals(req.headers.get("host"), "deno.land");
assertEquals(req.headers.get("content-type"), "text/plain");
assertEquals(req.headers.get("transfer-encoding"), "chunked");
assertEquals(
await readString(req.body),
"A secure JavaScript/TypeScript runtime built with V8, Rust, and Tokio"
);
assertEquals(req.trailers, void 0);
f.close();
});
test(async function serveioReadRequestPostChunkedWithTrailers() {
const f = await Deno.open("./fixtures/request_post_chunked_trailers.txt");
const req = await readRequest(f);
assertEquals(req.method, "POST");
assertEquals(req.url, "/index.html");
assertEquals(req.proto, "HTTP/1.1");
assertEquals(req.headers.get("host"), "deno.land");
assertEquals(req.headers.get("content-type"), "text/plain");
assertEquals(req.headers.get("transfer-encoding"), "chunked");
assertEquals(
await readString(req.body),
"A secure JavaScript/TypeScript runtime built with V8, Rust, and Tokio"
);
assertEquals(req.trailers, void 0);
assertEquals(typeof req.finalize, "function");
await req.finalize();
assertEquals(req.trailers.constructor, Headers);
assertEquals(req.trailers.get("x-deno"), "land");
assertEquals(req.trailers.get("x-node"), "js");
f.close();
});
test(async function serveioReadResponse() {
const f = await Deno.open("./fixtures/response.txt");
const res = await readResponse(f);
assertEquals(res.proto, "HTTP/1.1");
assertEquals(res.status, 200);
assertEquals(res.statusText, "OK");
assertEquals(res.headers.get("content-type"), "text/plain");
assertEquals(res.headers.get("content-length"), "69");
assertEquals(
await readString(res.body),
"A secure JavaScript/TypeScript runtime built with V8, Rust, and Tokio"
);
assertEquals(res.trailers, void 0);
assertEquals(typeof res.finalize, "function");
f.close();
});
test(async function serveioReadResponseChunked() {
const f = await Deno.open("./fixtures/response_chunked.txt");
const res = await readResponse(f);
assertEquals(res.proto, "HTTP/1.1");
assertEquals(res.status, 200);
assertEquals(res.statusText, "OK");
assertEquals(res.headers.get("content-type"), "text/plain");
assertEquals(res.headers.get("transfer-encoding"), "chunked");
assertEquals(
await readString(res.body),
"A secure JavaScript/TypeScript runtime built with V8, Rust, and Tokio"
);
assertEquals(res.trailers, void 0);
await res.finalize();
assertEquals(res.trailers.get("x-deno"), "land");
assertEquals(res.trailers.get("x-node"), "js");
assert(typeof res.finalize === "function");
f.close();
});
test(async function serveioWriteResponse() {
const list: [
ServerResponse["body"],
string | null,
string | undefined,
string
][] = [
["ok", "2", undefined, "text/plain; charset=UTF-8"],
[encode("ok"), "2", "text/plain", "text/plain"],
[new StringReader("ok"), null, undefined, "application/octet-stream"]
];
for (const [body, len, contentType, expContentType] of list) {
const buf = new Buffer();
const headers = new Headers();
if (contentType) {
headers.set("content-type", "text/plain");
}
await writeResponse(buf, {
status: 200,
headers,
body
});
const res = await readResponse(buf);
assertEquals(res.status, 200);
assertEquals(res.headers.get("content-type"), expContentType);
assertEquals(res.headers.get("content-length"), len);
const resBody = new Buffer();
await copy(resBody, res.body);
assertEquals(resBody.toString(), "ok");
}
});
test(async function serveioWriteResponseWithoutHeaders() {
const buf = new Buffer();
await writeResponse(buf, {
status: 200,
body: encode("ok")
});
const res = await readResponse(buf);
assertEquals(res.status, 200);
assertEquals(res.headers.get("content-length"), "2");
const resBody = new Buffer();
await copy(resBody, res.body);
assertEquals(resBody.toString(), "ok");
});
test(function serveioParseKeepAlive() {
const ka = parseKeepAlive(
new Headers({
"keep-alive": "timeout=5, max=100"
})
);
assertEquals(ka.timeout, 5);
assertEquals(ka.max, 100);
});
runIfMain(import.meta);