From afc4ea7b5e969d501194d8acdc88025f877a5510 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 1 Nov 2024 08:21:24 -0700 Subject: [PATCH] fix str(request.headers) --- CHANGES.rst | 3 +++ src/werkzeug/datastructures/headers.py | 4 ++-- tests/test_datastructures.py | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d4c040e2d..1f2938695 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,9 @@ Version 3.1.1 Unreleased +- Fix an issue that caused ``str(Request.headers)`` to always appear empty. + :issue:`2985` + Version 3.1.0 ------------- diff --git a/src/werkzeug/datastructures/headers.py b/src/werkzeug/datastructures/headers.py index db53cda7b..8b274c75f 100644 --- a/src/werkzeug/datastructures/headers.py +++ b/src/werkzeug/datastructures/headers.py @@ -568,13 +568,13 @@ def __copy__(self) -> te.Self: def __str__(self) -> str: """Returns formatted headers suitable for HTTP transmission.""" strs = [] - for key, value in self._list: + for key, value in self.to_wsgi_list(): strs.append(f"{key}: {value}") strs.append("\r\n") return "\r\n".join(strs) def __repr__(self) -> str: - return f"{type(self).__name__}({self._list!r})" + return f"{type(self).__name__}({list(self)!r})" def _options_header_vkw(value: str, kw: dict[str, t.Any]) -> str: diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index 51799722b..dcbc79697 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -885,6 +885,10 @@ def test_ior(self) -> None: with pytest.raises(TypeError): headers |= {"y": "2"} + def test_str(self) -> None: + headers = ds.EnvironHeaders({"CONTENT_LENGTH": "50", "HTTP_HOST": "test"}) + assert str(headers) == "Content-Length: 50\r\nHost: test\r\n\r\n" + class TestHeaderSet: storage_class = ds.HeaderSet