-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup_test.go
125 lines (118 loc) · 2.52 KB
/
setup_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package proxyprotocol
import (
"testing"
"time"
"github.com/caddyserver/caddy"
"github.com/stretchr/testify/assert"
)
func TestParseConfig(t *testing.T) {
type exp struct {
subnet string
timeout time.Duration
}
check := func(name, cfg string, expected ...exp) {
t.Run(name, func(t *testing.T) {
cfgs, err := parseConfig(caddy.NewTestController("http", cfg))
assert.NoError(t, err)
assert.Len(t, cfgs, len(expected))
for i, exp := range expected {
if len(cfgs) <= i {
break
}
cfg := cfgs[i]
assert.Equal(t, exp.subnet, cfg.Subnet.String(), "Subnet")
assert.Equal(t, exp.timeout.String(), cfg.Timeout.String(), "Timeout")
}
})
}
check(
"empty",
``,
)
check(
"default",
`proxyprotocol`,
exp{subnet: "0.0.0.0/0", timeout: 5 * time.Second},
exp{subnet: "::/0", timeout: 5 * time.Second},
)
check(
"default-options",
`proxyprotocol {
timeout 1s
}`,
exp{subnet: "0.0.0.0/0", timeout: time.Second},
exp{subnet: "::/0", timeout: time.Second},
)
check(
"single-subnet",
`proxyprotocol 127.0.0.1/32`,
exp{subnet: "127.0.0.1/32", timeout: 5 * time.Second},
)
check(
"multi-subnet",
`proxyprotocol 0.0.0.0/0 ::/0`,
exp{subnet: "0.0.0.0/0", timeout: 5 * time.Second},
exp{subnet: "::/0", timeout: 5 * time.Second},
)
check(
"duplicate",
`proxyprotocol 0.0.0.0/0
proxyprotocol ::/0`,
exp{subnet: "0.0.0.0/0", timeout: 5 * time.Second},
exp{subnet: "::/0", timeout: 5 * time.Second},
)
check(
"no-timeout",
`proxyprotocol 0.0.0.0/0 {
timeout 0
}`,
exp{subnet: "0.0.0.0/0"},
)
check(
"no-timeout",
`proxyprotocol 0.0.0.0/0 {
timeout none
}`,
exp{subnet: "0.0.0.0/0"},
)
check(
"block-single",
`proxyprotocol 0.0.0.0/0 {
timeout 2s
}`,
exp{subnet: "0.0.0.0/0", timeout: 2 * time.Second},
)
check(
"block-multi",
`proxyprotocol 0.0.0.0/0 1234:321::1/24 {
timeout 25m
}`,
exp{subnet: "0.0.0.0/0", timeout: 25 * time.Minute},
// normalized subnet str
exp{subnet: "1234:300::/24", timeout: 25 * time.Minute},
)
check(
"block-duplicate",
`proxyprotocol 0.0.0.0/0 {
timeout 25m
}
proxyprotocol 1234:321::1/24 {
timeout 30m
}`,
exp{subnet: "0.0.0.0/0", timeout: 25 * time.Minute},
exp{subnet: "1234:300::/24", timeout: 30 * time.Minute},
)
check(
"multi-site",
`example.com {
proxyprotocol 0.0.0.0/0
}
foo.com {
proxyprotocol 1234:321::1/24 {
timeout 30m
}
}`,
exp{subnet: "0.0.0.0/0", timeout: 5 * time.Second},
exp{subnet: "1234:300::/24", timeout: 30 * time.Minute},
)
}