-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_test.go
65 lines (59 loc) · 1.16 KB
/
extract_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
package jwtmw
import (
"fmt"
"net/http"
"testing"
)
func TestBearerTokenFromRequest(t *testing.T) {
for k, tc := range []struct {
h http.Header
tok string
err bool
exist bool
}{
{
h: http.Header{BearerHeaderKey: {"Bearer jwt"}},
tok: "jwt",
exist: true,
},
{
// header is case sensitive
h: http.Header{"authorization": {"Bearer jwt"}},
tok: "",
exist: false,
},
{
// bearer is in case sensitive
h: http.Header{BearerHeaderKey: {"BeAReR jwt"}},
tok: "jwt",
exist: true,
},
{
h: http.Header{BearerHeaderKey: {"wrong jwt"}},
err: true,
},
{
h: http.Header{BearerHeaderKey: {}},
},
{
h: http.Header{BearerHeaderKey: {"jwt"}},
err: true,
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
tok, err := BearerTokenFromRequest(&http.Request{Header: tc.h})
if !tc.err && err != nil {
t.Errorf("unexpected error")
}
if tc.err && err == nil {
t.Errorf("expected error")
}
if tc.tok != tok {
t.Errorf("tokens are not equal: %s != %s", tc.tok, tok)
}
if tc.exist && tok == "" {
t.Errorf("expected token to exist")
}
})
}
}