-
Notifications
You must be signed in to change notification settings - Fork 1
/
realip_test.go
155 lines (147 loc) · 3.65 KB
/
realip_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
package realip_test
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/natureglobal/realip"
)
func mustParseCIDR(addr string) *net.IPNet {
_, ipnet, err := net.ParseCIDR(addr)
if err != nil {
panic(err)
}
return ipnet
}
func TestMiddleware(t *testing.T) {
testCases := []struct {
name string
config *realip.Config
headers map[string]string
expect string
}{{
name: "X-Real-IP: default",
expect: "127.0.0.1",
}, {
name: "X-Real-IP: no RealIPFrom",
headers: map[string]string{
"x-real-ip": "1.1.1.1",
},
expect: "1.1.1.1",
}, {
name: "X-Real-IP: not a trusted ip from",
config: &realip.Config{
RealIPFrom: []*net.IPNet{mustParseCIDR("192.168.0.0/16")},
},
headers: map[string]string{
"x-real-ip": "1.1.1.1",
},
expect: "127.0.0.1",
}, {
name: "x-forwarded-for",
config: &realip.Config{
RealIPHeader: realip.HeaderXForwardedFor,
},
headers: map[string]string{
"X-Forwarded-For": "192.168.0.1",
},
expect: "192.168.0.1",
}, {
name: "x-forwarded-for: recent non-trusted one",
config: &realip.Config{
RealIPFrom: []*net.IPNet{
mustParseCIDR("127.0.0.1/32"),
mustParseCIDR("192.168.0.0/16"),
},
RealIPHeader: realip.HeaderXForwardedFor,
RealIPRecursive: true,
},
headers: map[string]string{
"X-Forwarded-For": "1.2.3.4, 1.1.1.1, 192.168.0.1",
},
expect: "1.1.1.1",
}, {
name: "x-forwarded-for: recent trusted one",
config: &realip.Config{
RealIPFrom: []*net.IPNet{
mustParseCIDR("127.0.0.1/32"),
mustParseCIDR("192.168.0.0/16"),
},
RealIPHeader: realip.HeaderXForwardedFor,
},
headers: map[string]string{
"X-Forwarded-For": "1.2.3.4, 1.1.1.1, 192.168.0.1",
},
expect: "192.168.0.1",
}, {
name: "x-forwarded-for: remoteAddr is not a trusted address",
config: &realip.Config{
RealIPFrom: []*net.IPNet{
mustParseCIDR("192.168.0.0/16"),
},
RealIPHeader: realip.HeaderXForwardedFor,
},
headers: map[string]string{
"X-Forwarded-For": "1.2.3.4, 1.1.1.1, 192.168.0.1",
},
expect: "127.0.0.1",
}, {
name: "x-forwarded-for: all entries in xff is trusted ip",
config: &realip.Config{
RealIPFrom: []*net.IPNet{
mustParseCIDR("127.0.0.1/32"),
mustParseCIDR("192.168.0.0/16"),
},
RealIPHeader: realip.HeaderXForwardedFor,
RealIPRecursive: true,
},
headers: map[string]string{
"X-Forwarded-For": "192.168.0.2, 192.168.0.1",
},
expect: "192.168.0.2",
}, {
name: "x-forwarded-for: no RealIPFrom config and true RealIPRecursive return left entry in xff",
config: &realip.Config{
RealIPHeader: realip.HeaderXForwardedFor,
RealIPRecursive: true,
},
headers: map[string]string{
"X-Forwarded-For": "192.168.0.2, 192.168.0.1",
},
expect: "192.168.0.2",
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
middleware := realip.MustMiddleware(tc.config)
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, req.Header.Get(realip.HeaderXRealIP))
}))
ts := httptest.NewServer(handler)
defer ts.Close()
req, _ := http.NewRequest(http.MethodGet, ts.URL, nil)
if tc.headers != nil {
for k, v := range tc.headers {
req.Header.Add(k, v)
}
}
r, _ := http.DefaultClient.Do(req)
defer r.Body.Close()
data, _ := ioutil.ReadAll(r.Body)
out := strings.TrimSpace(string(data))
if out != tc.expect {
t.Errorf("out: %s, expect: %s", out, tc.expect)
}
})
}
t.Run("error", func(t *testing.T) {
_, err := realip.Middleware(&realip.Config{
RealIPHeader: "Forwarded",
})
if err == nil {
t.Errorf("error should be occurred but nil")
}
})
}