forked from Hellspam/goharproxy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathharproxy_test.go
More file actions
340 lines (285 loc) · 9.59 KB
/
harproxy_test.go
File metadata and controls
340 lines (285 loc) · 9.59 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package goharproxy
import (
"testing"
"net/http"
"net/http/httptest"
"net/url"
"crypto/tls"
"io"
"log"
"encoding/json"
"fmt"
"net"
"strconv"
"bytes"
"io/ioutil"
"strings"
)
var acceptAllCerts = &tls.Config{InsecureSkipVerify: true}
var srv = httptest.NewServer(nil)
func init() {
http.DefaultServeMux.Handle("/bobo", ConstantHanlder("bobo"))
http.DefaultServeMux.Handle("/query", QueryHandler{})
http.DefaultServeMux.Handle("/", ConstantHanlder("google"))
}
type QueryHandler struct{}
func (QueryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err != nil {
panic(err)
}
io.WriteString(w, req.Form.Get("result"))
}
type ConstantHanlder string
func (h ConstantHanlder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, string(h))
}
// HarProxy Tests
func TestHttpHarProxyGetIpEntries(t *testing.T) {
client, harProxy, s := oneShotProxy()
defer s.Close()
testUrl , _ := url.Parse(srv.URL + "/bobo")
_, err := client.Get(testUrl.String())
if err != nil {
t.Fatal(err)
}
harLog := testLog(t, harProxy.NewHarReader())
host, _, _ := net.SplitHostPort(testUrl.Host)
if harLog.Entries[0].ServerIpAddress != host {
t.Fatal("Expected to get host: ", host, " but got: ", harLog.Entries[0].ServerIpAddress)
}
}
func TestHttpHarProxyGetHostEntries(t *testing.T) {
client, harProxy, s := oneShotProxy()
defer s.Close()
_, err := client.Get("http://www.google.com")
if err != nil {
t.Fatal(err)
}
ipaddr, ipErr := net.LookupIP("www.google.com")
if ipErr != nil {
t.Fatal(ipErr)
}
if len(ipaddr) == 0 {
t.Fatal("Didn't get ip for www.google.com")
}
harLog := testLog(t, harProxy.NewHarReader())
if ip := net.ParseIP(harLog.Entries[0].ServerIpAddress); ip == nil {
t.Fatal("Expected to get valid ip address in har ServerIpAddress")
}
}
// HarProxyServer tests
func TestHarProxyServerGetProxyAndDelete(t *testing.T) {
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerPort, _ := getProxiedClient(t, harProxyServer, testClient)
proxyServerDeleteUrl := fmt.Sprintf("%v/proxy/%v", harProxyServer.URL, proxyServerPort.Port)
req , reqErr := http.NewRequest("DELETE", proxyServerDeleteUrl, nil)
if reqErr != nil {
t.Fatal(reqErr)
}
resp, respErr := testClient.Do(req)
testResp(t, resp, respErr)
var proxyServerMessage *ProxyServerMessage = new(ProxyServerMessage)
json.NewDecoder(resp.Body).Decode(proxyServerMessage)
if proxyServerMessage.Message != fmt.Sprintf("Deleted proxy for port [%v] succesfully", proxyServerPort.Port) {
t.Fatal("Did not get delete message")
}
}
func TestHarProxyServerSendInvalidMessage(t *testing.T) {
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerUrl := fmt.Sprintf("%v/bla", harProxyServer.URL)
resp , err := testClient.Get(proxyServerUrl)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatal("Did not get 404 status code")
}
var proxyErrorMessage *ProxyServerErr = new(ProxyServerErr)
json.NewDecoder(resp.Body).Decode(proxyErrorMessage)
if proxyErrorMessage.Error != fmt.Sprintf("No such path: [%v]", "/bla") {
t.Fatal("Did not get expected error message")
}
}
func TestHarProxyServerGetInvalidProxy(t *testing.T) {
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerHarUrl := fmt.Sprintf("%v/proxy/%v/har", harProxyServer.URL, 9999)
req , err := http.NewRequest("PUT", proxyServerHarUrl, nil)
if err != nil {
t.Fatal(err)
}
resp, respErr := testClient.Do(req)
if respErr != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatal("Did not get 404 status code")
}
var proxyErrorMessage *ProxyServerErr = new(ProxyServerErr)
json.NewDecoder(resp.Body).Decode(proxyErrorMessage)
if proxyErrorMessage.Error != fmt.Sprintf("No proxy for port [%v]", 9999) {
t.Fatal("Did not get expected error message")
}
}
func TestHarProxyServerSendInvalidProxyMessage(t *testing.T) {
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerPort, _ := getProxiedClient(t, harProxyServer, testClient)
proxyServerHarUrl := fmt.Sprintf("%v/proxy/%v/bla", harProxyServer.URL, proxyServerPort.Port)
req , reqErr := http.NewRequest("PUT", proxyServerHarUrl, nil)
if reqErr != nil {
t.Fatal(reqErr)
}
resp, respErr := testClient.Do(req)
if respErr != nil {
t.Fatal(respErr)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatal("Did not get 404 status code")
}
var proxyErrorMessage *ProxyServerErr = new(ProxyServerErr)
json.NewDecoder(resp.Body).Decode(proxyErrorMessage)
if proxyErrorMessage.Error != "No such path [/bla] with method PUT" {
t.Fatal("Did not get expected error message")
}
}
func TestHarProxyServerGetProxyAndEntries(t *testing.T) {
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerPort, proxiedClient := getProxiedClient(t, harProxyServer, testClient)
_, err := proxiedClient.Get(srv.URL + "/bobo")
if err != nil {
t.Fatal(err)
}
proxyServerHarUrl := fmt.Sprintf("%v/proxy/%v/har", harProxyServer.URL, proxyServerPort.Port)
req , reqErr := http.NewRequest("PUT", proxyServerHarUrl, nil)
if reqErr != nil {
t.Fatal(reqErr)
}
resp, respErr := testClient.Do(req)
testResp(t, resp, respErr)
testLog(t, resp.Body)
}
func TestHarProxyServerGetProxyAndEntriesWithResponseContent(t *testing.T) {
captureContent = true
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerPort, proxiedClient := getProxiedClient(t, harProxyServer, testClient)
resp, err := proxiedClient.Get(srv.URL + "/query?result=bla")
if err != nil {
t.Fatal(err)
}
txt, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal("Error reading response body")
}
if string(txt) != "bla" {
t.Fatal("Expect to get bla in response body")
}
proxyServerHarUrl := fmt.Sprintf("%v/proxy/%v/har", harProxyServer.URL, proxyServerPort.Port)
req , reqErr := http.NewRequest("PUT", proxyServerHarUrl, nil)
if reqErr != nil {
t.Fatal(reqErr)
}
resp, err = testClient.Do(req)
testResp(t, resp, err)
harLog := testLog(t, resp.Body)
if harLog.Entries[0].Response.Content.Text != "bla" {
t.Fatal("Expect to get bla in har response")
}
}
func TestHarProxyServerGetProxyAndEntriesWithRequestPostData(t *testing.T) {
captureContent = true
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerPort, proxiedClient := getProxiedClient(t, harProxyServer, testClient)
resp, err := proxiedClient.Post(srv.URL + "/bobo", "form-data", strings.NewReader("bla"))
if err != nil {
t.Fatal(err)
}
proxyServerHarUrl := fmt.Sprintf("%v/proxy/%v/har", harProxyServer.URL, proxyServerPort.Port)
req , reqErr := http.NewRequest("PUT", proxyServerHarUrl, nil)
if reqErr != nil {
t.Fatal(reqErr)
}
resp, err = testClient.Do(req)
testResp(t, resp, err)
harLog := testLog(t, resp.Body)
if harLog.Entries[0].Request.PostData.Text!= "bla" {
t.Fatal("Expect to get bla in har request")
}
}
func TestHarProxyServerGetProxyChangeHost(t *testing.T) {
testClient, harProxyServer := newProxyTestServer()
defer harProxyServer.Close()
proxyServerPort, proxiedClient := getProxiedClient(t, harProxyServer, testClient)
proxyServerHostUrl := fmt.Sprintf("%v/proxy/%v/hosts", harProxyServer.URL, proxyServerPort.Port)
srvUrl , _ := url.Parse(srv.URL)
proxyHosts := []ProxyHosts{{Host : "www.google.com", NewHost : srvUrl.Host}}
proxyHostsJson, _ := json.Marshal(&proxyHosts)
buffer := bytes.NewBuffer(proxyHostsJson)
_, err := testClient.Post(proxyServerHostUrl, "application/json", buffer)
if err != nil {
t.Fatal(err)
}
resp, err := proxiedClient.Get("http://www.google.com")
testResp(t, resp, err)
str, _ := ioutil.ReadAll(resp.Body)
if string(str) != "google" {
t.Fatal("Failed redirecting request")
}
}
func getProxiedClient(t *testing.T, harProxyServer *httptest.Server, testClient *http.Client) (proxyServerPort *ProxyServerPort, client *http.Client) {
resp, err := testClient.Post(harProxyServer.URL + "/proxy", "", nil)
testResp(t, resp, err)
proxyServerPort = new(ProxyServerPort)
if e := json.NewDecoder(resp.Body).Decode(proxyServerPort); e != nil {
log.Fatal(e)
}
host, _, _ := net.SplitHostPort(harProxyServer.URL)
proxyUrl, _ := url.Parse(host + ":" + strconv.Itoa(proxyServerPort.Port))
client = newProxyHttpTestClient(proxyUrl)
return
}
func testLog(t *testing.T, r io.Reader) *HarLog{
var harLog *HarLog = new(HarLog)
json.NewDecoder(r).Decode(harLog)
log.Printf("Har entries len: %v", len(harLog.Entries))
if len(harLog.Entries) == 0 {
t.Fatal("Didn't get valid har entries")
}
return harLog
}
func testResp(t *testing.T, resp *http.Response, err error) {
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.Status)
}
}
func oneShotProxy() (client *http.Client, harProxy *HarProxy, s *httptest.Server) {
harProxy = NewHarProxy()
client, s = newProxyHttpTestServer(harProxy)
return
}
func newProxyTestServer() (client *http.Client, s *httptest.Server) {
s = httptest.NewServer(http.HandlerFunc(proxyHandler))
tr := &http.Transport{TLSClientConfig: acceptAllCerts}
client = &http.Client{Transport: tr}
return
}
func newProxyHttpTestServer(harProxy *HarProxy) (client *http.Client, s *httptest.Server) {
s = httptest.NewServer(harProxy.Proxy)
proxyUrl, _ := url.Parse(s.URL)
client = newProxyHttpTestClient(proxyUrl)
return
}
func newProxyHttpTestClient(proxyUrl *url.URL) (client *http.Client) {
log.Println(proxyUrl)
tr := &http.Transport{TLSClientConfig: acceptAllCerts, Proxy: http.ProxyURL(proxyUrl)}
client = &http.Client{Transport: tr}
return
}