Skip to content

Commit a637d81

Browse files
authored
Merge pull request #3 from netr/dev
Add functionality to pass postdata as io.Reader when testing requests
2 parents f5cb803 + dec0bf8 commit a637d81

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

trex/trex.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ func (tr *TestResponse) TestRequest(method, url string, postData *url.Values, he
9494
return tr
9595
}
9696

97+
// TestReaderRequest will execute a fiber.App().Test() on the given request data and return a TestResponse.
98+
func (tr *TestResponse) TestReaderRequest(method, url string, reader io.Reader, headers *http.Header) *TestResponse {
99+
req := createReaderRequest(method, tr.makeUrl(url), reader, headers)
100+
resp, _ := tr.suite.App().Test(req, 15000)
101+
102+
tr.response = resp
103+
tr.request = req
104+
return tr
105+
}
106+
97107
// BodyReader returns the response body as io.ReadCloser.
98108
//
99109
// See: https://stackoverflow.com/a/60098300 for information about this issue.
@@ -376,6 +386,24 @@ func convertUrlValuesToJson(values *url.Values) string {
376386
return jsonString
377387
}
378388

389+
// createReaderRequest helper function to create httptest requests
390+
func createReaderRequest(method, url string, reader io.Reader, headers *http.Header) *http.Request {
391+
req := httptest.NewRequest(method, url, reader)
392+
393+
if headers != nil {
394+
// add the parameter headers into the requests headers
395+
for key, value := range *headers {
396+
req.Header.Add(key, value[0])
397+
}
398+
}
399+
400+
if req.Header.Get(fiber.HeaderContentType) == "" {
401+
req.Header.Set(fiber.HeaderContentType, fiber.MIMEApplicationForm)
402+
}
403+
404+
return req
405+
}
406+
379407
// createRequest helper function to create httptest requests
380408
func createRequest(method, url string, postData *url.Values, headers *http.Header) *http.Request {
381409
var req *http.Request

0 commit comments

Comments
 (0)