-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup_test.go
124 lines (121 loc) · 2.94 KB
/
lookup_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
package clientip_test
import (
"net/http"
"testing"
"github.com/dmitrymomot/clientip"
)
func TestLookupFromRequest(t *testing.T) {
type args struct {
r *http.Request
headers []string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Default headers",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "48.135.12.111")
r.Header.Set("X-Real-IP", "48.135.12.111")
r.RemoteAddr = "230.173.15.154:1234"
return r
}(),
},
want: "48.135.12.111",
},
{
name: "Custom headers",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "48.135.12.111")
r.Header.Set("X-Real-IP", "48.135.12.111")
r.Header.Set("Custom-IP", "212.207.103.215")
r.RemoteAddr = "230.173.15.154:1234"
return r
}(),
headers: []string{"Custom-IP"},
},
want: "212.207.103.215",
},
{
name: "X-Forwarded-For",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "48.135.12.111")
r.RemoteAddr = "230.173.15.154:1234"
return r
}(),
},
want: "48.135.12.111",
},
{
name: "X-Forwarded-For: multiple addresses",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "48.135.12.111, 181.95.251.176")
r.RemoteAddr = "230.173.15.154:1234"
return r
}(),
},
want: "48.135.12.111",
},
{
name: "default remote address",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.RemoteAddr = "230.173.15.154:1234"
return r
}(),
},
want: "230.173.15.154",
},
{
name: "X-Forwarded-For: ipv6 address",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "42e7:9f02:2ced:9303:2691:cd2e:7f9d:8ae3")
return r
}(),
},
want: "42e7:9f02:2ced:9303::", // /64 prefix.
},
{
name: "invalid ip address: parse error",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "123:456789")
return r
}(),
},
want: "",
},
{
name: "not an ip address at all: no ip address found in the request",
args: args{
r: func() *http.Request {
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("X-Forwarded-For", "123456789")
return r
}(),
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := clientip.LookupFromRequest(tt.args.r, tt.args.headers...); got != tt.want {
t.Errorf("LookupFromRequest() = %v, want %v", got, tt.want)
}
})
}
}