Skip to content

Commit

Permalink
#164 Addressing gzip error on Content-Length zero (#165)
Browse files Browse the repository at this point in the history
* don't use gzipped reader when response content-length is zero
* add test case for auto gzip response
         1. gzipped empty response, content-length will not be empty, because of gzip header
         2. no gzipped body and content-length is zero
  • Loading branch information
Jack47 authored and jeevatkm committed Jun 26, 2018
1 parent d6e6c4b commit 59cee09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ func (c *Client) execute(req *Request) (*Response, error) {
body := resp.Body

// GitHub #142
if strings.EqualFold(resp.Header.Get(hdrContentEncodingKey), "gzip") {
if strings.EqualFold(resp.Header.Get(hdrContentEncodingKey), "gzip") && resp.ContentLength > 0 {
if _, ok := body.(*gzip.Reader); !ok {
body, err = gzip.NewReader(body)
if err != nil {
Expand Down
26 changes: 16 additions & 10 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,22 @@ func TestAutoGzip(t *testing.T) {
defer ts.Close()

c := New()
testcases := []struct{ url, want string }{
{ts.URL + "/gzip-test", "This is Gzip response testing"},
{ts.URL + "/gzip-test-gziped-empty-body", ""},
{ts.URL + "/gzip-test-no-gziped-body", ""},
}
for _, tc := range testcases {
resp, err := c.R().
SetHeader("Accept-Encoding", "gzip").
Get(tc.url)

resp, err := c.R().
SetHeader("Accept-Encoding", "gzip").
Get(ts.URL + "/gzip-test")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "200 OK", resp.Status())
assertNotNil(t, resp.Body())
assertEqual(t, "This is Gzip response testing", resp.String())
assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "200 OK", resp.Status())
assertNotNil(t, resp.Body())
assertEqual(t, tc.want, resp.String())

logResponse(t, resp)
logResponse(t, resp)
}
}
12 changes: 12 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,19 @@ func createGenServer(t *testing.T) *httptest.Server {
zw := gzip.NewWriter(w)
zw.Write([]byte("This is Gzip response testing"))
zw.Close()
} else if r.URL.Path == "/gzip-test-gziped-empty-body" {
w.Header().Set(hdrContentTypeKey, plainTextType)
w.Header().Set(hdrContentEncodingKey, "gzip")
zw := gzip.NewWriter(w)
// write gziped empty body
zw.Write([]byte(""))
zw.Close()
} else if r.URL.Path == "/gzip-test-no-gziped-body" {
w.Header().Set(hdrContentTypeKey, plainTextType)
w.Header().Set(hdrContentEncodingKey, "gzip")
// don't write body
}

return
}

Expand Down

0 comments on commit 59cee09

Please sign in to comment.