diff --git a/http/request.go b/http/request.go index 6d2ec5c..e1d02e4 100644 --- a/http/request.go +++ b/http/request.go @@ -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 } diff --git a/http/request_test.go b/http/request_test.go index 84e308e..01283e5 100644 --- a/http/request_test.go +++ b/http/request_test.go @@ -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") @@ -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) {