Skip to content

Commit c7bedfa

Browse files
committed
feat: add DisableDecompress field.
1 parent 4e9f689 commit c7bedfa

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ There are the available config options for performing a request, and all fields
171171
| `Body` | `any` | The request body. |
172172
| `ContentType` | `string` | The content type of this request. Available options are: `"json"`, and default `"json"`. |
173173
| `Context` | `context.Context` | Self-control context. |
174+
| `DisableDecompress` | `bool` | Indicates whether or not disable decompression of the response body automatically. |
174175
| `Headers` | `map[string][]string` | Custom headers to be sent. |
175176
| `MaxRedirects` | `int` | The maximum number of redirects for the request, default 5. |
176177
| `Method` | `string` | HTTP request method, default `GET`. |

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ resp, err := cli.GET("/products/1")
170170
| `Body` | `any` | 请求内容 |
171171
| `ContentType` | `string` | 请求内容类型,当前可用值包括:`"json"`,默认为`"json"` |
172172
| `Context` | `context.Context` | 用于请求的上下文 |
173+
| `DisableDecompress` | `bool` | 是否禁用自动解压 |
173174
| `Headers` | `map[string][]string` | 自定义请求头部 |
174175
| `MaxRedirects` | `int` | 最大跳转次数 |
175176
| `Method` | `string` | 请求方式,默认为`GET` |

builtin_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestRequestWithBody(t *testing.T) {
107107
func TestRequestWithGZipEncodedBody(t *testing.T) {
108108
a := assert.New(t)
109109

110-
data, _, err := ToObject[testResponse](Request("", RequestOptions{
110+
data, resp, err := ToObject[testResponse](Request("", RequestOptions{
111111
BaseURL: "http://localhost:8080",
112112
Headers: map[string][]string{
113113
"Accept-Encoding": {"gzip", "deflate"},
@@ -117,12 +117,13 @@ func TestRequestWithGZipEncodedBody(t *testing.T) {
117117

118118
a.NotNilNow(data.Method)
119119
a.EqualNow(*data.Method, "GET")
120+
a.NotTrueNow(resp.Header.Get("Content-Encoding"))
120121
}
121122

122123
func TestRequestWithDeflateEncodedBody(t *testing.T) {
123124
a := assert.New(t)
124125

125-
data, _, err := ToObject[testResponse](Request("", RequestOptions{
126+
data, resp, err := ToObject[testResponse](Request("", RequestOptions{
126127
BaseURL: "http://localhost:8080",
127128
Headers: map[string][]string{
128129
"Accept-Encoding": {"deflate"},
@@ -132,6 +133,7 @@ func TestRequestWithDeflateEncodedBody(t *testing.T) {
132133

133134
a.NotNilNow(data.Method)
134135
a.EqualNow(*data.Method, "GET")
136+
a.NotTrueNow(resp.Header.Get("Content-Encoding"))
135137
}
136138

137139
func TestRequestWithInvalidContentEncoding(t *testing.T) {
@@ -160,6 +162,21 @@ func TestRequestWithInvalidContentEncoding(t *testing.T) {
160162
a.NotNilNow(err)
161163
}
162164

165+
func TestRequestWithContentEncodingAndDisableDecompress(t *testing.T) {
166+
a := assert.New(t)
167+
168+
resp, err := Request("", RequestOptions{
169+
BaseURL: "http://localhost:8080",
170+
Headers: map[string][]string{
171+
"Accept-Encoding": {"gzip", "deflate"},
172+
},
173+
DisableDecompress: true,
174+
})
175+
a.NilNow(err)
176+
177+
a.EqualNow(resp.Header.Get("Content-Encoding"), "gzip")
178+
}
179+
163180
func TestRequestWithParameters(t *testing.T) {
164181
a := assert.New(t)
165182

request.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type RequestOptions struct {
7070
// Context: ctx,
7171
// })
7272
Context context.Context
73+
// DisableDecompress indicates whether or not disable decompression of the response body
74+
// automatically. If it is set to `true`, it will not decompress the response body.
75+
DisableDecompress bool
7376
// Headers are custom headers to be sent.
7477
//
7578
// resp, err := request.Request("http://example.com", request.RequestOptions{
@@ -169,7 +172,9 @@ func (cli *Client) handleResponse(
169172
resp *http.Response,
170173
opt RequestOptions,
171174
) (*http.Response, error) {
172-
resp = cli.decodeResponseBody(resp)
175+
if !opt.DisableDecompress {
176+
resp = cli.decodeResponseBody(resp)
177+
}
173178

174179
return cli.validateResponse(resp, opt)
175180
}
@@ -187,13 +192,15 @@ func (cli *Client) decodeResponseBody(resp *http.Response) *http.Response {
187192
reader := flate.NewReader(bytes.NewReader(data))
188193
resp.Body.Close()
189194
resp.Body = reader
195+
resp.Header.Del("Content-Encoding")
190196
case "gzip", "x-gzip":
191197
reader, err := gzip.NewReader(resp.Body)
192198
if err != nil {
193199
return nil
194200
}
195201
resp.Body.Close()
196202
resp.Body = reader
203+
resp.Header.Del("Content-Encoding")
197204
}
198205

199206
return resp

0 commit comments

Comments
 (0)