]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Ensure Authorization header has priority over .netrc
authorTom Christie <tom@tomchristie.com>
Sat, 30 Nov 2019 12:06:16 +0000 (12:06 +0000)
committerTom Christie <tom@tomchristie.com>
Sat, 30 Nov 2019 12:06:16 +0000 (12:06 +0000)
httpx/client.py
tests/client/test_auth.py

index 4c594f2b09cc2f81f1e14e56f6922324e62c6f7c..1fe4f6c0143ee73b1d92e1b00867315397a14a44 100644 (file)
@@ -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])
index 58999cff7252f60b50ba9f3c28da550cc907a12e..d81ddb11a046db9b9fd3192186b3ea5c55cc7fda 100644 (file)
@@ -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"