-
Notifications
You must be signed in to change notification settings - Fork 230
/
application_test.ts
192 lines (165 loc) · 4.24 KB
/
application_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
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import { test, assert, assertEquals, assertStrictEq } from "./test_deps.ts";
import { Application } from "./application.ts";
import { Context } from "./context.ts";
import {
HTTPOptions,
HTTPSOptions,
serve,
serveTLS,
Server,
ServerRequest
} from "./deps.ts";
let serverRequestStack: ServerRequest[] = [];
let addrStack: Array<string | HTTPOptions> = [];
let httpsOptionsStack: HTTPSOptions[] = [];
function teardown() {
serverRequestStack = [];
addrStack = [];
httpsOptionsStack = [];
}
class MockServer {
close(): void {}
async *[Symbol.asyncIterator]() {
for await (const request of serverRequestStack) {
yield request;
}
}
}
const mockServe: typeof serve = function(addr: string | HTTPOptions): Server {
addrStack.push(addr);
return new MockServer() as Server;
};
const mockServeTLS: typeof serveTLS = function(options: HTTPSOptions): Server {
httpsOptionsStack.push(options);
return new MockServer() as Server;
};
function createMockRequest(url = "https://example.com/"): ServerRequest {
return {
url,
async respond() {}
} as any;
}
test(function constructApp() {
const app = new Application();
assert(app instanceof Application);
});
test(async function registerMiddleware() {
serverRequestStack.push(createMockRequest());
const app = new Application(mockServe);
let called = 0;
app.use((context, next) => {
assert(context instanceof Context);
assertEquals(typeof next, "function");
called++;
});
await app.listen("");
assertEquals(called, 1);
teardown();
});
test(async function middlewareExecutionOrder1() {
serverRequestStack.push(createMockRequest());
const app = new Application(mockServe);
const callStack: number[] = [];
app.use(() => {
callStack.push(1);
});
app.use(() => {
callStack.push(2);
});
await app.listen("");
assertEquals(callStack, [1]);
teardown();
});
test(async function middlewareExecutionOrder2() {
serverRequestStack.push(createMockRequest());
const app = new Application(mockServe);
const callStack: number[] = [];
app.use((_context, next) => {
callStack.push(1);
next();
});
app.use(() => {
callStack.push(2);
});
await app.listen("");
assertEquals(callStack, [1, 2]);
teardown();
});
test(async function middlewareExecutionOrder3() {
serverRequestStack.push(createMockRequest());
const app = new Application(mockServe);
const callStack: number[] = [];
app.use((_context, next) => {
callStack.push(1);
next();
callStack.push(2);
});
app.use(async () => {
callStack.push(3);
await Promise.resolve();
callStack.push(4);
});
await app.listen("");
assertEquals(callStack, [1, 3, 2, 4]);
teardown();
});
test(async function middlewareExecutionOrder4() {
serverRequestStack.push(createMockRequest());
const app = new Application(mockServe);
const callStack: number[] = [];
app.use(async (_context, next) => {
callStack.push(1);
await next();
callStack.push(2);
});
app.use(async () => {
callStack.push(3);
await Promise.resolve();
callStack.push(4);
});
await app.listen("");
assertEquals(callStack, [1, 3, 4, 2]);
teardown();
});
test(async function appListen() {
const app = new Application(mockServe);
await app.listen("127.0.0.1:8080");
assertEquals(addrStack, ["127.0.0.1:8080"]);
teardown();
});
test(async function appListenOptions() {
const app = new Application(mockServe);
await app.listen({ port: 8000 });
assertEquals(addrStack, [{ port: 8000 }]);
teardown();
});
test(async function appListenTLS() {
const app = new Application(mockServe, mockServeTLS);
await app.listenTLS({
port: 8000,
certFile: "",
keyFile: ""
});
assertEquals(httpsOptionsStack, [
{
port: 8000,
certFile: "",
keyFile: ""
}
]);
teardown();
});
test(async function appState() {
serverRequestStack.push(createMockRequest());
const app = new Application<{ foo?: string }>(mockServe);
app.state.foo = "bar";
let called = false;
app.use(context => {
assertEquals(context.state, { foo: "bar" });
assertStrictEq(app.state, context.state);
called = true;
});
await app.listen("");
assert(called);
});