-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_ip_test.go
45 lines (38 loc) · 1.22 KB
/
client_ip_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
package goweb_test
import (
"net/http/httptest"
"testing"
"github.com/sehrgutesoftware/goweb"
"github.com/stretchr/testify/assert"
)
func TestItExtractsTheClientIPFromTheRemoteAddr(t *testing.T) {
}
func TestItExtractsTheClientIPFromProxyHeaders(t *testing.T) {
// X-Forwarded-For is highest priority
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("X-Forwarded-For", "42.42.42.42")
r.Header.Set("X-Real-IP", "21.21.21.21")
r.RemoteAddr = "0.0.0.0:123"
ip, err := goweb.ClientIP(r)
assert.NoError(t, err)
assert.Equal(t, "42.42.42.42", ip)
// X-Real-IP is second priority
r = httptest.NewRequest("GET", "/", nil)
r.Header.Set("X-Real-IP", "21.21.21.21")
r.RemoteAddr = "0.0.0.0:123"
ip, err = goweb.ClientIP(r)
assert.NoError(t, err)
assert.Equal(t, "21.21.21.21", ip)
// Fall back to RemoteAddr
r = httptest.NewRequest("GET", "/", nil)
r.RemoteAddr = "0.0.0.0:123"
ip, err = goweb.ClientIP(r)
assert.NoError(t, err)
assert.Equal(t, "0.0.0.0", ip)
// First element in the list is the original client
r = httptest.NewRequest("GET", "/", nil)
r.Header.Set("X-Forwarded-For", "42.42.42.42,31.31.31.31,20.20.20.20")
ip, err = goweb.ClientIP(r)
assert.NoError(t, err)
assert.Equal(t, "42.42.42.42", ip)
}