forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_test.ts
154 lines (130 loc) · 3.46 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
// 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 { serve, Server, ServerRequest } from "./deps.ts";
let serverRequestStack: ServerRequest[] = [];
let addrStack: string[] = [];
function teardown() {
serverRequestStack = [];
addrStack = [];
}
class MockServer {
close(): void {}
async *[Symbol.asyncIterator]() {
for await (const request of serverRequestStack) {
yield request;
}
}
}
const mockServe: typeof serve = function(addr: string): Server {
addrStack.push(addr);
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 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);
});