diff --git a/httpx/dispatch/http2.py b/httpx/dispatch/http2.py
index 786110c..fd40964 100644
--- a/httpx/dispatch/http2.py
+++ b/httpx/dispatch/http2.py
@@ -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
diff --git a/httpx/middleware.py b/httpx/middleware.py
index dd23ac2..c98c85e 100644
--- a/httpx/middleware.py
+++ b/httpx/middleware.py
@@ -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:
diff --git a/httpx/models.py b/httpx/models.py
index 59f543f..d0bbc41 100644
--- a/httpx/models.py
+++ b/httpx/models.py
@@ -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:

Deletion of non-existent keys from `Headers` should raise KeyError.
Prompted by  #310

To replicate:

```python
headers = httpx.Headers()
del headers['DOES_NOT_EXIST']  # Should raise a KeyError, but doesn't
```

To resolve this issue we'll need to:

* Ensure that deletion of non-existant keys raises a KeyError.
* Ensure that we have a `.pop(key, default)` that we can use instead for removal without KeyErrors.
* Replace instances of `del headers[key]` in our codebase with `headers.pop(key, None)` where appropriate.

diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py
index 2d17c12..c5429a8 100755
--- a/tests/client/test_headers.py
+++ b/tests/client/test_headers.py
@@ -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"]

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
3.7
test-requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==24.2.0
brotlipy==0.7.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
cffi==1.15.1
chardet==3.0.4
click==8.1.8
coverage==7.2.7
cryptography==44.0.2
exceptiongroup==1.2.2
flake8==5.0.4
flake8-bugbear==23.3.12
flake8-comprehensions==3.13.0
h11==0.8.1
h2==3.2.0
hpack==3.0.0
hstspreload==2025.1.1
-e git+https://github.com/encode/httpx.git@6b97b2ed00a03d7528abcb45d59e111bebdf0c1a#egg=httpx
hyperframe==5.2.0
idna==2.10
importlib-metadata==4.2.0
iniconfig==2.0.0
isort==5.11.5
mccabe==0.7.0
mypy==1.4.1
mypy-extensions==1.0.0
packaging==24.0
pluggy==1.2.0
pycodestyle==2.9.1
pycparser==2.21
pyflakes==2.5.0
pytest==7.4.4
pytest-asyncio==0.21.2
pytest-cov==4.1.0
rfc3986==1.5.0
tomli==2.0.1
trustme==1.0.0
typed-ast==1.5.5
typing_extensions==4.7.1
uvicorn==0.22.0
uvloop==0.12.2
zipp==3.15.0

name: httpx
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==24.2.0
      - brotlipy==0.7.0
      - cffi==1.15.1
      - chardet==3.0.4
      - click==8.1.8
      - coverage==7.2.7
      - cryptography==44.0.2
      - exceptiongroup==1.2.2
      - flake8==5.0.4
      - flake8-bugbear==23.3.12
      - flake8-comprehensions==3.13.0
      - h11==0.8.1
      - h2==3.2.0
      - hpack==3.0.0
      - hstspreload==2025.1.1
      - hyperframe==5.2.0
      - idna==2.10
      - importlib-metadata==4.2.0
      - iniconfig==2.0.0
      - isort==5.11.5
      - mccabe==0.7.0
      - mypy==1.4.1
      - mypy-extensions==1.0.0
      - packaging==24.0
      - pluggy==1.2.0
      - pycodestyle==2.9.1
      - pycparser==2.21
      - pyflakes==2.5.0
      - pytest==7.4.4
      - pytest-asyncio==0.21.2
      - pytest-cov==4.1.0
      - rfc3986==1.5.0
      - tomli==2.0.1
      - trustme==1.0.0
      - typed-ast==1.5.5
      - typing-extensions==4.7.1
      - uvicorn==0.22.0
      - uvloop==0.12.2
      - zipp==3.15.0
prefix: /opt/conda/envs/httpx

tests/client/test_headers.py::test_header_does_not_exist
tests/client/test_headers.py::test_client_header tests/client/test_headers.py::test_header_merge tests/client/test_headers.py::test_header_merge_conflicting_headers tests/client/test_headers.py::test_header_update
httpx/dispatch/http2.py httpx/middleware.py httpx/models.py
httpx/dispatch/http2.py httpx/middleware.py httpx/models.py