@@ -345,8 +345,8 @@ async def test_access_non_existing_resource(
345
345
client = await aiohttp_client (app )
346
346
347
347
# Request the root of the static directory.
348
- r = await client .get ("/non_existing_resource" )
349
- assert r .status == 404
348
+ async with client .get ("/non_existing_resource" ) as r :
349
+ assert r .status == 404
350
350
351
351
352
352
@pytest .mark .parametrize (
@@ -369,8 +369,8 @@ async def handler(request: web.Request) -> web.Response:
369
369
app .router .add_get (registered_path , handler )
370
370
client = await aiohttp_client (app )
371
371
372
- r = await client .get (request_url )
373
- assert r .status == 200
372
+ async with client .get (request_url ) as r :
373
+ assert r .status == 200
374
374
375
375
376
376
async def test_handler_metadata_persistence () -> None :
@@ -405,8 +405,8 @@ async def test_static_directory_without_read_permission(
405
405
app .router .add_static ("/" , str (tmp_path ), show_index = True )
406
406
client = await aiohttp_client (app )
407
407
408
- r = await client .get (f"/{ my_dir .name } /{ file_request } " )
409
- assert r .status == 403
408
+ async with client .get (f"/{ my_dir .name } /{ file_request } " ) as r :
409
+ assert r .status == 403
410
410
411
411
412
412
@pytest .mark .parametrize ("file_request" , ["" , "my_file.txt" ])
@@ -440,10 +440,10 @@ def mock_is_dir(self: pathlib.Path, **kwargs: Any) -> bool:
440
440
app .router .add_static ("/" , str (tmp_path ), show_index = True )
441
441
client = await aiohttp_client (app )
442
442
443
- r = await client .get ("/" )
444
- assert r .status == 200
445
- r = await client .get (f"/{ my_dir .name } /{ file_request } " )
446
- assert r .status == 403
443
+ async with client .get ("/" ) as r :
444
+ assert r .status == 200
445
+ async with client .get (f"/{ my_dir .name } /{ file_request } " ) as r :
446
+ assert r .status == 403
447
447
448
448
449
449
@pytest .mark .skipif (
@@ -461,8 +461,8 @@ async def test_static_file_without_read_permission(
461
461
app .router .add_static ("/" , str (tmp_path ))
462
462
client = await aiohttp_client (app )
463
463
464
- r = await client .get (f"/{ my_file .name } " )
465
- assert r .status == 403
464
+ async with client .get (f"/{ my_file .name } " ) as r :
465
+ assert r .status == 403
466
466
467
467
468
468
async def test_static_file_with_mock_permission_error (
@@ -490,10 +490,10 @@ def mock_open(self: pathlib.Path, *args: Any, **kwargs: Any) -> Any:
490
490
client = await aiohttp_client (app )
491
491
492
492
# Test the mock only applies to my_file, then test the permission error.
493
- r = await client .get (f"/{ my_readable .name } " )
494
- assert r .status == 200
495
- r = await client .get (f"/{ my_file .name } " )
496
- assert r .status == 403
493
+ async with client .get (f"/{ my_readable .name } " ) as r :
494
+ assert r .status == 200
495
+ async with client .get (f"/{ my_file .name } " ) as r :
496
+ assert r .status == 403
497
497
498
498
499
499
async def test_access_symlink_loop (
@@ -510,8 +510,8 @@ async def test_access_symlink_loop(
510
510
client = await aiohttp_client (app )
511
511
512
512
# Request the root of the static directory.
513
- r = await client .get ("/" + my_dir_path .name )
514
- assert r .status == 404
513
+ async with client .get ("/" + my_dir_path .name ) as r :
514
+ assert r .status == 404
515
515
516
516
517
517
async def test_access_compressed_file_as_symlink (
@@ -530,9 +530,8 @@ async def test_access_compressed_file_as_symlink(
530
530
client = await aiohttp_client (app )
531
531
532
532
# Symlink should be ignored; response reflects missing uncompressed file.
533
- resp = await client .get (f"/{ gz_link .stem } " , auto_decompress = False )
534
- assert resp .status == 404
535
- resp .release ()
533
+ async with client .get (f"/{ gz_link .stem } " , auto_decompress = False ) as resp :
534
+ assert resp .status == 404
536
535
537
536
# Again symlin is ignored, and then uncompressed is served.
538
537
txt_file = gz_link .with_suffix ("" )
@@ -564,8 +563,8 @@ async def test_access_special_resource(
564
563
app .router .add_static ("/" , str (tmp_path ))
565
564
566
565
client = await aiohttp_client (app )
567
- r = await client .get (f"/{ my_special .name } " )
568
- assert r .status == 403
566
+ async with client .get (f"/{ my_special .name } " ) as r :
567
+ assert r .status == 403
569
568
my_socket .close ()
570
569
571
570
@@ -595,8 +594,8 @@ def mock_stat(path: Any, **kwargs: Any) -> os.stat_result:
595
594
app .router .add_static ("/" , str (tmp_path ))
596
595
client = await aiohttp_client (app )
597
596
598
- r = await client .get (f"/{ my_special .name } " )
599
- assert r .status == 403
597
+ async with client .get (f"/{ my_special .name } " ) as r :
598
+ assert r .status == 403
600
599
601
600
602
601
async def test_partially_applied_handler (aiohttp_client : AiohttpClient ) -> None :
@@ -626,8 +625,8 @@ async def test_static_head(
626
625
app .router .add_static ("/" , str (tmp_path ))
627
626
client = await aiohttp_client (app )
628
627
629
- r = await client .head ("/test.txt" )
630
- assert r .status == 200
628
+ async with client .head ("/test.txt" ) as r :
629
+ assert r .status == 200
631
630
632
631
# Check that there is no content sent (see #4809). This can't easily be
633
632
# done with aiohttp_client because the buffering can consume the content.
@@ -665,21 +664,17 @@ async def handler(request: web.Request) -> web.Response:
665
664
app .router .add_get ("/b" , handler , allow_head = False , name = "b" )
666
665
client = await aiohttp_client (app )
667
666
668
- r = await client .get ("/a" )
669
- assert r .status == 200
670
- r .release ()
667
+ async with client .get ("/a" ) as r :
668
+ assert r .status == 200
671
669
672
- r = await client .head ("/a" )
673
- assert r .status == 200
674
- r .release ()
670
+ async with client .head ("/a" ) as r :
671
+ assert r .status == 200
675
672
676
- r = await client .get ("/b" )
677
- assert r .status == 200
678
- r .release ()
673
+ async with client .get ("/b" ) as r :
674
+ assert r .status == 200
679
675
680
- r = await client .head ("/b" )
681
- assert r .status == 405
682
- r .release ()
676
+ async with client .head ("/b" ) as r :
677
+ assert r .status == 405
683
678
684
679
685
680
@pytest .mark .parametrize (
@@ -736,17 +731,14 @@ async def post(self) -> web.Response:
736
731
737
732
client = await aiohttp_client (app )
738
733
739
- r = await client .get ("/a" )
740
- assert r .status == 200
741
- r .release ()
734
+ async with client .get ("/a" ) as r :
735
+ assert r .status == 200
742
736
743
- r = await client .post ("/a" )
744
- assert r .status == 200
745
- r .release ()
737
+ async with client .post ("/a" ) as r :
738
+ assert r .status == 200
746
739
747
- r = await client .put ("/a" )
748
- assert r .status == 405
749
- r .release ()
740
+ async with client .put ("/a" ) as r :
741
+ assert r .status == 405
750
742
751
743
752
744
async def test_decorate_view (aiohttp_client : AiohttpClient ) -> None :
@@ -765,17 +757,14 @@ async def post(self) -> web.Response:
765
757
766
758
client = await aiohttp_client (app )
767
759
768
- r = await client .get ("/a" )
769
- assert r .status == 200
770
- r .release ()
760
+ async with client .get ("/a" ) as r :
761
+ assert r .status == 200
771
762
772
- r = await client .post ("/a" )
773
- assert r .status == 200
774
- r .release ()
763
+ async with client .post ("/a" ) as r :
764
+ assert r .status == 200
775
765
776
- r = await client .put ("/a" )
777
- assert r .status == 405
778
- r .release ()
766
+ async with client .put ("/a" ) as r :
767
+ assert r .status == 405
779
768
780
769
781
770
async def test_web_view (aiohttp_client : AiohttpClient ) -> None :
@@ -792,17 +781,14 @@ async def post(self) -> web.Response:
792
781
793
782
client = await aiohttp_client (app )
794
783
795
- r = await client .get ("/a" )
796
- assert r .status == 200
797
- r .release ()
784
+ async with client .get ("/a" ) as r :
785
+ assert r .status == 200
798
786
799
- r = await client .post ("/a" )
800
- assert r .status == 200
801
- r .release ()
787
+ async with client .post ("/a" ) as r :
788
+ assert r .status == 200
802
789
803
- r = await client .put ("/a" )
804
- assert r .status == 405
805
- r .release ()
790
+ async with client .put ("/a" ) as r :
791
+ assert r .status == 405
806
792
807
793
808
794
async def test_static_absolute_url (
@@ -817,8 +803,8 @@ async def test_static_absolute_url(
817
803
here = pathlib .Path (__file__ ).parent
818
804
app .router .add_static ("/static" , here )
819
805
client = await aiohttp_client (app )
820
- resp = await client .get ("/static/" + str (file_path .resolve ()))
821
- assert resp .status == 403
806
+ async with client .get ("/static/" + str (file_path .resolve ())) as resp :
807
+ assert resp .status == 403
822
808
823
809
824
810
async def test_for_issue_5250 (
@@ -960,9 +946,8 @@ async def get(self) -> web.Response:
960
946
961
947
client = await aiohttp_client (app )
962
948
963
- r = await client .get ("///a" )
964
- assert r .status == 200
965
- r .release ()
949
+ async with client .get ("///a" ) as r :
950
+ assert r .status == 200
966
951
967
952
968
953
async def test_route_with_regex (aiohttp_client : AiohttpClient ) -> None :
0 commit comments