]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Raise KeyError if header isn't found in Headers (#324)
authorGaurav Dhameeja <gdhameeja@gmail.com>
Sat, 7 Sep 2019 21:12:37 +0000 (02:42 +0530)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Sat, 7 Sep 2019 21:12:37 +0000 (16:12 -0500)
httpx/dispatch/http2.py
httpx/middleware.py
httpx/models.py
tests/client/test_headers.py

index 786110c92044a631d057921930790d59c1c93e0b..fd40964d265432e26762d18fe0945d8d5cc2d0c7 100644 (file)
@@ -4,7 +4,7 @@ import typing
 import h2.connection
 import h2.events
 
-from ..concurrency.base import BaseStream, ConcurrencyBackend, TimeoutFlag, BaseEvent
+from ..concurrency.base import BaseEvent, BaseStream, ConcurrencyBackend, TimeoutFlag
 from ..config import TimeoutConfig, TimeoutTypes
 from ..models import AsyncRequest, AsyncResponse
 from ..utils import get_logger
index dd23ac2a72d8c9608a285b1d137e239775177551..c98c85e573f0a70f4d348817c36b464a396a61c3 100644 (file)
@@ -147,15 +147,14 @@ class RedirectMiddleware(BaseMiddleware):
         if url.origin != request.url.origin:
             # Strip Authorization headers when responses are redirected away from
             # the origin.
-            del headers["Authorization"]
+            headers.pop("Authorization", None)
             headers["Host"] = url.authority
 
         if method != request.method and method == "GET":
             # If we've switch to a 'GET' request, then strip any headers which
             # are only relevant to the request body.
-            del headers["Content-Length"]
-            del headers["Transfer-Encoding"]
-
+            headers.pop("Content-Length", None)
+            headers.pop("Transfer-Encoding", None)
         return headers
 
     def redirect_content(self, request: AsyncRequest, method: str) -> bytes:
index 59f543fd40470b2fee1bd540af194c94d07cc4af..d0bbc4180cb8127ec5087e808cf49a024f694df5 100644 (file)
@@ -473,6 +473,8 @@ class Headers(typing.MutableMapping[str, str]):
         for idx, (item_key, _) in enumerate(self._list):
             if item_key == del_key:
                 pop_indexes.append(idx)
+        if not pop_indexes:
+            raise KeyError(key)
 
         for idx in reversed(pop_indexes):
             del self._list[idx]
@@ -624,7 +626,6 @@ class AsyncRequest(BaseRequest):
             assert hasattr(data, "__aiter__")
             self.is_streaming = True
             self.content_aiter = data
-
         self.prepare()
 
     async def read(self) -> bytes:
index 2d17c1259e5aa270da010319037c75d84c64d576..c5429a8713d8f20d214dbcb34fd2f444d222e020 100755 (executable)
@@ -2,6 +2,8 @@
 
 import json
 
+import pytest
+
 from httpx import (
     AsyncDispatcher,
     AsyncRequest,
@@ -11,6 +13,7 @@ from httpx import (
     TimeoutTypes,
     VerifyTypes,
     __version__,
+    models,
 )
 
 
@@ -122,3 +125,9 @@ def test_header_update():
             "user-agent": "python-myclient/0.2.1",
         }
     }
+
+
+def test_header_does_not_exist():
+    headers = models.Headers({"foo": "bar"})
+    with pytest.raises(KeyError):
+        del headers["baz"]