-
Notifications
You must be signed in to change notification settings - Fork 16
/
main_test.go
150 lines (134 loc) · 2.99 KB
/
main_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
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
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// Test the reverse proxy handler
func TestProxyHandler(t *testing.T) {
// given
tt := []TestCase{
GetRequest(),
PostRequest(),
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
service := httptest.NewServer(http.HandlerFunc(tc.service))
capture := httptest.NewServer(NewProxyHandler(service.URL))
// when
resp := tc.request(capture.URL)
// then
tc.test(t, resp)
resp.Body.Close()
capture.Close()
service.Close()
})
}
}
type TestCase struct {
name string
request func(string) *http.Response
service func(http.ResponseWriter, *http.Request)
test func(*testing.T, *http.Response)
}
func GetRequest() TestCase {
msg := "hello"
return TestCase{
name: "GetRequest",
request: func(url string) *http.Response {
res, _ := http.Get(url)
return res
},
service: func(rw http.ResponseWriter, req *http.Request) {
fmt.Fprint(rw, string(msg))
},
test: func(t *testing.T, res *http.Response) {
body, _ := ioutil.ReadAll(res.Body)
if string(body) != msg {
t.Error("Wrong Body Response")
}
},
}
}
func PostRequest() TestCase {
msg := "hello"
return TestCase{
name: "PostRequest",
request: func(url string) *http.Response {
res, _ := http.Post(url, "text/plain", strings.NewReader(msg))
return res
},
service: func(rw http.ResponseWriter, req *http.Request) {
io.Copy(rw, req.Body)
},
test: func(t *testing.T, res *http.Response) {
body, _ := ioutil.ReadAll(res.Body)
if string(body) != msg {
t.Error("Wrong Body Response")
}
},
}
}
func TestDashboardRedirect(t *testing.T) {
// Given.
req, _ := http.NewRequest(http.MethodGet, "/something/", nil)
rec := httptest.NewRecorder()
// When.
NewDashboardHTMLHandler().ServeHTTP(rec, req)
// Then.
if rec.Code != http.StatusTemporaryRedirect {
t.Errorf("Wrong response code: got %d, want %d", rec.Code, http.StatusTemporaryRedirect)
}
if loc := rec.Header().Get("Location"); loc != "/" {
t.Errorf("Wrong redirect path: got '%s', want '/'", loc)
}
}
func Example_dump() {
c := &Capture{
Req: Req{
Proto: "HTTP/1.1",
Url: "http://localhost/hello",
Path: "/hello",
Method: "GET",
Header: map[string][]string{"Content-Encoding": {"none"}},
Body: []byte(`hello`),
},
Res: Res{
Proto: "HTTP/1.1",
Header: map[string][]string{"Content-Encoding": {"gzip"}},
Body: gzipStr("gziped hello"),
Status: "200 OK",
},
}
got := dump(c)
fmt.Println(got.Request)
fmt.Println(got.Response)
fmt.Println(got.Curl)
// Output:
// GET /hello HTTP/1.1
//
// Content-Encoding: none
//
// hello
// HTTP/1.1 200 OK
//
// Content-Encoding: gzip
//
// gziped hello
// curl -X GET http://localhost/hello \
// -H 'Content-Encoding: none' \
// -d 'hello'
}
func gzipStr(str string) []byte {
var buff bytes.Buffer
g := gzip.NewWriter(&buff)
io.WriteString(g, str)
g.Close()
return buff.Bytes()
}