diff --git a/playwright/_impl/_network.py b/playwright/_impl/_network.py index 3e5b8fe89..229883657 100644 --- a/playwright/_impl/_network.py +++ b/playwright/_impl/_network.py @@ -18,7 +18,6 @@ import json as json_utils import mimetypes import re -from collections import defaultdict from pathlib import Path from types import SimpleNamespace from typing import ( @@ -1031,9 +1030,10 @@ def _on_close(self) -> None: class RawHeaders: def __init__(self, headers: HeadersArray) -> None: self._headers_array = headers - self._headers_map: Dict[str, Dict[str, bool]] = defaultdict(dict) + self._headers_map: Dict[str, Dict[str, bool]] = {} for header in headers: - self._headers_map[header["name"].lower()][header["value"]] = True + values = self._headers_map.setdefault(header["name"].lower(), {}) + values[header["value"]] = True @staticmethod def _from_headers_dict_lossy(headers: Dict[str, str]) -> "RawHeaders": @@ -1047,7 +1047,7 @@ def get(self, name: str) -> Optional[str]: return separator.join(values) def get_all(self, name: str) -> List[str]: - return list(self._headers_map[name.lower()].keys()) + return list(self._headers_map.get(name.lower(), {}).keys()) def headers(self) -> Dict[str, str]: result = {} diff --git a/tests/async/test_network.py b/tests/async/test_network.py index e88b24c24..b03490ef6 100644 --- a/tests/async/test_network.py +++ b/tests/async/test_network.py @@ -369,6 +369,9 @@ async def test_response_headers_should_work(page: Page, server: Server) -> None: assert response assert response.headers["foo"] == "bar" assert (await response.all_headers())["foo"] == "bar" + # Looking up a missing header must not add it. + assert await response.header_value("x-missing") is None + assert "x-missing" not in await response.all_headers() async def test_request_post_data_should_work(page: Page, server: Server) -> None: