forked from vikram1565/request-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest-ip_test.go
More file actions
46 lines (43 loc) · 1.42 KB
/
request-ip_test.go
File metadata and controls
46 lines (43 loc) · 1.42 KB
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
package ip
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestGetClientIP(t *testing.T) {
tests := []struct {
headerName string
want string
headerKey string
headerValue string
}{
{"localhost", "127.0.0.1", "", ""},
{"x-client-ip", "172.20.10.4", "x-client-ip", "172.20.10.4"},
{"x-forwarded-for", "172.20.10.4", "x-forwarded-for", "172.20.10.4:8080,182.4.10.4, 180.2.10.10"},
{"cf-connecting-ip", "172.20.10.4", "cf-connecting-ip", "172.20.10.4"},
{"fastly-client-ip", "172.20.10.4", "fastly-client-ip", "172.20.10.4"},
{"true-client-ip", "172.20.10.4", "true-client-ip", "172.20.10.4"},
{"x-real-ip", "172.20.10.4", "x-real-ip", "172.20.10.4"},
{"x-cluster-client-ip", "172.20.10.4", "x-cluster-client-ip", "172.20.10.4"},
{"x-forwarded", "172.20.10.4", "x-forwarded", "172.20.10.4"},
{"forwarded-for", "172.20.10.4", "forwarded-for", "172.20.10.4"},
{"forwarded", "172.20.10.4", "forwarded", "172.20.10.4"},
}
for _, tt := range tests {
t.Run(tt.headerName, func(t *testing.T) {
var got string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tt.headerKey != "" {
r.Header.Set(tt.headerKey, tt.headerValue)
}
got = GetClientIP(r)
t.Log("Client IP :", got)
}))
defer ts.Close()
http.Get(ts.URL)
if got != tt.want {
t.Fatalf("GetClientIP(%v) => got %v, want %v ", tt.headerName, got, tt.want)
}
})
}
}