Skip to content

Commit ad2fc7f

Browse files
committed
fix: Response content-length header needs to be recalculated if the response body is mutated in a post hook.
1 parent e34e5d5 commit ad2fc7f

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

src/starlette_problem/handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __call__(self: t.Self, request: Request, exc: Exception) -> Response:
106106

107107
for post_hook in self.post_hooks:
108108
content, response = post_hook(content, request, response)
109+
response.headers["content-length"] = str(len(response.body))
109110

110111
return response
111112

tests/test_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def test_strip_extras_post_hook_disabled(self):
116116
response.body
117117
== b'{"type":"something-wrong","title":"This is an error.","status":500,"a":"b","detail":"something bad"}'
118118
)
119+
assert response.headers["content-length"] == "100"
119120

120121
def test_strip_extras_post_hook_enabled(self):
121122
request = mock.Mock(headers={})
@@ -130,6 +131,7 @@ def test_strip_extras_post_hook_enabled(self):
130131
response.body
131132
== b'{"type":"something-wrong","title":"This is an error.","status":500,"detail":"something bad"}'
132133
)
134+
assert response.headers["content-length"] == "92"
133135

134136
def test_strip_extras_post_hook_exclude_status_code(self):
135137
request = mock.Mock(headers={})

0 commit comments

Comments
 (0)