forked from Hellspam/goharproxy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhar_test.go
More file actions
134 lines (114 loc) · 3.24 KB
/
har_test.go
File metadata and controls
134 lines (114 loc) · 3.24 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
package goharproxy
import (
"testing"
"net/http"
"bytes"
"net/url"
"reflect"
"strconv"
"strings"
)
func TestParseHttpGETRequest (t *testing.T) {
req, _ := http.NewRequest("GET", "http://google.com", nil)
if req == nil {
t.Errorf("Failure creating request")
}
expectedReq := HarRequest{
Method : "GET",
Url : "http://google.com",
BodySize : 0,
}
if harReq := parseRequest(req); reflect.DeepEqual(expectedReq, harReq) {
t.Errorf("Expected:\n %v \n\n Actual:\n %v \n\n", expectedReq, harReq)
}
}
func TestParseIpString (t *testing.T) {
req, _ := http.NewRequest("GET", "http://google.com", nil)
if req == nil {
t.Errorf("Failure creating request")
}
expectedReq := HarRequest{
Method : "GET",
Url : "http://google.com",
BodySize : 0,
}
if harReq := parseRequest(req); reflect.DeepEqual(expectedReq, harReq) {
t.Errorf("Expected:\n %v \n\n Actual:\n %v \n\n", expectedReq, harReq)
}
}
func TestParseHttpIpGETRequest (t *testing.T) {
req, _ := http.NewRequest("GET", "http://google.com", nil)
if req == nil {
t.Errorf("Failure creating request")
}
expectedReq := HarRequest{
Method : "GET",
Url : "http://google.com",
BodySize : 0,
}
if harReq := parseRequest(req); reflect.DeepEqual(expectedReq, harReq) {
t.Errorf("Expected:\n %v \n\n Actual:\n %v \n\n", expectedReq, harReq)
}
}
func TestParseHttpPOSTRequest (t *testing.T) {
req, expectedReq := getTestSendRequest("POST", t)
captureContent = true
if harReq := parseRequest(req); reflect.DeepEqual(expectedReq, harReq) {
t.Errorf("Expected:\n %v \n\n Actual:\n %v \n\n", expectedReq, harReq)
}
}
func TestParseHttpPUTRequest (t *testing.T) {
req, expectedReq := getTestSendRequest("PUT", t)
captureContent = true
if harReq := parseRequest(req); reflect.DeepEqual(expectedReq, harReq) {
t.Errorf("Expected:\n %v \n\n Actual:\n %v \n\n", expectedReq, harReq)
}
}
func TestParsePostDataRaw(t *testing.T) {
testString := "BLA"
req, err := http.NewRequest("POST", "http://google.com", strings.NewReader(testString))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "Raw")
contentLength := strconv.Itoa(len(testString))
req.Header.Add("Content-Length", contentLength)
postData := parsePostData(req)
if postData.Text != testString {
t.Fatal("Did not get expected text")
}
}
func getTestSendRequest(method string, t *testing.T) (*http.Request, *HarRequest) {
data := url.Values{}
data.Set("name", "foo")
data.Add("surname", "bar")
req, err := http.NewRequest(method, "http://google.com", bytes.NewBufferString(data.Encode()))
if err != nil {
t.Fatal(err)
}
contentType := "application/x-www-form-urlencoded"
req.Header.Add("Content-Type", contentType)
contentLength := strconv.Itoa(len(data.Encode()))
req.Header.Add("Content-Length", contentLength)
index := 0
params := make([]HarPostDataParam, len(data))
for k, v := range data {
param := HarPostDataParam {
Name : k,
Value : strings.Join(v, ","),
}
params[index] = param
index++
}
harPostData := HarPostData {
Params : params,
MimeType : contentType,
}
expectedReq := HarRequest{
Method : method,
Url : "http://google.com",
BodySize : (int64)(len(data.Encode())),
PostData : &harPostData,
}
return req, &expectedReq
}