This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
config_test.go
111 lines (91 loc) · 2.05 KB
/
config_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
104
105
106
107
108
109
110
111
package jwtware
import (
"testing"
)
func TestPanicOnMissingConfiguration(t *testing.T) {
t.Parallel()
defer func() {
// Assert
if err := recover(); err == nil {
t.Fatalf("Middleware should panic on missing configuration")
}
}()
// Arrange
config := make([]Config, 0)
// Act
makeCfg(config)
}
func TestDefaultConfiguration(t *testing.T) {
t.Parallel()
defer func() {
// Assert
if err := recover(); err != nil {
t.Fatalf("Middleware should not panic")
}
}()
// Arrange
config := append(make([]Config, 0), Config{
SigningKey: SigningKey{Key: []byte("")},
})
// Act
cfg := makeCfg(config)
// Assert
if cfg.ContextKey != "user" {
t.Fatalf("Default context key should be 'user'")
}
if cfg.Claims == nil {
t.Fatalf("Default claims should not be 'nil'")
}
if cfg.TokenLookup != defaultTokenLookup {
t.Fatalf("Default token lookup should be '%v'", defaultTokenLookup)
}
if cfg.AuthScheme != "Bearer" {
t.Fatalf("Default auth scheme should be 'Bearer'")
}
}
func TestExtractorsInitialization(t *testing.T) {
t.Parallel()
defer func() {
// Assert
if err := recover(); err != nil {
t.Fatalf("Middleware should not panic")
}
}()
// Arrange
cfg := Config{
SigningKey: SigningKey{Key: []byte("")},
TokenLookup: defaultTokenLookup + ",query:token,param:token,cookie:token,something:something",
}
// Act
extractors := cfg.getExtractors()
// Assert
if len(extractors) != 4 {
t.Fatalf("Extractors should not be created for invalid lookups")
}
if cfg.AuthScheme != "" {
t.Fatal("AuthScheme should be \"\"")
}
}
func TestCustomTokenLookup(t *testing.T) {
t.Parallel()
defer func() {
// Assert
if err := recover(); err != nil {
t.Fatalf("Middleware should not panic")
}
}()
// Arrange
lookup := `header:X-Auth`
scheme := "Token"
cfg := Config{
SigningKey: SigningKey{Key: []byte("")},
TokenLookup: lookup,
AuthScheme: scheme,
}
if cfg.TokenLookup != lookup {
t.Fatalf("TokenLookup should be %s", lookup)
}
if cfg.AuthScheme != scheme {
t.Fatalf("AuthScheme should be %s", scheme)
}
}