-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.test.js
More file actions
61 lines (52 loc) · 1.42 KB
/
lib.test.js
File metadata and controls
61 lines (52 loc) · 1.42 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
const a = require('assert')
const { encodings } = require("./lib");
const RED = 31
const GREEN = 32
const RESET = '\u001b[39m\u001b[49m'
test("URL encoding - match", () => {
a(encodings.url.match("foo%20bar"))
a(!encodings.url.match("foobar"))
a(!encodings.url.match("foo%bar"))
a(!encodings.url.match("foo%%bar"))
})
test("URL encoding - decode", () => {
a(encodings.url.decode("foo%20bar") === "foo bar")
})
test("base64 encoding - match", () => {
a(encodings.base64.match("Zm9vIGJhcg=="))
a(!encodings.base64.match("Zm9vIGJhcg="))
})
test("base64 encoding - decode", () => {
a(encodings.base64.decode("Zm9vIGJhcg==") === "foo bar")
})
const jwt1 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ"
test("JWT encoding - match", () => {
a(encodings.jwt.match(jwt1))
})
test("JWT encoding - decode", () => {
const decoded = encodings.jwt.decode(jwt1)
a.deepEqual(decoded, {
"header": {
"alg": "HS256",
"typ": "JWT"
},
"payload": {
"iat": 1516239022
},
"signature": "tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ"
})
})
function test(name, fn) {
try {
fn()
greenFirst('PASS', name)
} catch (ex) {
redFirst('FAIL', name, ex)
}
}
function greenFirst(text, ...rest) {
console.log(`\u001b[${GREEN}m${text}\u001b${RESET}`, ...rest)
}
function redFirst(text, ...rest) {
console.log(`\u001b[${RED}m${text}\u001b${RESET}`, ...rest)
}