4
4
"context"
5
5
"fmt"
6
6
"io"
7
- "io/ioutil"
8
7
"net/http"
9
8
"net/http/httptest"
10
9
"reflect"
@@ -86,7 +85,7 @@ var callTests = []struct {
86
85
err1 , ok := errgo .Cause (err ).(* httprequest.DecodeResponseError )
87
86
c .Assert (ok , qt .Equals , true , qt .Commentf ("error not of type *httprequest.DecodeResponseError (%T)" , errgo .Cause (err )))
88
87
c .Assert (err1 .Response , qt .Not (qt .IsNil ))
89
- data , err := ioutil .ReadAll (err1 .Response .Body )
88
+ data , err := io .ReadAll (err1 .Response .Body )
90
89
c .Assert (err , qt .Equals , nil )
91
90
c .Assert (string (data ), qt .Equals , "bad response" )
92
91
},
@@ -99,7 +98,7 @@ var callTests = []struct {
99
98
err1 , ok := errgo .Cause (err ).(* httprequest.DecodeResponseError )
100
99
c .Assert (ok , qt .Equals , true , qt .Commentf ("error not of type *httprequest.DecodeResponseError (%T)" , errgo .Cause (err )))
101
100
c .Assert (err1 .Response , qt .Not (qt .IsNil ))
102
- data , err := ioutil .ReadAll (err1 .Response .Body )
101
+ data , err := io .ReadAll (err1 .Response .Body )
103
102
c .Assert (err , qt .Equals , nil )
104
103
c .Assert (string (data ), qt .Equals , "bad error value" )
105
104
c .Assert (err1 .Response .StatusCode , qt .Equals , http .StatusTeapot )
@@ -152,10 +151,11 @@ var callTests = []struct {
152
151
153
152
func TestCall (t * testing.T ) {
154
153
c := qt .New (t )
155
- defer c .Done ()
156
154
157
155
srv := newServer ()
158
- c .Defer (srv .Close )
156
+ c .Cleanup (func () {
157
+ srv .Close ()
158
+ })
159
159
160
160
for _ , test := range callTests {
161
161
c .Run (test .about , func (c * qt.C ) {
@@ -183,10 +183,11 @@ func TestCall(t *testing.T) {
183
183
184
184
func TestCallURLNoRequestPath (t * testing.T ) {
185
185
c := qt .New (t )
186
- defer c .Done ()
187
186
188
187
srv := newServer ()
189
- c .Defer (srv .Close )
188
+ c .Cleanup (func () {
189
+ srv .Close ()
190
+ })
190
191
191
192
var client httprequest.Client
192
193
req := struct {
@@ -263,16 +264,13 @@ var doTests = []struct {
263
264
expectResp : & chM2Resp {"foo" , 999 },
264
265
}}
265
266
266
- func newInt64 (i int64 ) * int64 {
267
- return & i
268
- }
269
-
270
267
func TestDo (t * testing.T ) {
271
268
c := qt .New (t )
272
- defer c .Done ()
273
269
274
270
srv := newServer ()
275
- c .Defer (srv .Close )
271
+ c .Cleanup (func () {
272
+ srv .Close ()
273
+ })
276
274
277
275
for _ , test := range doTests {
278
276
test := test
@@ -302,10 +300,11 @@ func TestDo(t *testing.T) {
302
300
303
301
func TestDoWithHTTPReponse (t * testing.T ) {
304
302
c := qt .New (t )
305
- defer c .Done ()
306
303
307
304
srv := newServer ()
308
- c .Defer (srv .Close )
305
+ c .Cleanup (func () {
306
+ srv .Close ()
307
+ })
309
308
310
309
client := & httprequest.Client {
311
310
BaseURL : srv .URL ,
@@ -314,17 +313,18 @@ func TestDoWithHTTPReponse(t *testing.T) {
314
313
err := client .Get (context .Background (), "/m1/foo" , & resp )
315
314
c .Assert (err , qt .Equals , nil )
316
315
defer resp .Body .Close ()
317
- data , err := ioutil .ReadAll (resp .Body )
316
+ data , err := io .ReadAll (resp .Body )
318
317
c .Assert (err , qt .Equals , nil )
319
318
c .Assert (string (data ), qt .Equals , `{"P":"foo"}` )
320
319
}
321
320
322
321
func TestDoWithHTTPReponseAndError (t * testing.T ) {
323
322
c := qt .New (t )
324
- defer c .Done ()
325
323
326
324
srv := newServer ()
327
- c .Defer (srv .Close )
325
+ c .Cleanup (func () {
326
+ srv .Close ()
327
+ })
328
328
329
329
var doer closeCountingDoer // Also check the body is closed.
330
330
client := & httprequest.Client {
@@ -341,10 +341,11 @@ func TestDoWithHTTPReponseAndError(t *testing.T) {
341
341
342
342
func TestCallWithHTTPResponse (t * testing.T ) {
343
343
c := qt .New (t )
344
- defer c .Done ()
345
344
346
345
srv := newServer ()
347
- c .Defer (srv .Close )
346
+ c .Cleanup (func () {
347
+ srv .Close ()
348
+ })
348
349
349
350
client := & httprequest.Client {
350
351
BaseURL : srv .URL ,
@@ -353,18 +354,20 @@ func TestCallWithHTTPResponse(t *testing.T) {
353
354
err := client .Call (context .Background (), & chM1Req {
354
355
P : "foo" ,
355
356
}, & resp )
357
+ c .Assert (err , qt .IsNil )
356
358
defer resp .Body .Close ()
357
- data , err := ioutil .ReadAll (resp .Body )
359
+ data , err := io .ReadAll (resp .Body )
358
360
c .Assert (err , qt .Equals , nil )
359
361
c .Assert (string (data ), qt .Equals , `{"P":"foo"}` )
360
362
}
361
363
362
364
func TestCallClosesResponseBodyOnSuccess (t * testing.T ) {
363
365
c := qt .New (t )
364
- defer c .Done ()
365
366
366
367
srv := newServer ()
367
- c .Defer (srv .Close )
368
+ c .Cleanup (func () {
369
+ srv .Close ()
370
+ })
368
371
369
372
var doer closeCountingDoer
370
373
client := & httprequest.Client {
@@ -383,10 +386,11 @@ func TestCallClosesResponseBodyOnSuccess(t *testing.T) {
383
386
384
387
func TestCallClosesResponseBodyOnError (t * testing.T ) {
385
388
c := qt .New (t )
386
- defer c .Done ()
387
389
388
390
srv := newServer ()
389
- c .Defer (srv .Close )
391
+ c .Cleanup (func () {
392
+ srv .Close ()
393
+ })
390
394
391
395
var doer closeCountingDoer
392
396
client := & httprequest.Client {
@@ -401,10 +405,11 @@ func TestCallClosesResponseBodyOnError(t *testing.T) {
401
405
402
406
func TestDoClosesResponseBodyOnSuccess (t * testing.T ) {
403
407
c := qt .New (t )
404
- defer c .Done ()
405
408
406
409
srv := newServer ()
407
- c .Defer (srv .Close )
410
+ c .Cleanup (func () {
411
+ srv .Close ()
412
+ })
408
413
409
414
var doer closeCountingDoer
410
415
client := & httprequest.Client {
@@ -423,10 +428,11 @@ func TestDoClosesResponseBodyOnSuccess(t *testing.T) {
423
428
424
429
func TestDoClosesResponseBodyOnError (t * testing.T ) {
425
430
c := qt .New (t )
426
- defer c .Done ()
427
431
428
432
srv := newServer ()
429
- c .Defer (srv .Close )
433
+ c .Cleanup (func () {
434
+ srv .Close ()
435
+ })
430
436
431
437
var doer closeCountingDoer
432
438
client := & httprequest.Client {
@@ -443,10 +449,11 @@ func TestDoClosesResponseBodyOnError(t *testing.T) {
443
449
444
450
func TestGet (t * testing.T ) {
445
451
c := qt .New (t )
446
- defer c .Done ()
447
452
448
453
srv := newServer ()
449
- c .Defer (srv .Close )
454
+ c .Cleanup (func () {
455
+ srv .Close ()
456
+ })
450
457
451
458
client := httprequest.Client {
452
459
BaseURL : srv .URL ,
@@ -459,10 +466,11 @@ func TestGet(t *testing.T) {
459
466
460
467
func TestGetNoBaseURL (t * testing.T ) {
461
468
c := qt .New (t )
462
- defer c .Done ()
463
469
464
470
srv := newServer ()
465
- c .Defer (srv .Close )
471
+ c .Cleanup (func () {
472
+ srv .Close ()
473
+ })
466
474
467
475
client := httprequest.Client {}
468
476
var resp chM1Resp
@@ -479,7 +487,7 @@ func TestUnmarshalJSONResponseWithBodyReadError(t *testing.T) {
479
487
"Content-Type" : {"application/json" },
480
488
},
481
489
StatusCode : http .StatusOK ,
482
- Body : ioutil .NopCloser (io .MultiReader (
490
+ Body : io .NopCloser (io .MultiReader (
483
491
strings .NewReader (`{"one": "two"}` ),
484
492
errorReader ("some bad read" ),
485
493
)),
@@ -535,7 +543,7 @@ func TestUnmarshalJSONResponseWithVariedJSONContentTypes(t *testing.T) {
535
543
"Content-Type" : {test .contentType },
536
544
},
537
545
StatusCode : http .StatusTeapot ,
538
- Body : ioutil .NopCloser (strings .NewReader (`{}` )),
546
+ Body : io .NopCloser (strings .NewReader (`{}` )),
539
547
}
540
548
var val map [string ]string
541
549
err := httprequest .UnmarshalJSONResponse (resp , & val )
@@ -552,7 +560,6 @@ func TestUnmarshalJSONResponseWithVariedJSONContentTypes(t *testing.T) {
552
560
553
561
func TestUnmarshalJSONResponseWithErrorAndLargeBody (t * testing.T ) {
554
562
c := qt .New (t )
555
- defer c .Done ()
556
563
557
564
c .Patch (httprequest .MaxErrorBodySize , 11 )
558
565
@@ -561,7 +568,7 @@ func TestUnmarshalJSONResponseWithErrorAndLargeBody(t *testing.T) {
561
568
"Content-Type" : {"foo/bar" },
562
569
},
563
570
StatusCode : http .StatusOK ,
564
- Body : ioutil .NopCloser (strings .NewReader (`123456789 123456789` )),
571
+ Body : io .NopCloser (strings .NewReader (`123456789 123456789` )),
565
572
}
566
573
var val map [string ]string
567
574
err := httprequest .UnmarshalJSONResponse (resp , & val )
@@ -572,7 +579,6 @@ func TestUnmarshalJSONResponseWithErrorAndLargeBody(t *testing.T) {
572
579
573
580
func TestUnmarshalJSONResponseWithLargeBody (t * testing.T ) {
574
581
c := qt .New (t )
575
- defer c .Done ()
576
582
577
583
c .Patch (httprequest .MaxErrorBodySize , 11 )
578
584
@@ -581,7 +587,7 @@ func TestUnmarshalJSONResponseWithLargeBody(t *testing.T) {
581
587
"Content-Type" : {"application/json" },
582
588
},
583
589
StatusCode : http .StatusOK ,
584
- Body : ioutil .NopCloser (strings .NewReader (`"23456789 123456789"` )),
590
+ Body : io .NopCloser (strings .NewReader (`"23456789 123456789"` )),
585
591
}
586
592
var val string
587
593
err := httprequest .UnmarshalJSONResponse (resp , & val )
@@ -597,7 +603,7 @@ func TestUnmarshalJSONWithDecodeError(t *testing.T) {
597
603
"Content-Type" : {"application/json" },
598
604
},
599
605
StatusCode : http .StatusOK ,
600
- Body : ioutil .NopCloser (strings .NewReader (`{"one": "two"}` )),
606
+ Body : io .NopCloser (strings .NewReader (`{"one": "two"}` )),
601
607
}
602
608
var val chan string
603
609
err := httprequest .UnmarshalJSONResponse (resp , & val )
@@ -608,7 +614,6 @@ func TestUnmarshalJSONWithDecodeError(t *testing.T) {
608
614
609
615
func TestUnmarshalJSONWithDecodeErrorAndLargeBody (t * testing.T ) {
610
616
c := qt .New (t )
611
- defer c .Done ()
612
617
613
618
c .Patch (httprequest .MaxErrorBodySize , 11 )
614
619
@@ -617,7 +622,7 @@ func TestUnmarshalJSONWithDecodeErrorAndLargeBody(t *testing.T) {
617
622
"Content-Type" : {"application/json" },
618
623
},
619
624
StatusCode : http .StatusOK ,
620
- Body : ioutil .NopCloser (strings .NewReader (`"23456789 123456789"` )),
625
+ Body : io .NopCloser (strings .NewReader (`"23456789 123456789"` )),
621
626
}
622
627
var val chan string
623
628
err := httprequest .UnmarshalJSONResponse (resp , & val )
@@ -629,7 +634,7 @@ func TestUnmarshalJSONWithDecodeErrorAndLargeBody(t *testing.T) {
629
634
func assertDecodeResponseError (c * qt.C , err error , status int , body string ) {
630
635
err1 , ok := errgo .Cause (err ).(* httprequest.DecodeResponseError )
631
636
c .Assert (ok , qt .Equals , true , qt .Commentf ("error not of type *httprequest.DecodeResponseError (%T)" , errgo .Cause (err )))
632
- data , err := ioutil .ReadAll (err1 .Response .Body )
637
+ data , err := io .ReadAll (err1 .Response .Body )
633
638
c .Assert (err , qt .Equals , nil )
634
639
c .Assert (err1 .Response .StatusCode , qt .Equals , status )
635
640
c .Assert (string (data ), qt .Equals , body )
@@ -849,39 +854,6 @@ func (r *closeCountingReader) Close() error {
849
854
return r .ReadCloser .Close ()
850
855
}
851
856
852
- // largeReader implements a reader that produces up to total bytes
853
- // in 1 byte reads.
854
- type largeReader struct {
855
- byte byte
856
- total int
857
- n int
858
- }
859
-
860
- func (r * largeReader ) Read (buf []byte ) (int , error ) {
861
- if r .n >= r .total {
862
- return 0 , io .EOF
863
- }
864
- r .n ++
865
- return copy (buf , []byte {r .byte }), nil
866
- }
867
-
868
- func (r * largeReader ) Seek (offset int64 , whence int ) (int64 , error ) {
869
- if offset != 0 || whence != 0 {
870
- panic ("unexpected seek" )
871
- }
872
- r .n = 0
873
- return 0 , nil
874
- }
875
-
876
- func (r * largeReader ) Close () error {
877
- // By setting n to zero, we ensure that if there's
878
- // a concurrent read, it will also read from n
879
- // and so the race detector should pick up the
880
- // problem.
881
- r .n = 0
882
- return nil
883
- }
884
-
885
857
func isRemoteError (err error ) bool {
886
858
_ , ok := err .(* httprequest.RemoteError )
887
859
return ok
0 commit comments