forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.ts
120 lines (112 loc) · 3.17 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
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
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
// deno-lint-ignore-file
import { assert, assertEquals, assertStrictEquals, test } from "./test_deps.ts";
import type { State } from "./application.ts";
import type { Context } from "./context.ts";
import { Status } from "./deps.ts";
import { createHttpError, httpErrors } from "./httpError.ts";
import type { ErrorStatus } from "./types.d.ts";
import { compose, Middleware } from "./middleware.ts";
function createMockContext<S extends State = Record<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(),
},
assert(
condition: any,
errorStatus: ErrorStatus = 500,
message?: string,
props?: object,
): asserts condition {
if (condition) {
return;
}
const err = createHttpError(errorStatus, message);
if (props) {
Object.assign(err, props);
}
throw err;
},
throw(errorStatus: ErrorStatus, message?: string, props?: object): never {
const err = createHttpError(errorStatus, message);
if (props) {
Object.assign(err, props);
}
throw err;
},
} as unknown) as Context<S>;
}
test({
name: "test compose()",
async fn() {
const callStack: number[] = [];
const mockContext = createMockContext();
const mw1: Middleware = async (context, next) => {
assertStrictEquals(context, mockContext);
assertEquals(typeof next, "function");
callStack.push(1);
await next();
};
const mw2: Middleware = async (context, next) => {
assertStrictEquals(context, mockContext);
assertEquals(typeof next, "function");
callStack.push(2);
await next();
};
await compose([mw1, mw2])(mockContext);
assertEquals(callStack, [1, 2]);
},
});
test({
name: "next() is catchable",
async fn() {
let caught: any;
const mw1: Middleware = async (ctx, next) => {
try {
await next();
} catch (err) {
caught = err;
}
};
const mw2: Middleware = async (ctx) => {
ctx.throw(500);
};
const context = createMockContext();
await compose([mw1, mw2])(context);
assert(caught instanceof httpErrors.InternalServerError);
},
});
test({
name: "composed middleware accepts next middleware",
async fn() {
const callStack: number[] = [];
const mockContext = createMockContext();
const mw0: Middleware = async (context, next): Promise<void> => {
assertEquals(typeof next, "function");
callStack.push(3);
await next();
};
const mw1: Middleware = async (context, next) => {
assertEquals(typeof next, "function");
callStack.push(1);
await next();
};
const mw2: Middleware = async (context, next) => {
assertEquals(typeof next, "function");
callStack.push(2);
await next();
};
await compose([mw1, mw2])(mockContext, mw0 as () => Promise<void>);
assertEquals(callStack, [1, 2, 3]);
},
});