-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathauth_test.go
More file actions
65 lines (56 loc) · 1.14 KB
/
auth_test.go
File metadata and controls
65 lines (56 loc) · 1.14 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
package auth
import (
"fmt"
"net/http"
"strings"
"testing"
)
func TestGetAPIKey(t *testing.T) {
tests := []struct {
key string
value string
expect string
expectErr string
}{
{
expectErr: "no authorization header",
},
{
key: "Authorization",
expectErr: "no authorization header",
},
{
key: "Authorization",
value: "-",
expectErr: "malformed authorization header",
},
{
key: "Authorization",
value: "Bearer xxxxxx",
expectErr: "malformed authorization header",
},
{
key: "Authorization",
value: "ApiKey xxxxxx",
expect: "xxxxxx",
expectErr: "not expecting an error",
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("TestGetAPIKey Case # %v:", i), func(t *testing.T) {
header := http.Header{}
header.Add(tc.key, tc.value)
got, err := GetAPIKey(header)
if err != nil {
if strings.Contains(err.Error(), tc.expectErr) {
return
}
t.Errorf("Unexpected: TestGetAPIKey: %v\n", err)
}
if got != tc.expect {
t.Errorf("Expected: TestGetAPIKey: %v\n Actual: %v", tc.expect, got)
return
}
})
}
}