1 package handlers
2
3 import (
4 "net/http"
5 "net/http/httptest"
6 "testing"
7 )
8
9 type headerTable struct {
10 key string
11 val string
12 expected string
13 }
14
15 func TestGetIP(t *testing.T) {
16 headers := []headerTable{
17 {xForwardedFor, "8.8.8.8", "8.8.8.8"},
18 {xForwardedFor, "8.8.8.8, 8.8.4.4", "8.8.8.8"},
19 {xForwardedFor, "[2001:db8:cafe::17]:4711", "[2001:db8:cafe::17]:4711"},
20 {xForwardedFor, "", ""},
21 {xRealIP, "8.8.8.8", "8.8.8.8"},
22 {xRealIP, "8.8.8.8, 8.8.4.4", "8.8.8.8, 8.8.4.4"},
23 {xRealIP, "[2001:db8:cafe::17]:4711", "[2001:db8:cafe::17]:4711"},
24 {xRealIP, "", ""},
25 {forwarded, `for="_gazonk"`, "_gazonk"},
26 {forwarded, `For="[2001:db8:cafe::17]:4711`, `[2001:db8:cafe::17]:4711`},
27 {forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, `192.0.2.60`},
28 {forwarded, `for=192.0.2.43, for=198.51.100.17`, "192.0.2.43"},
29 {forwarded, `for="workstation.local",for=198.51.100.17`, "workstation.local"},
30 }
31
32 for _, v := range headers {
33 req := &http.Request{
34 Header: http.Header{
35 v.key: []string{v.val},
36 }}
37 res := getIP(req)
38 if res != v.expected {
39 t.Fatalf("wrong header for %s: got %s want %s", v.key, res,
40 v.expected)
41 }
42 }
43 }
44
45 func TestGetScheme(t *testing.T) {
46 headers := []headerTable{
47 {xForwardedProto, "https", "https"},
48 {xForwardedProto, "http", "http"},
49 {xForwardedProto, "HTTP", "http"},
50 {xForwardedScheme, "https", "https"},
51 {xForwardedScheme, "http", "http"},
52 {xForwardedScheme, "HTTP", "http"},
53 {forwarded, `For="[2001:db8:cafe::17]:4711`, ""},
54 {forwarded, `for=192.0.2.43, for=198.51.100.17;proto=https`, "https"},
55 {forwarded, `for=172.32.10.15; proto=https;by=127.0.0.1`, "https"},
56 {forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, "http"},
57 }
58
59 for _, v := range headers {
60 req := &http.Request{
61 Header: http.Header{
62 v.key: []string{v.val},
63 },
64 }
65 res := getScheme(req)
66 if res != v.expected {
67 t.Fatalf("wrong header for %s: got %s want %s", v.key, res,
68 v.expected)
69 }
70 }
71 }
72
73
74 func TestProxyHeaders(t *testing.T) {
75 rr := httptest.NewRecorder()
76 r := newRequest("GET", "/")
77
78 r.Header.Set(xForwardedFor, "8.8.8.8")
79 r.Header.Set(xForwardedProto, "https")
80 r.Header.Set(xForwardedHost, "google.com")
81 var (
82 addr string
83 proto string
84 host string
85 )
86 ProxyHeaders(http.HandlerFunc(
87 func(w http.ResponseWriter, r *http.Request) {
88 addr = r.RemoteAddr
89 proto = r.URL.Scheme
90 host = r.Host
91 })).ServeHTTP(rr, r)
92
93 if rr.Code != http.StatusOK {
94 t.Fatalf("bad status: got %d want %d", rr.Code, http.StatusOK)
95 }
96
97 if addr != r.Header.Get(xForwardedFor) {
98 t.Fatalf("wrong address: got %s want %s", addr,
99 r.Header.Get(xForwardedFor))
100 }
101
102 if proto != r.Header.Get(xForwardedProto) {
103 t.Fatalf("wrong address: got %s want %s", proto,
104 r.Header.Get(xForwardedProto))
105 }
106 if host != r.Header.Get(xForwardedHost) {
107 t.Fatalf("wrong address: got %s want %s", host,
108 r.Header.Get(xForwardedHost))
109 }
110
111 }
112
View as plain text