forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.ts
45 lines (42 loc) · 1.28 KB
/
middleware_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
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import { test, assertEquals, assertStrictEq } from "./test_deps.ts";
import { Context } from "./context.ts";
import { Status } from "./deps.ts";
import { compose, Middleware } from "./middleware.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
function createMockContext<S extends object = { [key: string]: any }>() {
return ({
request: {
headers: new Headers(),
method: "GET",
path: "/",
search: undefined,
searchParams: new URLSearchParams(),
url: "/"
},
response: {
status: Status.OK,
body: undefined,
headers: new Headers()
}
} as unknown) as Context<S>;
}
test(async function testCompose() {
const callStack: number[] = [];
const mockContext = createMockContext();
const mw1: Middleware = async (context, next) => {
assertStrictEq(context, mockContext);
assertEquals(typeof next, "function");
callStack.push(1);
await next();
};
const mw2: Middleware = async (context, next) => {
assertStrictEq(context, mockContext);
assertEquals(typeof next, "function");
callStack.push(2);
await next();
};
await compose([mw1, mw2])(mockContext);
assertEquals(callStack, [1, 2]);
});