From 0ac6f80b50db280b4d529fc85b50c99425e5724b Mon Sep 17 00:00:00 2001 From: Anton Gubarev Date: Thu, 2 Sep 2021 11:46:13 +0300 Subject: [PATCH] Fix empty body error in mirror --- pkg/server/service/loadbalancer/mirror/mirror.go | 2 +- .../service/loadbalancer/mirror/mirror_test.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/server/service/loadbalancer/mirror/mirror.go b/pkg/server/service/loadbalancer/mirror/mirror.go index 212d88731..bd7142394 100644 --- a/pkg/server/service/loadbalancer/mirror/mirror.go +++ b/pkg/server/service/loadbalancer/mirror/mirror.go @@ -204,7 +204,7 @@ func newReusableRequest(req *http.Request, maxBodySize int64) (*reusableRequest, if req == nil { return nil, nil, errors.New("nil input request") } - if req.Body == nil { + if req.Body == nil || req.ContentLength == 0 { return &reusableRequest{req: req}, nil, nil } diff --git a/pkg/server/service/loadbalancer/mirror/mirror_test.go b/pkg/server/service/loadbalancer/mirror/mirror_test.go index a23b1d9fd..eda49a960 100644 --- a/pkg/server/service/loadbalancer/mirror/mirror_test.go +++ b/pkg/server/service/loadbalancer/mirror/mirror_test.go @@ -243,9 +243,22 @@ func TestCloneRequest(t *testing.T) { req, err := http.NewRequest(http.MethodPost, "/", buf) assert.NoError(t, err) - _, expectedBytes, err := newReusableRequest(req, 20) + rr, expectedBytes, err := newReusableRequest(req, 20) assert.NoError(t, err) assert.Nil(t, expectedBytes) + assert.Len(t, rr.body, 10) + }) + + t.Run("valid GET case with maxBodySize", func(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + + req, err := http.NewRequest(http.MethodGet, "/", buf) + assert.NoError(t, err) + + rr, expectedBytes, err := newReusableRequest(req, 20) + assert.NoError(t, err) + assert.Nil(t, expectedBytes) + assert.Len(t, rr.body, 0) }) t.Run("no request given", func(t *testing.T) {