-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathBasicAuth.test.ts
More file actions
92 lines (83 loc) · 3.05 KB
/
Copy pathBasicAuth.test.ts
File metadata and controls
92 lines (83 loc) · 3.05 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
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
import { BasicAuth } from "../../../src/core/auth/BasicAuth";
describe("BasicAuth", () => {
interface ToHeaderTestCase {
description: string;
input: { username: string; password: string };
expected: string;
}
interface FromHeaderTestCase {
description: string;
input: string;
expected: { username: string; password: string };
}
interface ErrorTestCase {
description: string;
input: string;
expectedError: string;
}
describe("toAuthorizationHeader", () => {
const toHeaderTests: ToHeaderTestCase[] = [
{
description: "correctly converts to header",
input: { username: "username", password: "password" },
expected: "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
},
];
toHeaderTests.forEach(({ description, input, expected }) => {
it(description, () => {
expect(BasicAuth.toAuthorizationHeader(input)).toBe(expected);
});
});
});
describe("fromAuthorizationHeader", () => {
const fromHeaderTests: FromHeaderTestCase[] = [
{
description: "correctly parses header",
input: "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
expected: { username: "username", password: "password" },
},
{
description: "handles password with colons",
input: "Basic dXNlcjpwYXNzOndvcmQ=",
expected: { username: "user", password: "pass:word" },
},
{
description: "handles empty username and password (just colon)",
input: "Basic Og==",
expected: { username: "", password: "" },
},
{
description: "handles empty username",
input: "Basic OnBhc3N3b3Jk",
expected: { username: "", password: "password" },
},
{
description: "handles empty password",
input: "Basic dXNlcm5hbWU6",
expected: { username: "username", password: "" },
},
];
fromHeaderTests.forEach(({ description, input, expected }) => {
it(description, () => {
expect(BasicAuth.fromAuthorizationHeader(input)).toEqual(expected);
});
});
const errorTests: ErrorTestCase[] = [
{
description: "throws error for completely empty credentials",
input: "Basic ",
expectedError: "Invalid basic auth",
},
{
description: "throws error for credentials without colon",
input: "Basic dXNlcm5hbWU=",
expectedError: "Invalid basic auth",
},
];
errorTests.forEach(({ description, input, expectedError }) => {
it(description, () => {
expect(() => BasicAuth.fromAuthorizationHeader(input)).toThrow(expectedError);
});
});
});
});