forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
79 lines (76 loc) · 2.53 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
package authz
import (
"reflect"
"testing"
)
func TestParseAuthorizationHeader(t *testing.T) {
tests := map[string]struct {
token string
sudoUser string
err bool
}{
"token tok": {token: "tok"},
"token tok==": {token: "tok=="},
`token token=tok`: {token: "tok"},
`token token="tok=="`: {token: "tok=="},
`token-sudo token="tok==", user="alice"`: {token: "tok==", sudoUser: "alice"},
`token-sudo token=tok, user="alice"`: {token: "tok", sudoUser: "alice"},
`token-sudo token="tok==", user=alice`: {token: "tok==", sudoUser: "alice"},
"xyz tok": {err: true},
`token-sudo user="alice"`: {err: true},
`token-sudo token="",user="alice"`: {err: true},
`token k=v, k=v`: {err: true},
}
for input, test := range tests {
t.Run(input, func(t *testing.T) {
token, sudoUser, err := ParseAuthorizationHeader(input)
if (err != nil) != test.err {
t.Errorf("got error %v, want error? %v", err, test.err)
}
if err != nil {
return
}
if token != test.token {
t.Errorf("got token %q, want %q", token, test.token)
}
if sudoUser != test.sudoUser {
t.Errorf("got sudoUser %+v, want %+v", sudoUser, test.sudoUser)
}
})
}
}
func TestParseHTTPCredentials(t *testing.T) {
tests := map[string]struct {
scheme string
token68 string
params map[string]string
err bool
}{
"scheme v1": {scheme: "scheme", token68: "v1"},
"scheme v1==": {scheme: "scheme", token68: "v1=="},
`scheme k1="v1"`: {scheme: "scheme", params: map[string]string{"k1": "v1"}},
`scheme-2 k1="v1", k2="v2"`: {scheme: "scheme-2", params: map[string]string{"k1": "v1", "k2": "v2"}},
`scheme-2 k1=v1, k2="v2"`: {scheme: "scheme-2", params: map[string]string{"k1": "v1", "k2": "v2"}},
`scheme k=v, k=v`: {err: true},
}
for input, test := range tests {
t.Run(input, func(t *testing.T) {
scheme, token68, params, err := parseHTTPCredentials(input)
if (err != nil) != test.err {
t.Errorf("got error %v, want error? %v", err, test.err)
}
if err != nil {
return
}
if scheme != test.scheme {
t.Errorf("got scheme %q, want %q", scheme, test.scheme)
}
if token68 != test.token68 {
t.Errorf("got token68 %q, want %q", token68, test.token68)
}
if !reflect.DeepEqual(params, test.params) {
t.Errorf("got params %+v, want %+v", params, test.params)
}
})
}
}