-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_test.go
More file actions
121 lines (118 loc) · 2.27 KB
/
Copy pathconfig_test.go
File metadata and controls
121 lines (118 loc) · 2.27 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
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
112
113
114
115
116
117
118
119
120
121
package main
import (
"net"
"testing"
)
func TestConfig_validate(t *testing.T) {
type fields struct {
Port int
BaseAssetsPath string
SecureKeys []string
}
init := func(opts ...func(f *fields)) fields {
f := fields{
Port: 1234,
BaseAssetsPath: "/path/to/assets",
SecureKeys: []string{"secure key"},
}
for _, opt := range opts {
opt(&f)
}
return f
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "Success Case 1",
fields: init(),
wantErr: false,
},
{
name: "Bad Port",
fields: init(func(f *fields) {
f.Port = -1
}),
wantErr: true,
},
{
name: "Empty Assets Path",
fields: init(func(f *fields) {
f.BaseAssetsPath = ""
}),
wantErr: true,
},
{
name: "Empty Secure Key",
fields: init(func(f *fields) {
f.SecureKeys = []string{}
}),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
Port: tt.fields.Port,
BaseAssetsPath: tt.fields.BaseAssetsPath,
SecureKeys: tt.fields.SecureKeys,
}
if got := c.validate(); tt.wantErr != (got != nil) {
t.Errorf("Config.validate() = %v, want error? %v", got, tt.wantErr)
}
})
}
}
func TestTrustedProxy_UnmarshalText(t *testing.T) {
tests := []struct {
name string
text string
want net.IPNet
wantErr bool
}{
{
name: "Valid CIDR",
text: "192.168.0.0/16",
want: net.IPNet{
IP: net.ParseIP("192.168.0.0"),
Mask: net.CIDRMask(16, 32),
},
wantErr: false,
},
{
name: "Valid IP",
text: "127.0.0.1",
want: net.IPNet{
IP: net.ParseIP("127.0.0.1"),
Mask: net.CIDRMask(32, 32),
},
wantErr: false,
},
{
name: "Invalid IP",
text: "invalid",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tp TrustedProxy
gotErr := tp.UnmarshalText([]byte(tt.text))
got := tp.IPNet
if gotErr != nil {
if !tt.wantErr {
t.Errorf("UnmarshalText() failed: %v", gotErr)
}
return
}
if got.String() != tt.want.String() {
t.Errorf("UnmarshalText() = %v, want %v", got.String(), tt.want.String())
}
if tt.wantErr {
t.Fatal("UnmarshalText() succeeded unexpectedly")
}
})
}
}