-
Notifications
You must be signed in to change notification settings - Fork 4
/
basic_auth_test.ts
80 lines (74 loc) · 2.26 KB
/
basic_auth_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
import { BasicAuth, parseBasicAuth } from "./basic_auth.ts";
import { assertEquals, assertThrows, describe, it } from "./test_deps.ts";
import { InvalidClientError } from "./errors.ts";
const parseBasicAuthTests = describe("parseBasicAuth");
it(parseBasicAuthTests, "authorization header required", () => {
assertThrows(
() => parseBasicAuth(null),
InvalidClientError,
"authorization header required",
);
assertThrows(
() => parseBasicAuth(""),
InvalidClientError,
"authorization header required",
);
});
it(parseBasicAuthTests, "unsupported authorization header", () => {
assertThrows(
() => parseBasicAuth("x"),
InvalidClientError,
"unsupported authorization header",
);
assertThrows(
() => parseBasicAuth("basic"),
InvalidClientError,
"unsupported authorization header",
);
assertThrows(
() => parseBasicAuth("Bearer mF_9.B5f-4.1JqM"),
InvalidClientError,
"unsupported authorization header",
);
});
it(
parseBasicAuthTests,
"authorization header is not correctly encoded",
() => {
assertThrows(
() => parseBasicAuth("basic x"),
InvalidClientError,
"authorization header is not correctly encoded",
);
assertThrows(
() => parseBasicAuth(`basic ${btoa("kyle")}=`),
InvalidClientError,
"authorization header is not correctly encoded",
);
},
);
it(parseBasicAuthTests, "authorization header is malformed", () => {
assertThrows(
() => parseBasicAuth(`basic ${btoa("kyle")}`),
InvalidClientError,
"authorization header is malformed",
);
assertThrows(
() => parseBasicAuth(`basic ${btoa(":")}`),
InvalidClientError,
"authorization header is malformed",
);
assertThrows(
() => parseBasicAuth(`basic ${btoa(":hunter2")}`),
InvalidClientError,
"authorization header is malformed",
);
});
it(parseBasicAuthTests, "returns correct name and pass", () => {
let basicAuth: BasicAuth = parseBasicAuth(`basic ${btoa("kyle:")}`);
assertEquals(basicAuth, { name: "kyle", pass: "" });
basicAuth = parseBasicAuth(`BASIC ${btoa("kyle:hunter2")}`);
assertEquals(basicAuth, { name: "kyle", pass: "hunter2" });
basicAuth = parseBasicAuth(`BaSiC ${btoa("Kyle:Hunter2")}`);
assertEquals(basicAuth, { name: "Kyle", pass: "Hunter2" });
});