-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
58 lines (42 loc) · 1.63 KB
/
test.ts
File metadata and controls
58 lines (42 loc) · 1.63 KB
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
import { assert } from "jsr:@std/assert";
const api1 = "http://localhost:8000";
const api2 = "http://localhost:8000/user";
const api3 = "http://localhost:8000/admin";
const { alice, bob } = await fetch("http://localhost:8000/tokens").then((r) => r.json())
Deno.test("annonymous", async () => {
const response1 = await fetch(api1);
assert(response1.status === 200);
const response2 = await fetch(api2);
assert(response2.status === 401);
const response3 = await fetch(api3);
assert(response3.status === 401);
await response1.body?.cancel();
await response2.body?.cancel();
await response3.body?.cancel();
});
Deno.test("alice", async () => {
const headers = new Headers();
headers.set("Authorization", "Bearer " + alice);
const response1 = await fetch(api1, { headers });
assert(response1.status === 200);
const response2 = await fetch(api2, { headers });
assert(response2.status === 200);
const response3 = await fetch(api3, { headers });
assert(response3.status === 200);
await response1.body?.cancel();
await response2.body?.cancel();
await response3.body?.cancel();
});
Deno.test("bob", async () => {
const headers = new Headers();
headers.set("Authorization", "Bearer " + bob);
const response1 = await fetch(api1, { headers });
assert(response1.status === 200);
const response2 = await fetch(api2, { headers });
assert(response2.status === 200);
const response3 = await fetch(api3, { headers });
assert(response3.status === 403);
await response1.body?.cancel();
await response2.body?.cancel();
await response3.body?.cancel();
});