WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 64d27f7

Browse files
authored
fix str(request.headers) (#2986)
2 parents d1f60d6 + afc4ea7 commit 64d27f7

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Version 3.1.1
55

66
Unreleased
77

8+
- Fix an issue that caused ``str(Request.headers)`` to always appear empty.
9+
:issue:`2985`
10+
811

912
Version 3.1.0
1013
-------------

src/werkzeug/datastructures/headers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,13 @@ def __copy__(self) -> te.Self:
568568
def __str__(self) -> str:
569569
"""Returns formatted headers suitable for HTTP transmission."""
570570
strs = []
571-
for key, value in self._list:
571+
for key, value in self.to_wsgi_list():
572572
strs.append(f"{key}: {value}")
573573
strs.append("\r\n")
574574
return "\r\n".join(strs)
575575

576576
def __repr__(self) -> str:
577-
return f"{type(self).__name__}({self._list!r})"
577+
return f"{type(self).__name__}({list(self)!r})"
578578

579579

580580
def _options_header_vkw(value: str, kw: dict[str, t.Any]) -> str:

tests/test_datastructures.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,10 @@ def test_ior(self) -> None:
885885
with pytest.raises(TypeError):
886886
headers |= {"y": "2"}
887887

888+
def test_str(self) -> None:
889+
headers = ds.EnvironHeaders({"CONTENT_LENGTH": "50", "HTTP_HOST": "test"})
890+
assert str(headers) == "Content-Length: 50\r\nHost: test\r\n\r\n"
891+
888892

889893
class TestHeaderSet:
890894
storage_class = ds.HeaderSet

0 commit comments

Comments
 (0)