Repository navigation
Expand file tree
/
Copy pathheaders_test.go
More file actions
202 lines (164 loc) · 5.58 KB
/
Copy pathheaders_test.go
File metadata and controls
202 lines (164 loc) · 5.58 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package microproxy
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
// headerCheckHandler answers "OK" when the request carries every header it was
// given, and reports what it did receive otherwise.
type headerCheckHandler struct {
headers map[string]string
}
func (h *headerCheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for name, value := range h.headers {
if r.Header.Get(name) != value {
fmt.Fprintf(w, "FAIL. Headers: %v", r.Header)
return
}
}
fmt.Fprint(w, "OK")
}
func expectHeaders(t *testing.T, cfg Config, headers map[string]string) {
t.Helper()
background := httptest.NewServer(&headerCheckHandler{headers: headers})
defer background.Close()
client, _ := newTestProxy(t, cfg)
resp, err := client.Get(background.URL)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("expected 200 status code, got", resp.Status)
}
if body := readBody(t, resp); body != "OK" {
t.Errorf("expected 'OK', got '%s'", body)
}
}
func TestCustomHeaders(t *testing.T) {
expected := map[string]string{
"X-Custom-Header-1": "Value-1",
"X-Custom-Header-2": "Value-2",
}
expectHeaders(t, Config{
AddHeaders: [][]string{
{"X-Custom-Header-1", "Value-1"},
{"X-Custom-Header-2", "Value-2"},
},
}, expected)
}
func TestViaHeaders(t *testing.T) {
expectHeaders(t,
Config{ViaHeader: "on", ViaProxyName: "octopus"},
map[string]string{"Via": "1.1 octopus"})
}
// A request that already carries a Via header has to leave with a single one
// listing this proxy after the hops it went through, not with the inbound value
// repeated next to the extended one.
func TestViaHeaderAppendedToExistingOne(t *testing.T) {
received := make(chan []string, 1)
background := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
received <- r.Header.Values("Via")
fmt.Fprint(w, "OK")
}))
defer background.Close()
client, _ := newTestProxy(t, Config{ViaHeader: "on", ViaProxyName: "octopus"})
req, err := http.NewRequest(http.MethodGet, background.URL, http.NoBody)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Via", "1.0 upstream-cache")
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
headers := <-received
expected := "1.0 upstream-cache, 1.1 octopus"
if len(headers) != 1 || headers[0] != expected {
t.Errorf("expected a single Via header '%v', got %v", expected, headers)
}
}
// X-Forwarded-For behaves the same way: the client's address is appended to the
// hops already listed, and "truncate" keeps only the client's own.
func TestForwardedForHeader(t *testing.T) {
tests := map[string]struct {
action string
sent string
expected func(clientIP string) string
}{
"on appends": {"on", "10.0.0.1", func(ip string) string { return "10.0.0.1, " + ip }},
"on adds": {"on", "", func(ip string) string { return ip }},
"truncate replaces": {"truncate", "10.0.0.1", func(ip string) string { return ip }},
"delete removes": {"delete", "10.0.0.1", func(string) string { return "" }},
"off leaves it alone": {"off", "10.0.0.1", func(string) string { return "10.0.0.1" }},
"off adds nothing": {"off", "", func(string) string { return "" }},
"delete removes it all": {"delete", "", func(string) string { return "" }},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
received := make(chan string, 1)
background := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
received <- r.Header.Get("X-Forwarded-For")
fmt.Fprint(w, "OK")
}))
defer background.Close()
client, _ := newTestProxy(t, Config{ForwardedForHeader: test.action})
req, err := http.NewRequest(http.MethodGet, background.URL, http.NoBody)
if err != nil {
t.Fatal(err)
}
if test.sent != "" {
req.Header.Set("X-Forwarded-For", test.sent)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
// the proxy connects from the loopback in these tests
if actual, expected := <-received, test.expected("127.0.0.1"); actual != expected {
t.Errorf("expected X-Forwarded-For '%v', got '%v'", expected, actual)
}
})
}
}
// Handlers are registered once, but have to serve the requests that arrive
// after a reload with the reloaded configuration.
func TestReloadedConfigurationIsUsed(t *testing.T) {
background := httptest.NewServer(&headerCheckHandler{
headers: map[string]string{"X-Test-Header": "reloaded"},
})
defer background.Close()
server, err := New(Config{AddHeaders: [][]string{{"X-Test-Header", "startup"}}})
if err != nil {
t.Fatal(err)
}
proxyServer := httptest.NewServer(server.Handler())
defer proxyServer.Close()
if err := server.Reload(Config{AddHeaders: [][]string{{"X-Test-Header", "reloaded"}}}); err != nil {
t.Fatal(err)
}
client := proxyClient(t, proxyServer.URL)
resp, err := client.Get(background.URL)
if err != nil {
t.Fatal(err)
}
if body := readBody(t, resp); body != "OK" {
t.Errorf("expected 'OK', got '%s'", body)
}
}
// A reload that can't be used has to leave the running configuration in place.
func TestFailedReloadKeepsTheRunningConfiguration(t *testing.T) {
server, err := New(Config{AddHeaders: [][]string{{"X-Test-Header", "startup"}}})
if err != nil {
t.Fatal(err)
}
if err := server.Reload(Config{ViaHeader: "nonsense"}); err == nil {
t.Fatal("expected the reload to be refused")
}
headers := server.Config().AddHeaders
if len(headers) != 1 || headers[0][1] != "startup" {
t.Errorf("expected the startup configuration to still be in force, got %v", headers)
}
}