Skip to content

Commit

Permalink
Merge pull request #2 from gojek/NOJIRA_bug_clone_request
Browse files Browse the repository at this point in the history
[BUGFIX]: fix nil pointer bug in request.Clone()
  • Loading branch information
romanwozniak authored Oct 8, 2020
2 parents 101a992 + c7395fd commit 4f0f828
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
16 changes: 7 additions & 9 deletions http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ func NewHTTPRequest(req *http.Request) (*Request, error) {

// Copy creates a deep copy of this request
func (r *Request) Clone() (fiber.Request, error) {
body, err := r.GetBody()
if err != nil {
return nil, err
}
bodyReader := bytes.NewReader(r.Payload())

proxyRequest, err := http.NewRequest(r.Method, r.URL.String(), body)
proxyRequest, err := http.NewRequest(r.Method, r.URL.String(), bodyReader)
if err != nil {
return nil, err
}

for key, values := range r.Header() {
for i := range values {
proxyRequest.Header.Add(key, values[i])
}
proxyRequest.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bodyReader), nil
}

proxyRequest.Header = r.Header()

return &Request{CachedPayload: r.CachedPayload, Request: proxyRequest}, nil
}

Expand Down
43 changes: 28 additions & 15 deletions http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"testing"
"time"

"github.com/stretchr/testify/mock"

"github.com/stretchr/testify/require"
"github.com/gojek/fiber"
fiberHTTP "github.com/gojek/fiber/http"
"github.com/gojek/fiber/internal/testutils"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

var requestPayload, _ = testutils.ReadFile("../internal/testdata/request_payload.json")
Expand Down Expand Up @@ -100,25 +100,38 @@ func TestRequest_Clone(t *testing.T) {
"http://localhost:9999/api/mock",
strings.NewReader("*** request payload ***"),
))
req.Request.Header = http.Header{
"fiber-key": []string{
fmt.Sprintf("test-%d", time.Now().Unix()),
},
}

req.Request.Header.Add("fiber-key", fmt.Sprintf("test-%d", time.Now().Unix()))

cloned, err := req.Clone()
require.NoError(t, err)
compareRequests := func(t *testing.T, original *fiberHTTP.Request, clone fiber.Request) {
clonedReq, ok := clone.(*fiberHTTP.Request)
require.True(t, ok, "clone should have the same type as the original")

copyReq, ok := cloned.(*fiberHTTP.Request)
require.NotEqual(t, original, clonedReq)
require.Equal(t, original.Header(), clonedReq.Header())

require.True(t, ok, "clone should have the same type as the original")
expectedBody, err := original.GetBody()
require.NoError(t, err)
require.Equal(t, readBytes(expectedBody), readBytes(clonedReq.Body))
require.Equal(t, req.URL, clonedReq.URL)
}

require.NotEqual(t, req, copyReq)
require.Equal(t, req.Header(), copyReq.Header())
clone, err := req.Clone()
require.NoError(t, err)

expectedBody, err1 := req.GetBody()
t.Run("success", func(t *testing.T) {
compareRequests(t, req, clone)
})

require.NoError(t, err1)
require.Equal(t, readBytes(expectedBody), readBytes(copyReq.Body))
cloneOfClone, err := clone.Clone()
require.NoError(t, err)

require.Equal(t, req.URL, copyReq.URL)
t.Run("success | clone of cloned", func(t *testing.T) {
compareRequests(t, req, cloneOfClone)
})
}

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

0 comments on commit 4f0f828

Please sign in to comment.