forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.ts
153 lines (138 loc) · 4 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
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import {
test,
assertEquals,
assertStrictEq,
assertThrowsAsync
} from "./test_deps.ts";
import { ServerRequest } from "./deps.ts";
import httpErrors from "./httpError.ts";
import { Request, BodyType } from "./request.ts";
const encoder = new TextEncoder();
function createMockServerRequest(
url = "/",
body = "",
headerValues: { [header: string]: string } = {}
): ServerRequest {
const headers = new Headers();
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,
body() {
return Promise.resolve(encoder.encode(body));
},
async respond() {}
} as any;
}
test(function requestSearch() {
const request = new Request(createMockServerRequest("/foo?bar=baz&qat=qux"));
assertEquals(request.path, "/foo");
assertEquals(request.search, "?bar=baz&qat=qux");
assertEquals(request.method, "GET");
assertEquals(Array.from(request.searchParams.entries()), [
["bar", "baz"],
["qat", "qux"]
]);
});
test(function serverRequestAvail() {
const mockServerRequest = createMockServerRequest();
const request = new Request(mockServerRequest);
assertStrictEq(request.serverRequest, mockServerRequest);
});
test(function requestAcceptEncoding() {
const request = new Request(
createMockServerRequest("/", "", {
"Accept-Encoding": "gzip, compress;q=0.2, identity;q=0.5"
})
);
assertEquals(request.acceptsEncodings("gzip", "identity"), "gzip");
});
test(function requestAccepts() {
const request = new Request(
createMockServerRequest("/", "", {
Accept: "application/json;q=0.2, text/html"
})
);
assertEquals(request.accepts("application/json", "text/html"), "text/html");
});
test(function requestAcceptsNoProvided() {
const request = new Request(
createMockServerRequest("/", "", {
Accept: "application/json;q=0.2, text/html"
})
);
assertEquals(request.accepts(), ["text/html", "application/json"]);
});
test(function requestNoAccepts() {
const request = new Request(createMockServerRequest("/"));
assertEquals(request.accepts("application/json"), undefined);
});
test(function requestNoAcceptsMatch() {
const request = new Request(
createMockServerRequest("/", "", { Accept: "text/html" })
);
assertEquals(request.accepts("application/json"), undefined);
});
test(async function requestBodyJson() {
const request = new Request(
createMockServerRequest("/", `{"foo":"bar"}`, {
"Content-Type": "application/json"
})
);
assertEquals(await request.body(), {
type: BodyType.JSON,
value: { foo: "bar" }
});
});
test(async function requestBodyForm() {
const request = new Request(
createMockServerRequest("/", `foo=bar&bar=1`, {
"Content-Type": "application/x-www-form-urlencoded"
})
);
const actual = await request.body();
assertEquals(actual!.type, BodyType.Form);
if (actual && actual.type === "form") {
assertEquals(Array.from(actual.value.entries()), [
["foo", "bar"],
["bar", "1"]
]);
} else {
throw Error("Unexpected response");
}
});
test(async function requestBodyText() {
const request = new Request(
createMockServerRequest("/", "hello world!", {
"Content-Type": "text/plain"
})
);
assertEquals(await request.body(), {
type: BodyType.Text,
value: "hello world!"
});
});
test(async function noBodyResolvesUndefined() {
const request = new Request(createMockServerRequest());
assertEquals(await request.body(), {
type: BodyType.Undefined,
value: undefined
});
});
test(async function unsupportedMediaTypeBody() {
const request = new Request(
createMockServerRequest("/", "blah", {
"Content-Type": "multipart/form-data"
})
);
await assertThrowsAsync(async () => {
await request.body();
}, httpErrors.UnsupportedMediaType);
});