-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_maxsize_test.go
More file actions
396 lines (365 loc) · 13.1 KB
/
Copy pathresponse_maxsize_test.go
File metadata and controls
396 lines (365 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package req
import (
"bytes"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"github.com/imroc/req/v3/internal/tests"
)
func TestMaxResponseSizeWithinLimit(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
// "TestGet: text response" is 23 bytes
resp, err := c.SetMaxResponseSize(100).R().Get("/")
assertSuccess(t, resp, err)
tests.AssertEqual(t, "TestGet: text response", resp.String())
})
}
func TestMaxResponseSizeExactLimit(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
const size = 64
resp, err := c.SetMaxResponseSize(size).R().Get("/fixed-size?size=64")
assertSuccess(t, resp, err)
tests.AssertEqual(t, size, len(resp.Bytes()))
})
}
func TestMaxResponseSizeContentLengthExceeded(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
resp, err := c.SetMaxResponseSize(10).R().Get("/fixed-size?size=100")
if err == nil {
t.Fatal("expected error when Content-Length exceeds limit")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
var tooLarge *ResponseBodyTooLargeError
if !errors.As(err, &tooLarge) {
t.Fatalf("expected *ResponseBodyTooLargeError, got %T", err)
}
tests.AssertEqual(t, int64(10), tooLarge.Limit)
tests.AssertEqual(t, int64(100), tooLarge.ContentLength)
// Response headers/status should still be available.
tests.AssertEqual(t, http.StatusOK, resp.StatusCode)
// Body must not have been buffered.
tests.AssertEqual(t, 0, len(resp.Bytes()))
})
}
func TestMaxResponseSizeChunkedExceeded(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
// Chunked response has no Content-Length; limit is enforced while reading.
resp, err := c.SetMaxResponseSize(50).R().Get("/chunked-size?size=200")
if err == nil {
t.Fatal("expected error when chunked body exceeds limit")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
var tooLarge *ResponseBodyTooLargeError
if !errors.As(err, &tooLarge) {
t.Fatalf("expected *ResponseBodyTooLargeError, got %T", err)
}
tests.AssertEqual(t, int64(50), tooLarge.Limit)
tests.AssertEqual(t, int64(-1), tooLarge.ContentLength)
tests.AssertEqual(t, http.StatusOK, resp.StatusCode)
// Partial body may have been read up to the limit.
if len(resp.Bytes()) > 50 {
t.Fatalf("buffered body larger than limit: %d", len(resp.Bytes()))
}
})
}
func TestMaxResponseSizeChunkedWithinLimit(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
resp, err := c.SetMaxResponseSize(500).R().Get("/chunked-size?size=100")
assertSuccess(t, resp, err)
tests.AssertEqual(t, 100, len(resp.Bytes()))
})
}
func TestMaxResponseSizeRequestOverridesClient(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
c.SetMaxResponseSize(10)
// Request-level higher limit allows the larger body.
resp, err := c.R().SetMaxResponseSize(200).Get("/fixed-size?size=100")
assertSuccess(t, resp, err)
tests.AssertEqual(t, 100, len(resp.Bytes()))
// Request-level lower limit rejects a body the client would accept.
c.SetMaxResponseSize(1000)
resp, err = c.R().SetMaxResponseSize(10).Get("/fixed-size?size=100")
if err == nil {
t.Fatal("expected request-level limit to reject response")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
_ = resp
})
}
func TestMaxResponseSizeRequestDisablesClientLimit(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
c.SetMaxResponseSize(10)
// 0 on the request disables the limit for this request.
resp, err := c.R().SetMaxResponseSize(0).Get("/fixed-size?size=100")
assertSuccess(t, resp, err)
tests.AssertEqual(t, 100, len(resp.Bytes()))
})
}
func TestMaxResponseSizeDisableAutoRead(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
c.SetMaxResponseSize(50).DisableAutoReadResponse()
resp, err := c.R().Get("/chunked-size?size=200")
// Headers succeed; body is not auto-read.
tests.AssertNoError(t, err)
tests.AssertEqual(t, http.StatusOK, resp.StatusCode)
// Manual read hits the limit.
_, readErr := io.ReadAll(resp.Body)
if readErr == nil {
t.Fatal("expected error when reading oversized body manually")
}
if !errors.Is(readErr, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", readErr)
}
_ = resp.Body.Close()
})
}
func TestMaxResponseSizeToBytes(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
c.SetMaxResponseSize(50).DisableAutoReadResponse()
resp, err := c.R().Get("/chunked-size?size=200")
tests.AssertNoError(t, err)
_, err = resp.ToBytes()
if err == nil {
t.Fatal("expected ToBytes to fail when body exceeds limit")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
if !errors.Is(resp.Err, ErrResponseBodyTooLarge) {
t.Fatalf("expected resp.Err to be ErrResponseBodyTooLarge, got %v", resp.Err)
}
})
}
func TestMaxResponseSizeDownload(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
dir := t.TempDir()
out := filepath.Join(dir, "out.bin")
// Content-Length over limit: reject without writing a full file.
resp, err := c.SetMaxResponseSize(10).R().
SetOutputFile(out).
Get("/fixed-size?size=100")
if err == nil {
t.Fatal("expected download to fail when Content-Length exceeds limit")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
_ = resp
// File should not have been created (handleDownload skipped on prior error).
if _, statErr := os.Stat(out); !os.IsNotExist(statErr) {
t.Fatalf("expected output file not to be created, stat err=%v", statErr)
}
// Chunked body over limit: download fails mid-stream.
out2 := filepath.Join(dir, "out2.bin")
resp, err = c.SetMaxResponseSize(50).R().
SetOutputFile(out2).
Get("/chunked-size?size=200")
if err == nil {
t.Fatal("expected download to fail when body exceeds limit during read")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
_ = resp
})
}
func TestMaxResponseSizeDownloadWriter(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
var buf bytes.Buffer
resp, err := c.SetMaxResponseSize(50).R().
SetOutput(&buf).
Get("/chunked-size?size=200")
if err == nil {
t.Fatal("expected error when writing oversized body to output")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
if buf.Len() > 50 {
t.Fatalf("wrote more than limit: %d", buf.Len())
}
_ = resp
})
}
func TestMaxResponseSizeLargeContentLengthNoFullRead(t *testing.T) {
// Regression for the original bandwidth concern: a huge Content-Length must
// be rejected without buffering the body.
testWithAllTransport(t, func(t *testing.T, c *Client) {
resp, err := c.SetMaxResponseSize(1024).R().Get("/download")
if err == nil {
t.Fatal("expected error for 100MiB Content-Length with 1KiB limit")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
var tooLarge *ResponseBodyTooLargeError
if !errors.As(err, &tooLarge) {
t.Fatalf("expected *ResponseBodyTooLargeError, got %T", err)
}
tests.AssertEqual(t, int64(1024), tooLarge.Limit)
if tooLarge.ContentLength <= 1024 {
t.Fatalf("expected ContentLength > limit, got %d", tooLarge.ContentLength)
}
tests.AssertEqual(t, 0, len(resp.Bytes()))
})
}
func TestMaxResponseSizeZeroMeansUnlimited(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
resp, err := c.SetMaxResponseSize(0).R().Get("/fixed-size?size=100")
assertSuccess(t, resp, err)
tests.AssertEqual(t, 100, len(resp.Bytes()))
})
}
func TestMaxResponseSizeNegativeTreatedAsUnlimited(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
resp, err := c.SetMaxResponseSize(-1).R().Get("/fixed-size?size=100")
assertSuccess(t, resp, err)
tests.AssertEqual(t, 100, len(resp.Bytes()))
})
}
func TestMaxResponseSizeClone(t *testing.T) {
c := tc().SetMaxResponseSize(42)
cc := c.Clone()
tests.AssertEqual(t, int64(42), cc.maxResponseSize)
// Changing the clone must not affect the original.
cc.SetMaxResponseSize(99)
tests.AssertEqual(t, int64(42), c.maxResponseSize)
tests.AssertEqual(t, int64(99), cc.maxResponseSize)
}
func TestMaxResponseSizeErrorMessage(t *testing.T) {
e1 := &ResponseBodyTooLargeError{Limit: 10, ContentLength: 100}
if !strings.Contains(e1.Error(), "Content-Length 100") {
t.Fatalf("unexpected error message: %s", e1.Error())
}
if !strings.Contains(e1.Error(), "limit 10") {
t.Fatalf("unexpected error message: %s", e1.Error())
}
e2 := &ResponseBodyTooLargeError{Limit: 50, ContentLength: -1}
if !strings.Contains(e2.Error(), "exceeds limit of 50") {
t.Fatalf("unexpected error message: %s", e2.Error())
}
if !errors.Is(e1, ErrResponseBodyTooLarge) || !errors.Is(e2, ErrResponseBodyTooLarge) {
t.Fatal("errors.Is should match ErrResponseBodyTooLarge")
}
}
func TestMaxResponseBodyReaderStickyError(t *testing.T) {
r := &maxResponseBodyReader{
r: io.NopCloser(bytes.NewReader(bytes.Repeat([]byte{'a'}, 100))),
n: 10,
limit: 10,
}
buf := make([]byte, 64)
n, err := r.Read(buf)
if err == nil {
t.Fatal("expected error on first oversized read")
}
if n > 10 {
t.Fatalf("read more than limit: %d", n)
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
// Sticky: subsequent reads return the same error.
_, err2 := r.Read(buf)
if !errors.Is(err2, ErrResponseBodyTooLarge) {
t.Fatalf("expected sticky ErrResponseBodyTooLarge, got %v", err2)
}
}
func TestMaxResponseBodyReaderExactLimit(t *testing.T) {
data := bytes.Repeat([]byte{'b'}, 10)
r := &maxResponseBodyReader{
r: io.NopCloser(bytes.NewReader(data)),
n: 10,
limit: 10,
}
got, err := io.ReadAll(r)
tests.AssertNoError(t, err)
tests.AssertEqual(t, data, got)
}
func TestMaxResponseSizeWithSuccessResult(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
var result map[string]string
resp, err := c.SetMaxResponseSize(5).R().
SetSuccessResult(&result).
Get("/json")
if err == nil {
t.Fatal("expected error when JSON body exceeds limit")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
_ = resp
})
}
func TestMaxResponseSizeHEADDoesNotEarlyReject(t *testing.T) {
// HEAD responses advertise Content-Length of the resource but carry no body.
// The limit must not reject them, or ParallelDownload size probes break.
testWithAllTransport(t, func(t *testing.T, c *Client) {
resp, err := c.SetMaxResponseSize(10).R().Head("/fixed-size?size=100")
assertSuccess(t, resp, err)
tests.AssertEqual(t, int64(100), resp.ContentLength)
tests.AssertEqual(t, 0, len(resp.Bytes()))
})
}
func TestMaxResponseSizeDownloadStillRunsAfterUnmarshalError(t *testing.T) {
// Combining SetOutput with SetSuccessResult: if unmarshalling fails, the
// download path should still write the buffered body (pre-existing contract).
testWithAllTransport(t, func(t *testing.T, c *Client) {
var buf bytes.Buffer
var result struct {
Missing string `json:"missing_required_shape"`
}
// Valid JSON that unmarshals into the empty struct without error... use
// invalid JSON endpoint would 404. Use /fixed-size which is not JSON so
// unmarshal fails, while body is auto-read and available for download.
resp, err := c.R().
SetSuccessResult(&result).
SetOutput(&buf).
Get("/fixed-size?size=32")
// Unmarshal of non-JSON body should fail.
if err == nil {
// Some configs may not set Result when status is success but content is not JSON —
// ensure we at least got a response and the body was still written to output.
t.Logf("unmarshal did not error (err=nil); body written=%d status=%d", buf.Len(), resp.StatusCode)
}
if buf.Len() != 32 {
// If handleDownload was skipped incorrectly, buf would be empty.
// parseResponseBody runs first and may leave body in resp.body;
// handleDownload should still copy it.
if buf.Len() == 0 && resp != nil && len(resp.Bytes()) == 32 {
t.Fatal("download was skipped after non-size error; body was buffered but not written to output")
}
if buf.Len() != 32 {
t.Fatalf("expected 32 bytes written to output, got %d (err=%v)", buf.Len(), err)
}
}
})
}
func TestMaxResponseSizeContentLengthEarlyRejectSkipsDownload(t *testing.T) {
testWithAllTransport(t, func(t *testing.T, c *Client) {
var buf bytes.Buffer
resp, err := c.SetMaxResponseSize(10).R().
SetOutput(&buf).
Get("/fixed-size?size=100")
if err == nil {
t.Fatal("expected size limit error")
}
if !errors.Is(err, ErrResponseBodyTooLarge) {
t.Fatalf("expected ErrResponseBodyTooLarge, got %v", err)
}
if buf.Len() != 0 {
t.Fatalf("expected no download output on Content-Length early reject, got %d bytes", buf.Len())
}
_ = resp
})
}