Skip to content

Commit

Permalink
fix str(request.headers)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Nov 1, 2024
1 parent d1f60d6 commit afc4ea7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
4 changes: 2 additions & 2 deletions src/werkzeug/datastructures/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit afc4ea7

Please sign in to comment.