Skip to content

Commit 1de5d37

Browse files
authored
fix(network): do not add missing headers when looking them up (#3246)
1 parent 222a2e0 commit 1de5d37

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎playwright/_impl/_network.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import json as json_utils
1919
import mimetypes
2020
import re
21-
from collections import defaultdict
2221
from pathlib import Path
2322
from types import SimpleNamespace
2423
from typing import (
@@ -1031,9 +1030,10 @@ def _on_close(self) -> None:
10311030
class RawHeaders:
10321031
def __init__(self, headers: HeadersArray) -> None:
10331032
self._headers_array = headers
1034-
self._headers_map: Dict[str, Dict[str, bool]] = defaultdict(dict)
1033+
self._headers_map: Dict[str, Dict[str, bool]] = {}
10351034
for header in headers:
1036-
self._headers_map[header["name"].lower()][header["value"]] = True
1035+
values = self._headers_map.setdefault(header["name"].lower(), {})
1036+
values[header["value"]] = True
10371037

10381038
@staticmethod
10391039
def _from_headers_dict_lossy(headers: Dict[str, str]) -> "RawHeaders":
@@ -1047,7 +1047,7 @@ def get(self, name: str) -> Optional[str]:
10471047
return separator.join(values)
10481048

10491049
def get_all(self, name: str) -> List[str]:
1050-
return list(self._headers_map[name.lower()].keys())
1050+
return list(self._headers_map.get(name.lower(), {}).keys())
10511051

10521052
def headers(self) -> Dict[str, str]:
10531053
result = {}

‎tests/async/test_network.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ async def test_response_headers_should_work(page: Page, server: Server) -> None:
369369
assert response
370370
assert response.headers["foo"] == "bar"
371371
assert (await response.all_headers())["foo"] == "bar"
372+
# Looking up a missing header must not add it.
373+
assert await response.header_value("x-missing") is None
374+
assert "x-missing" not in await response.all_headers()
372375

373376

374377
async def test_request_post_data_should_work(page: Page, server: Server) -> None:

0 commit comments

Comments
 (0)