Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request/response logging for SetDoNotParseResponse(true) #836

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,8 @@
//
// Note: Response middlewares are not applicable, if you use this option. Basically you have
// taken over the control of response parsing from `Resty`.
func (c *Client) SetDoNotParseResponse(parse bool) *Client {
c.notParseResponse = parse
func (c *Client) SetDoNotParseResponse(notParse bool) *Client {
c.notParseResponse = notParse
return c
}

Expand Down Expand Up @@ -1234,8 +1234,12 @@
}

if err != nil || req.notParseResponse || c.notParseResponse {
logErr := responseLogger(c, response)
response.setReceivedAt()
return response, err
if err != nil {
return response, errors.Join(err, logErr)
}
return response, wrapNoRetryErr(logErr)
}

if !req.isSaveResponse {
Expand All @@ -1247,6 +1251,7 @@
if _, ok := body.(*gzip.Reader); !ok {
body, err = gzip.NewReader(body)
if err != nil {
err = errors.Join(err, responseLogger(c, response))

Check warning on line 1254 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L1254

Added line #L1254 was not covered by tests
response.setReceivedAt()
return response, err
}
Expand All @@ -1255,6 +1260,7 @@
}

if response.body, err = readAllWithLimit(body, req.responseBodyLimit); err != nil {
err = errors.Join(err, responseLogger(c, response))
response.setReceivedAt()
return response, err
}
Expand All @@ -1265,6 +1271,11 @@
response.setReceivedAt() // after we read the body

// Apply Response middleware
err = responseLogger(c, response)
if err != nil {
return response, wrapNoRetryErr(err)
}

for _, f := range c.afterResponse {
if err = f(c, response); err != nil {
break
Expand Down Expand Up @@ -1460,7 +1471,6 @@

// default after response middlewares
c.afterResponse = []ResponseMiddleware{
responseLogger,
parseResponseBody,
saveResponseIntoFile,
}
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,15 @@ func TestResponseBodyLimit(t *testing.T) {

_, err := c.R().Get(ts.URL + "/")
assertNotNil(t, err)
assertEqual(t, err, ErrResponseBodyTooLarge)
assertErrorIs(t, ErrResponseBodyTooLarge, err)
})

t.Run("request body limit", func(t *testing.T) {
c := dc()

_, err := c.R().SetResponseBodyLimit(1024).Get(ts.URL + "/")
assertNotNil(t, err)
assertEqual(t, err, ErrResponseBodyTooLarge)
assertErrorIs(t, ErrResponseBodyTooLarge, err)
})

t.Run("body less than limit", func(t *testing.T) {
Expand Down Expand Up @@ -1149,6 +1149,6 @@ func TestResponseBodyLimit(t *testing.T) {
c := dc()

_, err := c.R().SetResponseBodyLimit(10240).Get(tse.URL + "/")
assertErrorIs(t, err, gzip.ErrHeader)
assertErrorIs(t, gzip.ErrHeader, err)
})
}
8 changes: 2 additions & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package resty

import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -208,9 +208,5 @@ func TestRequestContext(t *testing.T) {
}

func errIsContextCanceled(err error) bool {
ue, ok := err.(*url.Error)
if !ok {
return false
}
return ue.Err == context.Canceled
return errors.Is(err, context.Canceled)
}
Loading