-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
40 lines (38 loc) · 1.44 KB
/
index.test.ts
File metadata and controls
40 lines (38 loc) · 1.44 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
import { describe, expect, test } from "bun:test";
import { readBasicAuth, writeBasicAuth } from ".";
describe("writeBasicAuth", () => {
test("example from IETF RFC 2617", () => {
expect(writeBasicAuth({ username: "Aladdin", password: "open sesame" }))
.toBe("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
})
test("example", async () => {
expect(writeBasicAuth({ username: "chris", password: "abcdef" }))
.toBe('Basic Y2hyaXM6YWJjZGVm')
})
test("credentials can be blank", async () => {
expect(writeBasicAuth({ username: "", password: "" }))
.toBe('Basic Og==')
})
})
describe("readBasicAuth", () => {
test("example from IETF RFC 2617", () => {
expect(readBasicAuth("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="))
.toEqual({ success: true, username: "Aladdin", password: "open sesame" })
})
test("inverts writeBasicAuth", () => {
const examples = [
{ username: "chris", password: "abcdef" },
{ username: "julie", password: "xyz" },
{ username: "", password: "" }
]
examples.forEach(x => {
expect(readBasicAuth(writeBasicAuth(x))).toEqual({ ...x, success: true })
})
})
test("rejects without Basic prefix", () => {
expect(readBasicAuth("Complicated Y2hyaXM6YWJjZGVm")).toEqual({ success: false })
})
test("rejects without a colon in the base64 content", () => {
expect(readBasicAuth(`Basic ${Buffer.from('whatever').toString("base64")}`)).toEqual({ success: false })
})
})