forked from fairfaxmedia/flywheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
91 lines (79 loc) · 1.79 KB
/
http_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
package flywheel
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// MockedHandler to verify if non 200 http codes return unmodified values
func MockedHandler(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/302") {
w.Header().Add("Location", "http://dev.zero/fakeRedirectHandler")
w.WriteHeader(http.StatusFound)
} else if strings.HasPrefix(r.URL.Path, "/404") {
w.WriteHeader(http.StatusNotFound)
} else if strings.HasPrefix(r.URL.Path, "/500") {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, r.Body)
}
func TestProxyFunction(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(MockedHandler))
defer server.Close()
// Reroute all traffic to the test server
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
fw := Flywheel{
config: &Config{
Vhosts: map[string]string{"www.example.org": "www.backend.example.org"},
},
}
handler := NewHandler(&fw)
handler.HTTPClient.Transport = transport
testTable := []struct {
url, host, method string
code int
}{
{
"/302something?flywheel=start",
"www.example.org",
"GET",
302,
},
{
"/404not_found?flywheel=start",
"www.example.org",
"GET",
404,
},
{
"/500error_buddy",
"www.example.org",
"GET",
500,
},
{
"/all_good_mate",
"www.example.org",
"GET",
200,
},
}
for _, tt := range testTable {
w := httptest.NewRecorder()
req, _ := http.NewRequest(tt.method, tt.url, nil)
req.Host = tt.host
handler.proxy(w, req)
if w.Code != tt.code {
t.Errorf("Expexted code %d, but got %d", tt.code, w.Code)
}
}
}