forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.ts
233 lines (217 loc) · 5.91 KB
/
context_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
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import {
test,
assert,
assertEquals,
assertStrictEquals,
assertThrows,
assertThrowsAsync,
BufReader,
BufWriter,
} from "./test_deps.ts";
import { Application, State } from "./application.ts";
import { Context } from "./context.ts";
import { Cookies } from "./cookies.ts";
import { ServerRequest } from "./deps.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
import { httpErrors } from "./httpError.ts";
function createMockApp<S extends State = Record<string, any>>(
state = {} as S,
): Application<S> {
return {
state,
dispatchEvent() {},
} as any;
}
interface MockServerOptions {
headers?: [string, string][];
proto?: string;
url?: string;
}
function createMockServerRequest(
{
url = "/",
proto = "HTTP/1.1",
headers: headersInit = [["host", "localhost"]],
}: MockServerOptions = {},
): ServerRequest {
const headers = new Headers(headersInit);
return {
conn: {
close() {},
},
r: new BufReader(new Deno.Buffer(new Uint8Array())),
w: new BufWriter(new Deno.Buffer(new Uint8Array())),
headers,
method: "GET",
proto,
url,
async respond() {},
} as any;
}
function isDenoReader(value: any): value is Deno.Reader {
return value && typeof value === "object" && "read" in value &&
typeof value.read === "function";
}
test({
name: "context",
fn() {
const app = createMockApp();
const serverRequest = createMockServerRequest();
const context = new Context(app, serverRequest);
assert(context instanceof Context);
assertStrictEquals(context.state, app.state);
assertStrictEquals(context.app, app);
assert(context.cookies instanceof Cookies);
assert(context.request instanceof Request);
assert(context.response instanceof Response);
},
});
test({
name: "context.assert()",
fn() {
const context: Context = new Context(
createMockApp(),
createMockServerRequest(),
);
assertThrows(
() => {
let loggedIn: string | undefined;
context.assert(loggedIn, 401, "Unauthorized");
},
httpErrors.Unauthorized,
"Unauthorized",
);
},
});
test({
name: "context.throw()",
fn() {
const context = new Context(createMockApp(), createMockServerRequest());
assertThrows(
() => {
context.throw(404, "foobar");
},
httpErrors.NotFound,
"foobar",
);
},
});
test({
name: "context.send() default path",
async fn() {
const context = new Context(
createMockApp(),
createMockServerRequest({ url: "/test.html" }),
);
const fixture = await Deno.readFile("./fixtures/test.html");
await context.send({ root: "./fixtures" });
const serverResponse = await context.response.toServerResponse();
const bodyReader = serverResponse.body;
assert(isDenoReader(bodyReader));
const body = await Deno.readAll(bodyReader);
assertEquals(body, fixture);
assertEquals(context.response.type, ".html");
assertEquals(
context.response.headers.get("content-length"),
String(fixture.length),
);
assert(context.response.headers.get("last-modified") != null);
assertEquals(context.response.headers.get("cache-control"), "max-age=0");
context.response.destroy();
},
});
test({
name: "context.send() specified path",
async fn() {
const context = new Context(createMockApp(), createMockServerRequest());
const fixture = await Deno.readFile("./fixtures/test.html");
await context.send({ path: "/test.html", root: "./fixtures" });
const serverResponse = context.response.toServerResponse();
const bodyReader = (await serverResponse).body;
assert(isDenoReader(bodyReader));
const body = await Deno.readAll(bodyReader);
assertEquals(body, fixture);
assertEquals(context.response.type, ".html");
assertEquals(
context.response.headers.get("content-length"),
String(fixture.length),
);
assert(context.response.headers.get("last-modified") != null);
assertEquals(context.response.headers.get("cache-control"), "max-age=0");
context.response.destroy();
},
});
test({
name: "context.upgrade()",
async fn() {
const context = new Context(
createMockApp(),
createMockServerRequest({
headers: [["Upgrade", "websocket"], ["Sec-WebSocket-Key", "abc"]],
}),
);
assert(context.socket === undefined);
const ws = await context.upgrade();
assert(ws);
assert(context.socket === ws);
assertEquals(context.respond, false);
},
});
test({
name: "context.upgrade() failure does not set socket/respond",
async fn() {
const context = new Context(createMockApp(), createMockServerRequest());
assert(context.socket === undefined);
await assertThrowsAsync(async () => {
await context.upgrade();
});
assert(context.socket === undefined);
assertEquals(context.respond, true);
},
});
test({
name: "context.isUpgradable true",
async fn() {
const context = new Context(
createMockApp(),
createMockServerRequest({
headers: [["Upgrade", "websocket"], ["Sec-WebSocket-Key", "abc"]],
}),
);
assertEquals(context.isUpgradable, true);
},
});
test({
name: "context.isUpgradable false",
async fn() {
const context = new Context(
createMockApp(),
createMockServerRequest({
headers: [["Upgrade", "websocket"]],
}),
);
assertEquals(context.isUpgradable, false);
},
});
test({
name: "context.getSSETarget()",
async fn() {
const context = new Context(createMockApp(), createMockServerRequest());
const sse = context.sendEvents();
sse.dispatchComment(`hello world`);
await sse.close();
},
});
test({
name: "context create secure",
fn() {
const context = new Context(
createMockApp(),
createMockServerRequest(),
true,
);
assertEquals(context.request.secure, true);
},
});