Skip to content

Commit

Permalink
Set content-type header in TestSimulator
Browse files Browse the repository at this point in the history
Abapt set_headers_for_response_content from scribe module to use it for TestSimulator
  • Loading branch information
tyzhnenko committed Jun 24, 2024
1 parent 026897f commit 8218f3e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions blacksheep/testing/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ def _create_scope(
)


def should_use_chunked_encoding(content: Content) -> bool:
return content.length < 0


def set_headers_for_response_content(message: Response):
content = message.content

if not content:
message.add_header(b"content-length", b"0")
return

message.add_header(b"content-type", content.type or b"application/octet-stream")

if should_use_chunked_encoding(content):
message.add_header(b"transfer-encoding", b"chunked")
else:
message.add_header(b"content-length", str(content.length).encode())


class AbstractTestSimulator:
"""An abstract class for custom Test simulator clients"""

Expand Down Expand Up @@ -87,6 +106,7 @@ async def send_request(
request.content = content

response = await self.app.handle(request)
set_headers_for_response_content(response)

return response

Expand Down

0 comments on commit 8218f3e

Please sign in to comment.