-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.test.ts
106 lines (99 loc) 路 2.88 KB
/
handlers.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
// Copyright 2022-2024 the oak authors. All rights reserved.
import { assert, assertEquals, assertRejects } from "./deps_test.ts";
import { errors, Status } from "./deps.ts";
import { Context } from "./context.ts";
import type { Addr, RequestEvent } from "./types_internal.ts";
import { auth, immutable } from "./handlers.ts";
function createRequestEvent(
input: RequestInfo | URL,
init?: RequestInit,
): RequestEvent {
const request = new Request(input, init);
return {
get addr(): Addr {
return { transport: "tcp", hostname: "127.0.0.1", port: 8000 };
},
get request() {
return request;
},
error(_reason) {},
respond(_response) {},
};
}
Deno.test({
name: "handler - auth() - authorized",
async fn() {
const handlerWithOptions = auth(
() => ({ hello: "world" }),
{ authorize: () => true },
);
const context = new Context({
requestEvent: createRequestEvent("https://example.com/"),
params: {},
headers: new Headers(),
});
const response = await handlerWithOptions.handler(context);
assertEquals(response, { hello: "world" });
},
});
Deno.test({
name: "handler - auth() - unauthorized",
async fn() {
const handlerWithOptions = auth(
() => ({ hello: "world" }),
{ authorize: () => false },
);
const context = new Context({
requestEvent: createRequestEvent("https://example.com/"),
params: {},
headers: new Headers(),
});
await assertRejects(
async () => {
await handlerWithOptions.handler(context);
},
errors.Unauthorized,
"Unauthorized",
);
},
});
Deno.test({
name: "handler - auth() - return body init",
async fn() {
const handlerWithOptions = auth(
() => ({ hello: "world" }),
{ authorize: () => "not authorized" },
);
const context = new Context({
requestEvent: createRequestEvent("https://example.com/"),
params: {},
headers: new Headers(),
});
const response = await handlerWithOptions.handler(context);
assert(response instanceof Response);
assertEquals(response.status, Status.Unauthorized);
assertEquals(await response.text(), "not authorized");
},
});
Deno.test({
name: "handler - immutable() - no options",
async fn() {
const handlerWithOptions = immutable({ hello: "world" });
assertEquals(Object.keys(handlerWithOptions), ["handler"]);
const context = new Context({
requestEvent: createRequestEvent("https://example.com/"),
params: {},
headers: new Headers(),
});
const response = await handlerWithOptions.handler(context);
assert(response instanceof Response);
assertEquals([...response.headers], [[
"cache-control",
"public, max-age=604800, immutable",
], [
"content-type",
"application/json; charset=UTF-8",
]]);
assertEquals(await response.text(), `{"hello":"world"}`);
},
});