-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
onlyfrom_test.go
152 lines (131 loc) · 3.86 KB
/
onlyfrom_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
package rest
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOnlyFromAllowedIP(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(OnlyFrom("127.0.0.1")(handler))
defer ts.Close()
resp, err := http.Get(ts.URL + "/blah")
require.Nil(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "blah blah", string(b))
}
func TestOnlyFromAllowedHeaders(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(OnlyFrom("1.1.1.1")(handler))
defer ts.Close()
reqWithHeader := func(header string) (*http.Request, error) {
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
return nil, err
}
req.Header.Set(header, "1.1.1.1")
return req, err
}
client := http.Client{}
t.Run("X-Real-IP", func(t *testing.T) {
req, err := reqWithHeader("X-Real-IP")
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)
})
t.Run("X-Forwarded-For", func(t *testing.T) {
req, err := reqWithHeader("X-Forwarded-For")
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)
})
t.Run("X-Forwarded-For and X-Real-IP missing", func(t *testing.T) {
req, err := reqWithHeader("blah")
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 403, resp.StatusCode)
})
}
func TestOnlyFromAllowedCIDR(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(OnlyFrom("1.1.1.0/24")(handler))
defer ts.Close()
client := http.Client{}
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
require.NoError(t, err)
req.Header.Set("X-Real-IP", "1.1.1.1")
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)
req.Header.Set("X-Real-IP", "1.1.2.0")
resp, err = client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 403, resp.StatusCode)
}
func TestOnlyFromRejected(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(OnlyFrom("127.0.0.2")(handler))
defer ts.Close()
resp, err := http.Get(ts.URL + "/blah")
require.Nil(t, err)
defer resp.Body.Close()
assert.Equal(t, 403, resp.StatusCode)
}
func TestOnlyFromErrors(t *testing.T) {
tests := []struct {
name string
remoteAddr string
status int
}{
{
name: "Invalid RemoteAddr",
remoteAddr: "bad-addr",
status: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.RemoteAddr = tt.remoteAddr
OnlyFrom("1.1.1.1")(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})).ServeHTTP(w, r)
})
ts := httptest.NewServer(handler)
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
require.NoError(t, err)
client := http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, tt.status, resp.StatusCode)
})
}
}