-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdenyip_test.go
160 lines (142 loc) · 3.57 KB
/
denyip_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package denyip_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/kvncrw/denyip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewDenyIP(t *testing.T) {
testCases := []struct {
desc string
denyList []string
expectedError bool
}{
{
desc: "invalid ip",
denyList: []string{"foo"},
expectedError: true,
},
{
desc: "no ip",
denyList: []string{},
expectedError: true,
},
{
desc: "valid ip range",
denyList: []string{"192.168.100.0/24"},
},
{
desc: "valid ip",
denyList: []string{"10.10.10.10"},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
cfg := denyip.CreateConfig()
cfg.IPDenyList = test.denyList
ctx := context.Background()
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
handler, err := denyip.New(ctx, next, cfg, "denyip-plugin")
if test.expectedError {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.NotNil(t, handler)
}
})
}
}
func TestNewDenyIP_ServeHTTP(t *testing.T) {
testCases := []struct {
desc string
denyList []string
remoteAddr string
xff string
expected int
}{
{
desc: "allowed ip with x-forwarded-for and remote address",
denyList: []string{"192.168.100.0/24"},
remoteAddr: "20.20.20.21",
xff: "127.0.0.1, 172.16.100.22, 20.20.20.21",
expected: 200,
},
{
desc: "allowed ip with x-forwarded-for",
denyList: []string{"192.168.100.0/24"},
remoteAddr: "",
xff: "127.0.0.1, 20.20.20.20",
expected: 200,
},
{
desc: "allowed ip with remote address",
denyList: []string{"20.20.20.21"},
remoteAddr: "20.20.20.20:1234",
xff: "",
expected: 200,
},
{
desc: "allowed ip range with remote address",
denyList: []string{"192.168.100.0/24"},
remoteAddr: "20.20.20.20:1234",
xff: "",
expected: 200,
},
{
desc: "denied ip with x-forwarded-for and remote address",
denyList: []string{"192.168.100.0/24"},
remoteAddr: "192.168.100.15",
xff: "127.0.0.1, 172.16.100.25, 192.16.100.15",
expected: 403,
},
{
desc: "denied ip with x-forwarded-for",
denyList: []string{"192.168.100.0/24"},
remoteAddr: "",
xff: "127.0.0.1, 192.168.100.25",
expected: 403,
},
{
desc: "denied ip with remote address",
denyList: []string{"20.20.20.21"},
remoteAddr: "20.20.20.21:1234",
xff: "",
expected: 403,
},
{
desc: "denied ip range with remote address",
denyList: []string{"192.168.100.0/24"},
remoteAddr: "192.168.100.25:1234",
xff: "",
expected: 403,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
recorder := httptest.NewRecorder()
cfg := denyip.CreateConfig()
cfg.IPDenyList = test.denyList
ctx := context.Background()
handler, err := denyip.New(ctx, next, cfg, "denyip-plugin")
require.NoError(t, err)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://10.10.10.10", nil)
require.NoError(t, err)
if test.remoteAddr != "" {
req.RemoteAddr = test.remoteAddr
}
if test.xff != "" {
req.Header.Add("X-Forwarded-For", test.xff)
}
handler.ServeHTTP(recorder, req)
assert.Equal(t, test.expected, recorder.Code)
})
}
}