Skip to content

Commit

Permalink
#142 added gzip handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Apr 20, 2018
1 parent dcac3e6 commit 7069327
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
28 changes: 21 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package resty

import (
"bytes"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"errors"
Expand Down Expand Up @@ -46,11 +47,12 @@ const (
)

var (
hdrUserAgentKey = http.CanonicalHeaderKey("User-Agent")
hdrAcceptKey = http.CanonicalHeaderKey("Accept")
hdrContentTypeKey = http.CanonicalHeaderKey("Content-Type")
hdrContentLengthKey = http.CanonicalHeaderKey("Content-Length")
hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization")
hdrUserAgentKey = http.CanonicalHeaderKey("User-Agent")
hdrAcceptKey = http.CanonicalHeaderKey("Accept")
hdrContentTypeKey = http.CanonicalHeaderKey("Content-Type")
hdrContentLengthKey = http.CanonicalHeaderKey("Content-Length")
hdrContentEncodingKey = http.CanonicalHeaderKey("Content-Encoding")
hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization")

plainTextType = "text/plain; charset=utf-8"
jsonContentType = "application/json; charset=utf-8"
Expand Down Expand Up @@ -797,11 +799,23 @@ func (c *Client) execute(req *Request) (*Response, error) {
}

if !req.isSaveResponse {
body := resp.Body

// GitHub #142
if strings.EqualFold(resp.Header.Get(hdrContentEncodingKey), "gzip") {
if _, ok := body.(*gzip.Reader); !ok {
body, err = gzip.NewReader(body)
if err != nil {
return response, err
}
}
}

defer func() {
_ = resp.Body.Close()
_ = body.Close()
}()

if response.body, err = ioutil.ReadAll(resp.Body); err != nil {
if response.body, err = ioutil.ReadAll(body); err != nil {
return response, err
}

Expand Down
19 changes: 19 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,22 @@ func TestSetLogPrefix(t *testing.T) {
assertEqual(t, "CUSTOM ", c.logPrefix)
assertEqual(t, "CUSTOM ", c.Log.Prefix())
}

func TestAutoGzip(t *testing.T) {
ts := createGenServer(t)
defer ts.Close()

c := New()

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())

logResponse(t, resp)
}
7 changes: 7 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package resty

import (
"compress/gzip"
"encoding/base64"
"encoding/json"
"encoding/xml"
Expand Down Expand Up @@ -433,6 +434,12 @@ func createGenServer(t *testing.T) *httptest.Server {
// text/plain; charset=utf-8
w.Header().Set(hdrContentTypeKey, "")
_, _ = w.Write([]byte(`{"response":"json response no content type set"}`))
} else if r.URL.Path == "/gzip-test" {
w.Header().Set(hdrContentTypeKey, plainTextType)
w.Header().Set(hdrContentEncodingKey, "gzip")
zw := gzip.NewWriter(w)
zw.Write([]byte("This is Gzip response testing"))
zw.Close()
}
return
}
Expand Down

0 comments on commit 7069327

Please sign in to comment.