forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.ts
296 lines (275 loc) · 6.86 KB
/
request_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
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license.
// deno-lint-ignore-file
import {
assert,
assertEquals,
assertStrictEquals,
assertThrows,
assertThrowsAsync,
} from "./test_deps.ts";
import type { ServerRequest } from "./http_server_std.ts";
import { Request } from "./request.ts";
const { test } = Deno;
const encoder = new TextEncoder();
function createMockBodyReader(body: string): Deno.Reader {
const buf = encoder.encode(body);
let offset = 0;
return {
async read(p: Uint8Array): Promise<number | null> {
if (offset >= buf.length) {
return null;
}
const chunkSize = Math.min(p.length, buf.length - offset);
p.set(buf);
offset += chunkSize;
return chunkSize;
},
};
}
interface MockServerRequestOptions {
url?: string;
host?: string;
body?: string;
headerValues?: Record<string, string>;
proto?: string;
conn?: {
remoteAddr: {
hostname: string;
};
};
}
function createMockServerRequest(
{
url = "/",
host = "localhost",
body = "",
headerValues = {},
proto = "HTTP/1.1",
}: MockServerRequestOptions = {},
): ServerRequest {
const headers = new Headers();
headers.set("host", host);
for (const [key, value] of Object.entries(headerValues)) {
headers.set(key, value);
}
if (body.length && !headers.has("content-length")) {
headers.set("content-length", String(body.length));
}
return {
headers,
method: "GET",
url,
proto,
body: createMockBodyReader(body),
async respond() {},
} as any;
}
test({
name: "request.searchParams",
fn() {
const request = new Request(
createMockServerRequest({ url: "/foo?bar=baz&qat=qux" }),
);
assertEquals(request.url.pathname, "/foo");
assertEquals(request.url.search, "?bar=baz&qat=qux");
assertEquals(request.method, "GET");
assertEquals(Array.from(request.url.searchParams.entries()), [
["bar", "baz"],
["qat", "qux"],
]);
},
});
test({
name: "request.url",
fn() {
const mockServerRequest = createMockServerRequest({
host: "oakserver.github.io:8080",
url: "/foo/bar/baz?up=down",
proto: "HTTP/1.1",
});
const request = new Request(mockServerRequest, false, true);
assert(request.url instanceof URL);
assertEquals(request.url.protocol, "https:");
assertEquals(request.url.hostname, "oakserver.github.io");
assertEquals(request.url.host, "oakserver.github.io:8080");
assertEquals(request.url.pathname, "/foo/bar/baz");
},
});
test({
name: "request.serverRequest",
fn() {
const mockServerRequest = createMockServerRequest();
const request = new Request(mockServerRequest);
assertStrictEquals(request.originalRequest, mockServerRequest);
},
});
test({
name: "request.acceptsEncodings",
fn() {
const request = new Request(
createMockServerRequest({
headerValues: {
"Accept-Encoding": "gzip, compress;q=0.2, identity;q=0.5",
},
}),
);
assertEquals(request.acceptsEncodings("gzip", "identity"), "gzip");
},
});
test({
name: "request.accepts()",
fn() {
const request = new Request(
createMockServerRequest({
headerValues: {
Accept: "application/json;q=0.2, text/html",
},
}),
);
assertEquals(request.accepts("application/json", "text/html"), "text/html");
},
});
test({
name: "request.accepts not provided",
fn() {
const request = new Request(
createMockServerRequest({
headerValues: {
Accept: "application/json;q=0.2, text/html",
},
}),
);
assertEquals(request.accepts(), ["text/html", "application/json"]);
},
});
test({
name: "request.accepts none",
fn() {
const request = new Request(createMockServerRequest({ url: "/" }));
assertEquals(request.accepts("application/json"), undefined);
},
});
test({
name: "request.accepts no match",
fn() {
const request = new Request(
createMockServerRequest({ headerValues: { Accept: "text/html" } }),
);
assertEquals(request.accepts("application/json"), undefined);
},
});
test({
name: "request.body()",
async fn() {
const request = new Request(
createMockServerRequest(
{
body: JSON.stringify({ hello: "world" }),
headerValues: { "content-type": "application/json" },
},
),
);
assert(request.hasBody);
const actual = request.body();
assertEquals(actual.type, "json");
assertEquals(await actual.value, { hello: "world" });
},
});
test({
name: "request.body() passes args",
async fn() {
const request = new Request(
createMockServerRequest(
{
body: JSON.stringify({ hello: "world" }),
headerValues: { "content-type": "text/plain" },
},
),
);
const actual = request.body({ type: "json" });
assertEquals(actual.type, "json");
assertEquals(await actual.value, { hello: "world" });
},
});
test({
name: "request.secure is false",
fn() {
const request = new Request(createMockServerRequest());
assertEquals(request.secure, false);
},
});
test({
name: "request.secure is true",
fn() {
const request = new Request(
createMockServerRequest(),
false,
true,
);
assertEquals(request.secure, true);
},
});
test({
name: "request with proxy true",
fn() {
const request = new Request(
createMockServerRequest({
headerValues: {
"x-forwarded-host": "10.10.10.10",
"x-forwarded-proto": "http",
"x-forwarded-for": "10.10.10.10, 192.168.1.1, 10.255.255.255",
},
conn: {
remoteAddr: {
hostname: "10.255.255.255",
},
},
}),
true,
true,
);
assertEquals(request.secure, true);
assertEquals(request.url.hostname, "10.10.10.10");
assertEquals(request.url.protocol, "http:");
assertEquals(request.ip, "10.10.10.10");
assertEquals(request.ips, ["10.10.10.10", "192.168.1.1", "10.255.255.255"]);
},
});
test({
name: "request with bad url",
fn() {
const request = new Request(createMockServerRequest({
url: " a ",
}));
assertThrows(
() => {
request.url;
},
TypeError,
`The server request URL of "http://localhost a " is invalid.`,
);
},
});
test({
name: "request with invalid JSON",
async fn() {
const request = new Request(
createMockServerRequest(
{
body: "random text, but not JSON",
headerValues: { "content-type": "application/json" },
},
),
);
assert(request.hasBody, "should have body");
const actual = request.body();
assert(actual.type === "json", "should be a JSON body");
await assertThrowsAsync(
async () => {
await actual.value;
},
SyntaxError,
"Unexpected token r in JSON at position 0",
);
},
});