Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set content-type header in TestSimulator #502

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading