-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
129 lines (119 loc) · 2.73 KB
/
proxy_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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
type EndServer struct {
server *httptest.Server
}
type RequestContent struct {
Path string
Method string
QueryString string
ReqBody string
}
type ResponseContent struct {
StatusCode int
Header http.Header
Body string
}
func (e *EndServer) request(c *http.Client, r *http.Request) (*http.Response, error) {
req, err := http.NewRequest(r.Method, e.server.URL+r.URL.Path, r.Body)
if err != nil {
return nil, err
}
return c.Do(req)
}
func NewEndServer(flows []FlowTest) EndServer {
mux := http.NewServeMux()
for _, flow := range flows {
flow := flow
mux.HandleFunc(flow.Request.URL.Path, func(rw http.ResponseWriter, r *http.Request) {
for k, val := range flow.Response.Header {
for _, v := range val {
rw.Header().Set(k, v)
}
}
rw.WriteHeader(flow.Response.StatusCode)
duplicateReadCloser(flow.Response.Body)
response, _ := stringify(flow.Response.Body)
fmt.Fprint(rw, response)
})
}
return EndServer{
server: httptest.NewServer(mux),
}
}
type FlowTest struct {
Flow
RespBody io.ReadCloser
}
func TestProxy(t *testing.T) {
testFlows := createFlows()
es := NewEndServer(testFlows)
p := NewGenProxy()
pserver := httptest.NewServer(p.Proxy())
url, _ := url.Parse(pserver.URL)
c := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(url),
},
}
for _, flow := range testFlows {
resp, err := es.request(&c, &flow.Request)
assert.NoError(t, err)
actual, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
expected, err := io.ReadAll(flow.RespBody)
assert.NoError(t, err)
assert.Equal(t, string(expected), string(actual))
}
capturedFlows := p.Flows()
assert.Equal(t, len(testFlows), len(capturedFlows))
}
func createFlows() []FlowTest {
original, dup := duplicateReadCloser(io.NopCloser(bytes.NewReader([]byte(`{"foo":"bar"}`))))
original2, dup2 := duplicateReadCloser(io.NopCloser(bytes.NewReader([]byte(`{"a":"b","hoge":"foo"}`))))
flows := []FlowTest{
{
Flow: Flow{
Request: http.Request{
Method: "GET",
URL: &url.URL{Path: "/"},
},
Response: http.Response{
StatusCode: 200,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: original,
},
},
RespBody: dup,
},
{
Flow: Flow{
Request: http.Request{
Method: "POST",
URL: &url.URL{Path: "/hoge"},
},
Response: http.Response{
StatusCode: 200,
Header: http.Header{
"Content-Type": []string{"application/json"},
"X-Foo": []string{"foo"},
},
Body: original2,
},
},
RespBody: dup2,
},
}
return flows
}