-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealip_test.go
61 lines (55 loc) · 1.25 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
package rip
import (
"net/http"
"testing"
)
func TestFromRequest(t *testing.T) {
// helper for testing
newRequest := func(remoteAddr, xRealIP string, xForwardedFor ...string) *http.Request {
h := http.Header{}
h.Set("X-Real-IP", xRealIP)
for _, address := range xForwardedFor {
h.Set("X-Forwarded-For", address)
}
return &http.Request{
RemoteAddr: remoteAddr,
Header: h,
}
}
// init test data
publicAddr1 := "144.12.54.87"
publicAddr2 := "119.14.55.11"
localAddr := "127.0.0.0"
testData := []struct {
name string
request *http.Request
expected string
}{
{
name: "No header",
request: newRequest(publicAddr1, ""),
expected: publicAddr1,
},
{
name: "Has X-Forwarded-For",
request: newRequest("", "", publicAddr1),
expected: publicAddr1,
},
{
name: "Has multiple X-Forwarded-For",
request: newRequest("", "", localAddr, publicAddr1, publicAddr2),
expected: publicAddr2,
},
{
name: "Has X-Real-IP",
request: newRequest("", publicAddr1),
expected: publicAddr1,
},
}
// Run test
for _, v := range testData {
if actual := FromRequest(v.request, nil); v.expected != actual {
t.Errorf("%s: expected %s but get %s", v.name, v.expected, actual)
}
}
}