-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.ts
53 lines (46 loc) · 1.08 KB
/
handler_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
import { Context, createHandler } from "./mod.ts";
import {
connInfo,
subtract5DelayedMiddleware,
tryMiddleware,
} from "./test_util.ts";
import { assertEquals } from "./test_deps.ts";
type State = { result: number };
const request = new Request("https:example.com/books");
class Ctx extends Context<State> {}
function catchMiddleware(ctx: Ctx) {
ctx.state.result = 1;
return ctx;
}
function finallyMiddleware(ctx: Ctx) {
ctx.response = new Response(ctx.state.result.toString());
return ctx;
}
function throwMiddleware(_ctx: Ctx): never {
throw new Error("uups");
}
Deno.test("createHandler", async function () {
assertEquals(
await (await createHandler(Ctx, { state: { result: 10 } })(tryMiddleware)(
subtract5DelayedMiddleware,
)(finallyMiddleware)(
request,
connInfo,
)).text(),
"28",
);
assertEquals(
await (await createHandler(Ctx, { state: { result: 10 } })(
tryMiddleware,
throwMiddleware,
)(
catchMiddleware,
)(
finallyMiddleware,
)(
request,
connInfo,
)).text(),
"1",
);
});