-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
middleware_test.ts
63 lines (63 loc) · 1.67 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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { basicAuth } from "./middleware.ts";
import { group } from "./_test_util.ts";
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { createRecorder } from "./testing.ts";
group("middleware", ({ test }) => {
test("basicAuth", async () => {
const auth = basicAuth({
credentials: [{
username: "deno",
password: "land",
}],
message: "hello",
});
let req = createRecorder({
url: "/",
method: "GET",
});
await auth(req);
let resp = await req.response();
assertEquals(resp.status, 401);
assertEquals(resp.headers.has("www-authenticate"), true);
assertEquals(await resp.text(), "hello");
const up = btoa("deno:land");
req = createRecorder({
url: "/",
method: "GET",
headers: new Headers({
"authorization": `Basic ${up}`,
}),
});
await auth(req);
assertEquals(req.isResponded(), false);
});
test("basicAuth failed", async () => {
const patterns = [
"Basic hoge",
`Basic ${btoa("deno:js")}`,
`Basic ${btoa("deno:")}`,
"Basic",
];
const auth = basicAuth({
credentials: [{
username: "deno",
password: "land",
}],
message: "hello",
});
for (const pat of patterns) {
let req = createRecorder({
url: "/",
method: "GET",
headers: new Headers({
"authorization": pat,
}),
});
await auth(req);
assertEquals(req.isResponded(), true);
const resp = await req.response();
assertEquals(resp.status, 401);
}
});
});