-
Notifications
You must be signed in to change notification settings - Fork 28
/
headers_test.go
108 lines (85 loc) · 2.19 KB
/
headers_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
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type myHandler struct {
headers map[string]string
}
func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
found := false
for h, v := range h.headers {
found = (r.Header.Get(h) == v)
if !found {
break
}
}
if found {
fmt.Fprint(w, "OK")
} else {
fmt.Fprintf(w, "FAIL. Headers: %v", r.Header)
}
}
func TestCustomHeaders(t *testing.T) {
expectedResponse := "OK"
expectedHeaders := map[string]string{
"X-Custom-Header-1": "Value-1",
"X-Custom-Header-2": "Value-2",
}
handler := &myHandler{headers: expectedHeaders}
background := httptest.NewServer(handler)
defer background.Close()
client, proxy, proxyserver := oneShotProxy()
defer proxyserver.Close()
s := `add_headers=[["X-Custom-Header-1", "Value-1"], ["X-Custom-Header-2", "Value-2"]]`
conf := newConfiguration(bytes.NewBuffer([]byte(s)))
setAddCustomHeadersHandler(conf, proxy)
resp, err := client.Get(background.URL)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Error("Expected 200 status code, got", resp.Status)
}
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
actualResponse := string(msg)
if actualResponse != expectedResponse {
t.Errorf("Expected '%s', actual '%s'", expectedResponse, actualResponse)
}
}
func TestViaHeaders(t *testing.T) {
expectedResponse := "OK"
expectedHeaders := map[string]string{
"Via": "1.1 octopus",
}
handler := &myHandler{headers: expectedHeaders}
background := httptest.NewServer(handler)
defer background.Close()
client, proxy, proxyserver := oneShotProxy()
defer proxyserver.Close()
s := "via_header=\"on\"\nvia_proxy_name=\"octopus\"\n"
conf := newConfiguration(bytes.NewBuffer([]byte(s)))
setViaHeaderHandler(conf, proxy)
resp, err := client.Get(background.URL)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Error("Expected 200 status code, got", resp.Status)
}
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
actualResponse := string(msg)
if actualResponse != expectedResponse {
t.Errorf("Expected '%s', actual '%s'", expectedResponse, actualResponse)
}
}