@@ -70,6 +70,23 @@ def fname(here: pathlib.Path) -> pathlib.Path:
7070 return here / "conftest.py"
7171
7272
73+ @pytest .fixture
74+ def headers_echo_client (
75+ aiohttp_client : AiohttpClient ,
76+ ) -> Callable [..., Awaitable [TestClient [web .Request , web .Application ]]]:
77+ """Create a client with an app that echoes request headers as JSON."""
78+
79+ async def factory (** kwargs : Any ) -> TestClient [web .Request , web .Application ]:
80+ async def handler (request : web .Request ) -> web .Response :
81+ return web .json_response ({"headers" : dict (request .headers )})
82+
83+ app = web .Application ()
84+ app .router .add_get ("/" , handler )
85+ return await aiohttp_client (app , ** kwargs )
86+
87+ return factory
88+
89+
7390async def test_keepalive_two_requests_success (aiohttp_client : AiohttpClient ) -> None :
7491 async def handler (request : web .Request ) -> web .Response :
7592 body = await request .read ()
@@ -3702,29 +3719,25 @@ async def handler(request: web.Request) -> web.Response:
37023719 assert resp .status == 200
37033720
37043721
3705- async def test_session_auth (aiohttp_client : AiohttpClient ) -> None :
3706- async def handler (request : web .Request ) -> web .Response :
3707- return web .json_response ({"headers" : dict (request .headers )})
3708-
3709- app = web .Application ()
3710- app .router .add_get ("/" , handler )
3711-
3712- client = await aiohttp_client (app , auth = aiohttp .BasicAuth ("login" , "pass" ))
3722+ async def test_session_auth (
3723+ headers_echo_client : Callable [
3724+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3725+ ],
3726+ ) -> None :
3727+ client = await headers_echo_client (auth = aiohttp .BasicAuth ("login" , "pass" ))
37133728
37143729 async with client .get ("/" ) as r :
37153730 assert r .status == 200
37163731 content = await r .json ()
37173732 assert content ["headers" ]["Authorization" ] == "Basic bG9naW46cGFzcw=="
37183733
37193734
3720- async def test_session_auth_override (aiohttp_client : AiohttpClient ) -> None :
3721- async def handler (request : web .Request ) -> web .Response :
3722- return web .json_response ({"headers" : dict (request .headers )})
3723-
3724- app = web .Application ()
3725- app .router .add_get ("/" , handler )
3726-
3727- client = await aiohttp_client (app , auth = aiohttp .BasicAuth ("login" , "pass" ))
3735+ async def test_session_auth_override (
3736+ headers_echo_client : Callable [
3737+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3738+ ],
3739+ ) -> None :
3740+ client = await headers_echo_client (auth = aiohttp .BasicAuth ("login" , "pass" ))
37283741
37293742 async with client .get ("/" , auth = aiohttp .BasicAuth ("other_login" , "pass" )) as r :
37303743 assert r .status == 200
@@ -3746,30 +3759,77 @@ async def handler(request: web.Request) -> NoReturn:
37463759 await client .get ("/" , headers = headers )
37473760
37483761
3749- async def test_session_headers (aiohttp_client : AiohttpClient ) -> None :
3750- async def handler (request : web .Request ) -> web .Response :
3751- return web .json_response ({"headers" : dict (request .headers )})
3752-
3753- app = web .Application ()
3754- app .router .add_get ("/" , handler )
3762+ @pytest .mark .usefixtures ("netrc_default_contents" )
3763+ async def test_netrc_auth_from_env ( # type: ignore[misc]
3764+ headers_echo_client : Callable [
3765+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3766+ ],
3767+ ) -> None :
3768+ """Test that netrc authentication works when NETRC env var is set and trust_env=True."""
3769+ client = await headers_echo_client (trust_env = True )
3770+ async with client .get ("/" ) as r :
3771+ assert r .status == 200
3772+ content = await r .json ()
3773+ # Base64 encoded "netrc_user:netrc_pass" is "bmV0cmNfdXNlcjpuZXRyY19wYXNz"
3774+ assert content ["headers" ]["Authorization" ] == "Basic bmV0cmNfdXNlcjpuZXRyY19wYXNz"
37553775
3756- client = await aiohttp_client (app , headers = {"X-Real-IP" : "192.168.0.1" })
37573776
3777+ @pytest .mark .usefixtures ("no_netrc" )
3778+ async def test_netrc_auth_skipped_without_env_var ( # type: ignore[misc]
3779+ headers_echo_client : Callable [
3780+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3781+ ],
3782+ ) -> None :
3783+ """Test that netrc authentication is skipped when NETRC env var is not set."""
3784+ client = await headers_echo_client (trust_env = True )
37583785 async with client .get ("/" ) as r :
37593786 assert r .status == 200
37603787 content = await r .json ()
3761- assert content ["headers" ]["X-Real-IP" ] == "192.168.0.1"
3788+ # No Authorization header should be present
3789+ assert "Authorization" not in content ["headers" ]
37623790
37633791
3764- async def test_session_headers_merge (aiohttp_client : AiohttpClient ) -> None :
3765- async def handler (request : web .Request ) -> web .Response :
3766- return web .json_response ({"headers" : dict (request .headers )})
3792+ @pytest .mark .usefixtures ("netrc_default_contents" )
3793+ async def test_netrc_auth_overridden_by_explicit_auth ( # type: ignore[misc]
3794+ headers_echo_client : Callable [
3795+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3796+ ],
3797+ ) -> None :
3798+ """Test that explicit auth parameter overrides netrc authentication."""
3799+ client = await headers_echo_client (trust_env = True )
3800+ # Make request with explicit auth (should override netrc)
3801+ async with client .get (
3802+ "/" , auth = aiohttp .BasicAuth ("explicit_user" , "explicit_pass" )
3803+ ) as r :
3804+ assert r .status == 200
3805+ content = await r .json ()
3806+ # Base64 encoded "explicit_user:explicit_pass" is "ZXhwbGljaXRfdXNlcjpleHBsaWNpdF9wYXNz"
3807+ assert (
3808+ content ["headers" ]["Authorization" ]
3809+ == "Basic ZXhwbGljaXRfdXNlcjpleHBsaWNpdF9wYXNz"
3810+ )
37673811
3768- app = web .Application ()
3769- app .router .add_get ("/" , handler )
37703812
3771- client = await aiohttp_client (
3772- app , headers = [("X-Real-IP" , "192.168.0.1" ), ("X-Sent-By" , "requests" )]
3813+ async def test_session_headers (
3814+ headers_echo_client : Callable [
3815+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3816+ ],
3817+ ) -> None :
3818+ client = await headers_echo_client (headers = {"X-Real-IP" : "192.168.0.1" })
3819+
3820+ async with client .get ("/" ) as r :
3821+ assert r .status == 200
3822+ content = await r .json ()
3823+ assert content ["headers" ]["X-Real-IP" ] == "192.168.0.1"
3824+
3825+
3826+ async def test_session_headers_merge (
3827+ headers_echo_client : Callable [
3828+ ..., Awaitable [TestClient [web .Request , web .Application ]]
3829+ ],
3830+ ) -> None :
3831+ client = await headers_echo_client (
3832+ headers = [("X-Real-IP" , "192.168.0.1" ), ("X-Sent-By" , "requests" )]
37733833 )
37743834
37753835 async with client .get ("/" , headers = {"X-Sent-By" : "aiohttp" }) as r :
0 commit comments