Skip to content

Commit

Permalink
when copying http request reset body so it can be read again
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Feb 22, 2019
1 parent 437ed40 commit 1bb79ee
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ func copyHttpRequest(request *http.Request) *http.Request {
if request.Body != nil {
bodyBytes, _ := ioutil.ReadAll(request.Body)
resCopy.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
}

if request.URL != nil {
Expand Down
4 changes: 3 additions & 1 deletion apitest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apitest

import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
Expand Down Expand Up @@ -588,7 +589,7 @@ func TestApiTest_BuildQueryCollection_EmptyIfNoParams(t *testing.T) {
}

func TestApiTest_CopyHttpRequest(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req := httptest.NewRequest(http.MethodGet, "/", bytes.NewBufferString("12345"))
req.Header.Add("a", "1")
req.Header.Add("b", "2")

Expand All @@ -599,6 +600,7 @@ func TestApiTest_CopyHttpRequest(t *testing.T) {
assert.Equal(t, reqCopy.Host, req.Host)
assert.Equal(t, reqCopy.ContentLength, req.ContentLength)
assert.Equal(t, reqCopy.Header, req.Header)
assert.Equal(t, reqCopy.Body, req.Body)
}

func TestCreateHash_GroupsByEndpoint(t *testing.T) {
Expand Down

0 comments on commit 1bb79ee

Please sign in to comment.