-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttp_test.go
348 lines (292 loc) · 9.77 KB
/
http_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
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
341
342
343
344
345
346
347
348
package httpfuzz
import (
"bytes"
"context"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httputil"
"strings"
"testing"
)
func TestRequestClonePreservesOriginalBody(t *testing.T) {
req, _ := http.NewRequest("POST", "", strings.NewReader("body"))
request := &Request{req}
clonedRequest, err := request.CloneBody(context.Background())
if err != nil {
t.Fatal(err)
}
// Read the cloned body first to make sure the original body doesn't get consumed.
clonedBody, _ := ioutil.ReadAll(clonedRequest.Body)
body, _ := ioutil.ReadAll(request.Body)
if len(body) == 0 {
t.Fatalf("Original body was drained with the clone")
}
if !bytes.Equal(body, clonedBody) {
t.Fatalf("Cloned body does not match original, expected %s, got %s", string(body), string(clonedBody))
}
if req.RequestURI != "" {
t.Fatalf("RequestURI was not removed before clone")
}
if request.Host != clonedRequest.URL.Host {
t.Fatalf("Host was not copied into URL")
}
}
func TestResponseClonePreservesOriginalBody(t *testing.T) {
body := "body"
resp := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
Body: ioutil.NopCloser(strings.NewReader(body)),
ContentLength: int64(len(body)),
}
response := &Response{resp}
clonedResponse, err := response.CloneBody()
if err != nil {
t.Fatal(err)
}
// Read the cloned body first to make sure the original body doesn't get consumed.
clonedBody, _ := ioutil.ReadAll(clonedResponse.Body)
originalBody, _ := ioutil.ReadAll(response.Body)
if len(originalBody) == 0 {
t.Fatal("Original body was consumed when clone body was read")
}
if !bytes.Equal(clonedBody, originalBody) {
t.Fatalf("Cloned body does not match original, expected %s, got %s", string(originalBody), string(clonedBody))
}
if response.Status != clonedResponse.Status {
t.Fatalf("Cloned response status does not match original response status")
}
}
func TestHasPathArgument(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path", strings.NewReader("body"))
request := &Request{req}
if !request.HasPathArgument("path") {
t.Fatal("Expected HasPathArgument to be true")
}
if request.HasPathArgument("notfound") {
t.Fatalf("Expected HasPathArgument to be false")
}
}
func TestSetQueryParam(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path", strings.NewReader("body"))
request := &Request{req}
request.SetQueryParam("param", "test")
expectedURL := "/test/path?param=test"
actualURL := request.URL.String()
if actualURL != expectedURL {
t.Fatalf("Expected %s, got %s", expectedURL, actualURL)
}
}
func TestSetURLPathArgument(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("body"))
request := &Request{req}
request.SetURLPathArgument("path", "test")
expectedURL := "/test/test?param=test"
actualURL := request.URL.String()
if actualURL != expectedURL {
t.Fatalf("Expected %s, got %s", expectedURL, actualURL)
}
}
func TestSetDirectoryRoot(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("body"))
request := &Request{req}
request.SetDirectoryRoot("added")
expectedURL := "/test/path/added?param=test"
actualURL := request.URL.String()
if actualURL != expectedURL {
t.Fatalf("Expected %s, got %s", expectedURL, actualURL)
}
}
func TestBodyTargetCount(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("`body``second`"))
request := &Request{req}
count, err := request.BodyTargetCount('`')
if err != nil {
t.Fatal(err)
}
if count != 2 {
t.Fatalf("Expected 2, got %d", count)
}
}
func TestBodyTargetCountUnbalancedDelimiters(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("`body"))
request := &Request{req}
count, err := request.BodyTargetCount('`')
if err == nil {
t.Fatalf("Expected error, got %d", count)
}
if count != 0 {
t.Fatalf("Expected 0 count, got %d", count)
}
}
func TestRemoveDelimiters(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("{\"type\": \"`body`\", \"second\": \"`value`\"}"))
request := &Request{req}
previousContentLength := request.ContentLength
targetCount, _ := request.BodyTargetCount('`')
err := request.RemoveDelimiters('`')
if err != nil {
t.Fatal(err)
}
expectedContentLength := previousContentLength - int64(targetCount*2)
if request.ContentLength != expectedContentLength {
t.Fatalf("Content length does not match, expected %d, got %d", expectedContentLength, request.ContentLength)
}
expectedBody := []byte("{\"type\": \"body\", \"second\": \"value\"}")
actualBody, _ := ioutil.ReadAll(request.Body)
// Ensure request body is not consumed when replacing delimiters.
if len(actualBody) == 0 {
t.Fatal("Request body was consumed")
}
if !bytes.Equal(actualBody, expectedBody) {
t.Fatalf("bodies do not match. expected %s, got %s", string(expectedBody), string(actualBody))
}
}
func TestRemoveDelimitersEmptyRequestBody(t *testing.T) {
req, _ := http.NewRequest("GET", "/test/path?param=test", nil)
request := &Request{req}
err := request.RemoveDelimiters('`')
if err != nil {
t.Fatal(err)
}
// Ensure request body remains empty.
if request.Body != nil {
t.Fatal("Request body not expected")
}
}
func TestInjectPayload(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("{\"type\": \"`body`\", \"second\": \"`value`\"}"))
request := &Request{req}
err := request.SetBodyPayloadAt(0, '`', "test")
if err != nil {
t.Fatal(err)
}
expectedBody := []byte("{\"type\": \"test\", \"second\": \"`value`\"}")
actualBody, _ := ioutil.ReadAll(request.Body)
expectedContentLength := int64(len(expectedBody))
if request.ContentLength != expectedContentLength {
t.Fatalf("Content length does not match, expected %d, got %d", expectedContentLength, request.ContentLength)
}
// Ensure request body is not consumed when injecting payload.
if len(actualBody) == 0 {
t.Fatal("Request body was consumed")
}
if !bytes.Equal(actualBody, expectedBody) {
t.Fatalf("bodies do not match. expected %s, got %s", string(expectedBody), string(actualBody))
}
}
func TestInjectPayloadUnbalancedDelimiters(t *testing.T) {
req, _ := http.NewRequest("POST", "/test/path?param=test", strings.NewReader("{\"type\": \"`body\", \"second\": \"`value`\"}"))
request := &Request{req}
err := request.SetBodyPayloadAt(0, '`', "test")
if err == nil {
t.Fatal("Expected error with imbalanced delimiters.")
}
}
func TestReplaceMultipartFileData(t *testing.T) {
const fileContents = "test file data"
const fileKey = "file"
body := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(body)
part, err := multipartWriter.CreateFormFile(fileKey, "filename.txt")
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(part, strings.NewReader(fileContents))
if err != nil {
t.Fatal(err)
}
multipartWriter.Close()
req, _ := http.NewRequest("POST", "/test/path?param=test", body)
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
request := &Request{req}
expectedPayload := []byte("expected payload")
file := &File{
Name: "newfile.txt",
Payload: expectedPayload,
Size: int64(len(expectedPayload)),
FileType: "txt",
}
err = request.ReplaceMultipartFileData(fileKey, file)
if err != nil {
t.Fatal(err)
}
expectedContentLength := int64(321)
if request.ContentLength != expectedContentLength {
debug, _ := httputil.DumpRequest(request.Request, true)
t.Errorf("Expected %d, got %d", expectedContentLength, request.ContentLength)
t.Fatalf(string(debug))
}
updatedFile, _, err := request.FormFile(fileKey)
if err != nil {
t.Fatal(err)
}
defer updatedFile.Close()
actualPayload, _ := ioutil.ReadAll(updatedFile)
if !bytes.Equal(expectedPayload, actualPayload) {
t.Fatalf("unexpected file, expected %s, got %s", string(expectedPayload), string(actualPayload))
}
}
func TestReplaceMultipartField(t *testing.T) {
const fileContents = "test file data"
const fileKey = "file"
body := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(body)
part, err := multipartWriter.CreateFormFile(fileKey, "filename.txt")
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(part, strings.NewReader(fileContents))
if err != nil {
t.Fatal(err)
}
multipartWriter.Close()
req, _ := http.NewRequest("POST", "/test/path?param=test", body)
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
request := &Request{req}
expectedValue := "data"
err = request.ReplaceMultipartField("fieldName", expectedValue)
if err != nil {
t.Fatal(err)
}
expectedContentLength := int64(442)
if request.ContentLength != expectedContentLength {
t.Fatalf("Expected %d, got %d", expectedContentLength, request.ContentLength)
}
actualValue := request.FormValue("fieldName")
if expectedValue != actualValue {
t.Fatalf("Expected %s, got %s", expectedValue, actualValue)
}
}
func TestRemoveDelimitersDoesNotOperateOnMultipartRequests(t *testing.T) {
const fileContents = "``````````````````a"
const fileKey = "file"
body := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(body)
part, err := multipartWriter.CreateFormFile(fileKey, "filename.txt")
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(part, strings.NewReader(fileContents))
if err != nil {
t.Fatal(err)
}
multipartWriter.Close()
req, _ := http.NewRequest("POST", "/test/path?param=test", body)
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
request := &Request{req}
err = request.RemoveDelimiters(byte('`'))
if err != nil {
t.Fatal(err)
}
actualPayload, _ := ioutil.ReadAll(request.Body)
if !bytes.Contains(actualPayload, []byte{byte('`')}) {
t.Fatalf("unexpected file, expected %s, got %s", fileContents, string(actualPayload))
}
}