forked from ulule/limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
60 lines (46 loc) · 966 Bytes
/
utils_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
package limiter
import (
"net"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
// TestGetIP tests GetIP() function.
func TestGetIP(t *testing.T) {
//
// RemoteAddr
//
expected := net.ParseIP("8.8.8.8")
r := http.Request{
URL: &url.URL{Path: "/"},
Header: http.Header{},
RemoteAddr: "8.8.8.8:8888",
}
ip := GetIP(&r)
assert.Equal(t, expected, ip)
//
// X-Forwarded-For
//
expected = net.ParseIP("9.9.9.9")
r = http.Request{
URL: &url.URL{Path: "/foo"},
Header: http.Header{},
RemoteAddr: "8.8.8.8:8888",
}
r.Header.Add("X-Forwarded-For", "9.9.9.9, 7.7.7.7, 6.6.6.6")
ip = GetIP(&r)
assert.Equal(t, expected, ip)
//
// X-Real-IP
//
expected = net.ParseIP("6.6.6.6")
r = http.Request{
URL: &url.URL{Path: "/bar"},
Header: http.Header{},
RemoteAddr: "8.8.8.8:8888",
}
r.Header.Add("X-Real-IP", "6.6.6.6")
ip = GetIP(&r)
assert.Equal(t, expected, ip)
}