Skip to content

Commit 4824648

Browse files
Fix flaky test (#11014)
1 parent 3f7a2e9 commit 4824648

File tree

1 file changed

+56
-71
lines changed

1 file changed

+56
-71
lines changed

tests/test_web_urldispatcher.py

Lines changed: 56 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ async def test_access_non_existing_resource(
345345
client = await aiohttp_client(app)
346346

347347
# 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
350350

351351

352352
@pytest.mark.parametrize(
@@ -369,8 +369,8 @@ async def handler(request: web.Request) -> web.Response:
369369
app.router.add_get(registered_path, handler)
370370
client = await aiohttp_client(app)
371371

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
374374

375375

376376
async def test_handler_metadata_persistence() -> None:
@@ -405,8 +405,8 @@ async def test_static_directory_without_read_permission(
405405
app.router.add_static("/", str(tmp_path), show_index=True)
406406
client = await aiohttp_client(app)
407407

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
410410

411411

412412
@pytest.mark.parametrize("file_request", ["", "my_file.txt"])
@@ -440,10 +440,10 @@ def mock_is_dir(self: pathlib.Path, **kwargs: Any) -> bool:
440440
app.router.add_static("/", str(tmp_path), show_index=True)
441441
client = await aiohttp_client(app)
442442

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
447447

448448

449449
@pytest.mark.skipif(
@@ -461,8 +461,8 @@ async def test_static_file_without_read_permission(
461461
app.router.add_static("/", str(tmp_path))
462462
client = await aiohttp_client(app)
463463

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
466466

467467

468468
async def test_static_file_with_mock_permission_error(
@@ -490,10 +490,10 @@ def mock_open(self: pathlib.Path, *args: Any, **kwargs: Any) -> Any:
490490
client = await aiohttp_client(app)
491491

492492
# 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
497497

498498

499499
async def test_access_symlink_loop(
@@ -510,8 +510,8 @@ async def test_access_symlink_loop(
510510
client = await aiohttp_client(app)
511511

512512
# 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
515515

516516

517517
async def test_access_compressed_file_as_symlink(
@@ -530,9 +530,8 @@ async def test_access_compressed_file_as_symlink(
530530
client = await aiohttp_client(app)
531531

532532
# 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
536535

537536
# Again symlin is ignored, and then uncompressed is served.
538537
txt_file = gz_link.with_suffix("")
@@ -564,8 +563,8 @@ async def test_access_special_resource(
564563
app.router.add_static("/", str(tmp_path))
565564

566565
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
569568
my_socket.close()
570569

571570

@@ -595,8 +594,8 @@ def mock_stat(path: Any, **kwargs: Any) -> os.stat_result:
595594
app.router.add_static("/", str(tmp_path))
596595
client = await aiohttp_client(app)
597596

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
600599

601600

602601
async def test_partially_applied_handler(aiohttp_client: AiohttpClient) -> None:
@@ -626,8 +625,8 @@ async def test_static_head(
626625
app.router.add_static("/", str(tmp_path))
627626
client = await aiohttp_client(app)
628627

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
631630

632631
# Check that there is no content sent (see #4809). This can't easily be
633632
# done with aiohttp_client because the buffering can consume the content.
@@ -665,21 +664,17 @@ async def handler(request: web.Request) -> web.Response:
665664
app.router.add_get("/b", handler, allow_head=False, name="b")
666665
client = await aiohttp_client(app)
667666

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
671669

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
675672

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
679675

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
683678

684679

685680
@pytest.mark.parametrize(
@@ -736,17 +731,14 @@ async def post(self) -> web.Response:
736731

737732
client = await aiohttp_client(app)
738733

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
742736

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
746739

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
750742

751743

752744
async def test_decorate_view(aiohttp_client: AiohttpClient) -> None:
@@ -765,17 +757,14 @@ async def post(self) -> web.Response:
765757

766758
client = await aiohttp_client(app)
767759

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
771762

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
775765

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
779768

780769

781770
async def test_web_view(aiohttp_client: AiohttpClient) -> None:
@@ -792,17 +781,14 @@ async def post(self) -> web.Response:
792781

793782
client = await aiohttp_client(app)
794783

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
798786

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
802789

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
806792

807793

808794
async def test_static_absolute_url(
@@ -817,8 +803,8 @@ async def test_static_absolute_url(
817803
here = pathlib.Path(__file__).parent
818804
app.router.add_static("/static", here)
819805
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
822808

823809

824810
async def test_for_issue_5250(
@@ -960,9 +946,8 @@ async def get(self) -> web.Response:
960946

961947
client = await aiohttp_client(app)
962948

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
966951

967952

968953
async def test_route_with_regex(aiohttp_client: AiohttpClient) -> None:

0 commit comments

Comments
 (0)