From cd394d0abf5066d3d6a0ebd4711fafe6dc21b41f Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sat, 16 Sep 2023 22:44:07 -0700 Subject: [PATCH 1/4] revert: pr #541 because it breaks the io.Reader bufferless processing --- middleware.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/middleware.go b/middleware.go index 4ffb9dbd..362df3b8 100644 --- a/middleware.go +++ b/middleware.go @@ -163,7 +163,9 @@ CL: func createHTTPRequest(c *Client, r *Request) (err error) { if r.bodyBuf == nil { - if c.setContentLength || r.setContentLength { + if reader, ok := r.Body.(io.Reader); ok { + r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader) + } else if c.setContentLength || r.setContentLength { r.RawRequest, err = http.NewRequest(r.Method, r.URL, http.NoBody) } else { r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil) @@ -441,9 +443,14 @@ func handleRequestBody(c *Client, r *Request) (err error) { r.bodyBuf = nil if reader, ok := r.Body.(io.Reader); ok { - r.bodyBuf = acquireBuffer() - _, err = r.bodyBuf.ReadFrom(reader) - r.Body = nil + if c.setContentLength || r.setContentLength { // keep backward compatibility + r.bodyBuf = acquireBuffer() + _, err = r.bodyBuf.ReadFrom(reader) + r.Body = nil + } else { + // Otherwise buffer less processing for `io.Reader`, sounds good. + return + } } else if b, ok := r.Body.([]byte); ok { bodyBytes = b } else if s, ok := r.Body.(string); ok { From 30fc88b12f021674e7046026c93436ba43a96a86 Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sat, 16 Sep 2023 22:54:20 -0700 Subject: [PATCH 2/4] fix: SetAllowGetMethodPayload inconsistent behaviour for #541 #618 --- client_test.go | 16 ++++++++++++++++ middleware.go | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 72dfd082..02ccdaf3 100644 --- a/client_test.go +++ b/client_test.go @@ -543,6 +543,22 @@ func TestClientAllowsGetMethodPayload(t *testing.T) { assertEqual(t, payload, resp.String()) } +func TestClientAllowsGetMethodPayloadIoReader(t *testing.T) { + ts := createGetServer(t) + defer ts.Close() + + c := dc() + c.SetAllowGetMethodPayload(true) + + payload := "test-payload" + body := bytes.NewReader([]byte(payload)) + resp, err := c.R().SetBody(body).Get(ts.URL + "/get-method-payload-test") + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + assertEqual(t, payload, resp.String()) +} + func TestClientAllowsGetMethodPayloadDisabled(t *testing.T) { ts := createGetServer(t) defer ts.Close() diff --git a/middleware.go b/middleware.go index 362df3b8..57a62703 100644 --- a/middleware.go +++ b/middleware.go @@ -163,7 +163,7 @@ CL: func createHTTPRequest(c *Client, r *Request) (err error) { if r.bodyBuf == nil { - if reader, ok := r.Body.(io.Reader); ok { + if reader, ok := r.Body.(io.Reader); ok && isPayloadSupported(r.Method, c.AllowGetMethodPayload) { r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader) } else if c.setContentLength || r.setContentLength { r.RawRequest, err = http.NewRequest(r.Method, r.URL, http.NoBody) From 5150be368d9a5947dbf15f1a8158f23f3b130532 Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sat, 16 Sep 2023 23:07:15 -0700 Subject: [PATCH 3/4] build: codecov error meesage about sha value --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 95957811..6e431e91 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: Setup Go uses: actions/setup-go@v2 From 46e25255f2bee9178df46b4555fbdf50ce086111 Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sat, 16 Sep 2023 23:29:13 -0700 Subject: [PATCH 4/4] build: update config and test --- .github/workflows/ci.yml | 4 +++- client_test.go | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e431e91..52cbefb5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,9 +32,11 @@ jobs: fetch-depth: 0 - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{ matrix.go }} + cache: true + cache-dependency-path: go.sum - name: Format run: diff -u <(echo -n) <(go fmt $(go list ./...)) diff --git a/client_test.go b/client_test.go index 02ccdaf3..966e029a 100644 --- a/client_test.go +++ b/client_test.go @@ -852,6 +852,7 @@ func TestClientOnResponseError(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { + t.Parallel() var assertErrorHook = func(r *Request, err error) { assertNotNil(t, r) v, ok := err.(*ResponseError)