@@ -77,8 +77,25 @@ def fname(here):
7777 return here / "conftest.py"
7878
7979
80- async def test_keepalive_two_requests_success (aiohttp_client ) -> None :
81- async def handler (request ):
80+ @pytest .fixture
81+ def headers_echo_client (
82+ aiohttp_client : AiohttpClient ,
83+ ) -> Callable [..., Awaitable [TestClient [web .Request , web .Application ]]]:
84+ """Create a client with an app that echoes request headers as JSON."""
85+
86+ async def factory (** kwargs : Any ) -> TestClient [web .Request , web .Application ]:
87+ async def handler (request : web .Request ) -> web .Response :
88+ return web .json_response ({"headers" : dict (request .headers )})
89+
90+ app = web .Application ()
91+ app .router .add_get ("/" , handler )
92+ return await aiohttp_client (app , ** kwargs )
93+
94+ return factory
95+
96+
97+ async def test_keepalive_two_requests_success (aiohttp_client : AiohttpClient ) -> None :
98+ async def handler (request : web .Request ) -> web .Response :
8299 body = await request .read ()
83100 assert b"" == body
84101 return web .Response (body = b"OK" )
@@ -3712,29 +3729,25 @@ async def handler(request):
37123729 assert not ctx ._coro .cr_running
37133730
37143731
3715- async def test_session_auth (aiohttp_client ) -> None :
3716- async def handler (request ):
3717- return web .json_response ({"headers" : dict (request .headers )})
3718-
3719- app = web .Application ()
3720- app .router .add_get ("/" , handler )
3721-
3722- client = await aiohttp_client (app , auth = aiohttp .BasicAuth ("login" , "pass" ))
3732+ async def test_session_auth (
3733+ headers_echo_client : Callable [
3734+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3735+ ],
3736+ ) -> None :
3737+ client = await headers_echo_client (auth = aiohttp .BasicAuth ("login" , "pass" ))
37233738
37243739 r = await client .get ("/" )
37253740 assert r .status == 200
37263741 content = await r .json ()
37273742 assert content ["headers" ]["Authorization" ] == "Basic bG9naW46cGFzcw=="
37283743
37293744
3730- async def test_session_auth_override (aiohttp_client ) -> None :
3731- async def handler (request ):
3732- return web .json_response ({"headers" : dict (request .headers )})
3733-
3734- app = web .Application ()
3735- app .router .add_get ("/" , handler )
3736-
3737- client = await aiohttp_client (app , auth = aiohttp .BasicAuth ("login" , "pass" ))
3745+ async def test_session_auth_override (
3746+ headers_echo_client : Callable [
3747+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3748+ ],
3749+ ) -> None :
3750+ client = await headers_echo_client (auth = aiohttp .BasicAuth ("login" , "pass" ))
37383751
37393752 r = await client .get ("/" , auth = aiohttp .BasicAuth ("other_login" , "pass" ))
37403753 assert r .status == 200
@@ -3756,30 +3769,77 @@ async def handler(request):
37563769 await client .get ("/" , headers = headers )
37573770
37583771
3759- async def test_session_headers (aiohttp_client ) -> None :
3760- async def handler (request ):
3761- return web .json_response ({"headers" : dict (request .headers )})
3772+ @pytest .mark .usefixtures ("netrc_default_contents" )
3773+ async def test_netrc_auth_from_env ( # type: ignore[misc]
3774+ headers_echo_client : Callable [
3775+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3776+ ],
3777+ ) -> None :
3778+ """Test that netrc authentication works when NETRC env var is set and trust_env=True."""
3779+ client = await headers_echo_client (trust_env = True )
3780+ async with client .get ("/" ) as r :
3781+ assert r .status == 200
3782+ content = await r .json ()
3783+ # Base64 encoded "netrc_user:netrc_pass" is "bmV0cmNfdXNlcjpuZXRyY19wYXNz"
3784+ assert content ["headers" ]["Authorization" ] == "Basic bmV0cmNfdXNlcjpuZXRyY19wYXNz"
37623785
3763- app = web .Application ()
3764- app .router .add_get ("/" , handler )
37653786
3766- client = await aiohttp_client (app , headers = {"X-Real-IP" : "192.168.0.1" })
3787+ @pytest .mark .usefixtures ("no_netrc" )
3788+ async def test_netrc_auth_skipped_without_env_var ( # type: ignore[misc]
3789+ headers_echo_client : Callable [
3790+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3791+ ],
3792+ ) -> None :
3793+ """Test that netrc authentication is skipped when NETRC env var is not set."""
3794+ client = await headers_echo_client (trust_env = True )
3795+ async with client .get ("/" ) as r :
3796+ assert r .status == 200
3797+ content = await r .json ()
3798+ # No Authorization header should be present
3799+ assert "Authorization" not in content ["headers" ]
3800+
3801+
3802+ @pytest .mark .usefixtures ("netrc_default_contents" )
3803+ async def test_netrc_auth_overridden_by_explicit_auth ( # type: ignore[misc]
3804+ headers_echo_client : Callable [
3805+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3806+ ],
3807+ ) -> None :
3808+ """Test that explicit auth parameter overrides netrc authentication."""
3809+ client = await headers_echo_client (trust_env = True )
3810+ # Make request with explicit auth (should override netrc)
3811+ async with client .get (
3812+ "/" , auth = aiohttp .BasicAuth ("explicit_user" , "explicit_pass" )
3813+ ) as r :
3814+ assert r .status == 200
3815+ content = await r .json ()
3816+ # Base64 encoded "explicit_user:explicit_pass" is "ZXhwbGljaXRfdXNlcjpleHBsaWNpdF9wYXNz"
3817+ assert (
3818+ content ["headers" ]["Authorization" ]
3819+ == "Basic ZXhwbGljaXRfdXNlcjpleHBsaWNpdF9wYXNz"
3820+ )
3821+
3822+
3823+ async def test_session_headers (
3824+ headers_echo_client : Callable [
3825+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3826+ ],
3827+ ) -> None :
3828+ client = await headers_echo_client (headers = {"X-Real-IP" : "192.168.0.1" })
37673829
37683830 r = await client .get ("/" )
37693831 assert r .status == 200
37703832 content = await r .json ()
37713833 assert content ["headers" ]["X-Real-IP" ] == "192.168.0.1"
37723834
37733835
3774- async def test_session_headers_merge (aiohttp_client ) -> None :
3775- async def handler (request ):
3776- return web .json_response ({"headers" : dict (request .headers )})
3777-
3778- app = web .Application ()
3779- app .router .add_get ("/" , handler )
3780-
3781- client = await aiohttp_client (
3782- app , headers = [("X-Real-IP" , "192.168.0.1" ), ("X-Sent-By" , "requests" )]
3836+ async def test_session_headers_merge (
3837+ headers_echo_client : Callable [
3838+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3839+ ],
3840+ ) -> None :
3841+ client = await headers_echo_client (
3842+ headers = [("X-Real-IP" , "192.168.0.1" ), ("X-Sent-By" , "requests" )]
37833843 )
37843844
37853845 r = await client .get ("/" , headers = {"X-Sent-By" : "aiohttp" })
0 commit comments