-
Notifications
You must be signed in to change notification settings - Fork 235
/
testing.test.ts
98 lines (89 loc) · 2.29 KB
/
testing.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
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
import { assert } from "./deps.ts";
import { assertEquals, assertStrictEquals } from "./deps_test.ts";
import {
createMockApp,
createMockContext,
createMockNext,
mockContextState,
} from "./testing.ts";
Deno.test({
name: "testing - createMockApp()",
fn() {
const app = createMockApp();
assertEquals(app.state, {});
},
});
Deno.test({
name: "testing - createMockApp() - with state",
fn() {
const app = createMockApp({ a: "a" });
assertEquals(app.state, { a: "a" });
},
});
Deno.test({
name: "testing - createMockContext()",
fn() {
const ctx = createMockContext();
assert(ctx.app);
assertEquals(ctx.request.method, "GET");
assertStrictEquals(ctx.params, undefined);
assertEquals(ctx.request.url.pathname, "/");
assertEquals(ctx.state, {});
assertEquals(ctx.request.acceptsEncodings("identity"), "identity");
ctx.response.redirect("/hello/world");
assertEquals(ctx.response.headers.get("Location"), "/hello/world");
},
});
Deno.test({
name: "testing - mockContextState",
fn() {
mockContextState.encodingsAccepted = "gzip";
const ctx = createMockContext();
try {
assertEquals(ctx.request.acceptsEncodings("gzip"), "gzip");
} finally {
mockContextState.encodingsAccepted = "identity";
}
},
});
Deno.test({
name: "testing - ctx.cookies.set()",
async fn() {
const ctx = createMockContext();
await ctx.cookies.set(
"sessionID",
"S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro",
{ httpOnly: true },
);
assertEquals([...ctx.response.headers], [
[
"set-cookie",
"sessionID=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro; path=/; httponly",
],
]);
},
});
Deno.test({
name: "testing - ctx.cookies.get()",
async fn() {
const ctx = createMockContext({
headers: [[
"cookie",
"sessionID=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro;",
]],
});
assertEquals(
await ctx.cookies.get("sessionID"),
"S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro",
);
},
});
Deno.test({
name: "testing - createMockNext()",
fn() {
const next = createMockNext();
assertStrictEquals(typeof next, "function");
assert(next() instanceof Promise);
},
});