MCPcopy
hub / github.com/grafana/dskit / TestGetSourceIPs

Function TestGetSourceIPs

middleware/source_ips_test.go:14–203  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestGetSourceIPs(t *testing.T) {
15 tests := []struct {
16 name string
17 req *http.Request
18 want string
19 }{
20 {
21 name: "no header",
22 req: &http.Request{RemoteAddr: "192.168.1.100:3454"},
23 want: "192.168.1.100",
24 },
25 {
26 name: "no header and remote has no port",
27 req: &http.Request{RemoteAddr: "192.168.1.100"},
28 want: "192.168.1.100",
29 },
30 {
31 name: "no header, remote address is invalid",
32 req: &http.Request{RemoteAddr: "192.168.100"},
33 want: "192.168.100",
34 },
35 {
36 name: "X-Forwarded-For and single forward address",
37 req: &http.Request{
38 RemoteAddr: "192.168.1.100:3454",
39 Header: map[string][]string{
40 http.CanonicalHeaderKey(xForwardedFor): {"172.16.1.1"},
41 },
42 },
43 want: "172.16.1.1, 192.168.1.100",
44 },
45 {
46 name: "X-Forwarded-For and single forward address which is same as remote",
47 req: &http.Request{
48 RemoteAddr: "192.168.1.100:3454",
49 Header: map[string][]string{
50 http.CanonicalHeaderKey(xForwardedFor): {"192.168.1.100"},
51 },
52 },
53 want: "192.168.1.100",
54 },
55 {
56 name: "single IPv6 X-Forwarded-For address",
57 req: &http.Request{
58 RemoteAddr: "[2001:db9::1]:3454",
59 Header: map[string][]string{
60 http.CanonicalHeaderKey(xForwardedFor): {"2001:db8::1"},
61 },
62 },
63 want: "2001:db8::1, 2001:db9::1",
64 },
65 {
66 name: "single X-Forwarded-For address no RemoteAddr",
67 req: &http.Request{
68 Header: map[string][]string{
69 http.CanonicalHeaderKey(xForwardedFor): {"172.16.1.1"},
70 },
71 },

Callers

nothing calls this directly

Calls 4

NewSourceIPsFunction · 0.85
RunMethod · 0.80
ErrorfMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected