-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheader_test.go
103 lines (94 loc) · 2.18 KB
/
header_test.go
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
93
94
95
96
97
98
99
100
101
102
103
package basicauth
import "testing"
func TestHeaderEncode(t *testing.T) {
var tests = []struct {
username string
password string
header string
ok bool
}{
{
username: "user",
password: "pass",
header: "Basic dXNlcjpwYXNz",
ok: true,
},
{
username: "user",
password: "p:(notallowed)ass",
header: "",
ok: false,
},
{
username: "123u%ser",
password: "pass132$",
header: "Basic MTIzdSVzZXI6cGFzczEzMiQ=",
ok: true,
},
}
for i, tt := range tests {
got, ok := encodeHeader(tt.username, tt.password)
if tt.ok != ok {
t.Fatalf("[%d] expected: %v but got: %v (username=%s,password=%s)", i, tt.ok, ok, tt.username, tt.password)
}
if tt.header != got {
t.Fatalf("[%d] expected result header: %q but got: %q", i, tt.header, got)
}
}
}
func TestHeaderDecode(t *testing.T) {
var tests = []struct {
header string
ok bool
username string
password string
}{
{
header: "Basic dXNlcjpwYXNz",
ok: true,
username: "user",
password: "pass",
},
{
header: "dXNlcjpwYXNz",
ok: false,
},
{
header: "Basic ",
ok: false,
},
{
header: "Basic dXNlcjp",
ok: false,
},
{
header: "dXNlcjpwYXNz Basic",
ok: false,
},
{
header: "dXNlcjpwYXNzBasic",
ok: false,
},
}
for i, tt := range tests {
fullUser, username, password, ok := decodeHeader(tt.header)
if expected, got := tt.ok, ok; expected != got {
t.Fatalf("[%d] expected: %v but got: %v (header=%s)", i, expected, got, tt.header)
}
if expected, got := tt.username, username; expected != got {
t.Fatalf("[%d] expected username: %q but got: %q", i, expected, got)
}
if expected, got := tt.password, password; expected != got {
t.Fatalf("[%d] expected password: %q but got: %q", i, expected, got)
}
if tt.username != "" || tt.password != "" {
if expected, got := tt.username+colonLiteral+tt.password, fullUser; expected != got {
t.Fatalf("[%d] expected username:password to be: %q but got: %q", i, expected, got)
}
} else {
if fullUser != "" {
t.Fatalf("[%d] expected username:password to be empty but got: %q", i, fullUser)
}
}
}
}