capture/main_test.go

175 lines
3.4 KiB
Go
Raw Normal View History

2018-11-11 17:54:35 +01:00
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) {
2018-11-22 22:45:20 +01:00
// given
2018-11-11 17:54:35 +01:00
tt := []TestCase{
GetRequest(),
PostRequest(),
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
service := httptest.NewServer(http.HandlerFunc(tc.service))
2018-11-22 22:52:02 +01:00
capture := httptest.NewServer(NewProxyHandler(service.URL))
2018-11-11 17:54:35 +01:00
2018-11-22 22:45:20 +01:00
// when
2018-11-11 17:54:35 +01:00
resp := tc.request(capture.URL)
2018-11-22 22:45:20 +01:00
// then
2018-11-11 17:54:35 +01:00
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 TestDumpRequest(t *testing.T) {
msg := "hello"
2018-11-22 22:45:20 +01:00
// given
2018-11-11 17:54:35 +01:00
req, err := http.NewRequest(http.MethodPost, "http://localhost:9000/", strings.NewReader(msg))
if err != nil {
t.Errorf("Could not create request: %v", err)
}
2018-11-22 22:45:20 +01:00
// when
2018-11-11 17:54:35 +01:00
body, err := dumpRequest(req)
2018-11-22 22:45:20 +01:00
// then
2018-11-11 17:54:35 +01:00
if err != nil {
t.Errorf("Dump Request error: %v", err)
}
if !strings.Contains(string(body), msg) {
t.Errorf("Dump Request is not '%s'", msg)
}
}
func TestDumpRequestGzip(t *testing.T) {
msg := "hello"
2018-11-22 22:45:20 +01:00
// given
2018-11-11 17:54:35 +01:00
req, err := http.NewRequest(http.MethodPost, "http://localhost:9000/", strings.NewReader(gzipStr(msg)))
req.Header.Set("Content-Encoding", "gzip")
if err != nil {
t.Errorf("Could not create request: %v", err)
}
2018-11-22 22:45:20 +01:00
// when
2018-11-11 17:54:35 +01:00
body, err := dumpRequest(req)
2018-11-22 22:45:20 +01:00
// then
2018-11-11 17:54:35 +01:00
if err != nil {
t.Errorf("Dump Request Gzip error: %v", err)
}
if !strings.Contains(string(body), msg) {
t.Errorf("Dump Request Gzip is not '%s'", msg)
}
}
func TestDumpResponse(t *testing.T) {
msg := "hello"
2018-11-22 22:45:20 +01:00
// given
2018-11-11 17:54:35 +01:00
res := &http.Response{Body: ioutil.NopCloser(strings.NewReader(msg))}
2018-11-22 22:45:20 +01:00
// when
2018-11-11 17:54:35 +01:00
body, err := dumpResponse(res)
2018-11-22 22:45:20 +01:00
// then
2018-11-11 17:54:35 +01:00
if err != nil {
t.Errorf("Dump Response Error: %v", err)
}
if !strings.Contains(string(body), msg) {
t.Errorf("Dump Response is not '%s'", msg)
}
}
func TestDumpResponseGzip(t *testing.T) {
msg := "hello"
2018-11-22 22:45:20 +01:00
// given
2018-11-11 17:54:35 +01:00
h := make(http.Header)
h.Set("Content-Encoding", "gzip")
res := &http.Response{Header: h, Body: ioutil.NopCloser(strings.NewReader(gzipStr(msg)))}
2018-11-22 22:45:20 +01:00
// when
2018-11-11 17:54:35 +01:00
body, err := dumpResponse(res)
2018-11-22 22:45:20 +01:00
// then
2018-11-11 17:54:35 +01:00
if err != nil {
t.Errorf("Dump Response error: %v", err)
}
if !strings.Contains(string(body), msg) {
t.Error("Not hello")
}
}
func gzipStr(str string) string {
var buff bytes.Buffer
g := gzip.NewWriter(&buff)
io.WriteString(g, str)
g.Close()
return buff.String()
}