From: Tom Christie Date: Sat, 30 Nov 2019 12:06:16 +0000 (+0000) Subject: Ensure Authorization header has priority over .netrc X-Git-Tag: 0.9.0~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=43331cfb3d3d97c9886125b821a04891951eea81;p=thirdparty%2Fhttpx.git Ensure Authorization header has priority over .netrc --- diff --git a/httpx/client.py b/httpx/client.py index 4c594f2b..1fe4f6c0 100644 --- a/httpx/client.py +++ b/httpx/client.py @@ -384,7 +384,7 @@ class Client: auth = BasicAuth(username=username, password=password) return auth(request) - if trust_env: + if trust_env and "Authorization" not in request.headers: credentials = self.netrc.get_credentials(request.url.authority) if credentials is not None: auth = BasicAuth(username=credentials[0], password=credentials[1]) diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index 58999cff..d81ddb11 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -156,6 +156,18 @@ async def test_netrc_auth(): } +@pytest.mark.asyncio +async def test_auth_header_has_priority_over_netrc(): + os.environ["NETRC"] = "tests/.netrc" + url = "http://netrcexample.org" + + client = Client(dispatch=MockDispatch()) + response = await client.get(url, headers={"Authorization": "Override"}) + + assert response.status_code == 200 + assert response.json() == {"auth": "Override"} + + @pytest.mark.asyncio async def test_trust_env_auth(): os.environ["NETRC"] = "tests/.netrc"