From 8218f3eca4aa33a91282d0f968bb1e4ee8e21954 Mon Sep 17 00:00:00 2001 From: Dmytro Tyzhnenko Date: Tue, 25 Jun 2024 01:31:08 +0300 Subject: [PATCH] Set content-type header in TestSimulator Abapt set_headers_for_response_content from scribe module to use it for TestSimulator --- blacksheep/testing/simulator.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/blacksheep/testing/simulator.py b/blacksheep/testing/simulator.py index 32506197..ea7b4251 100644 --- a/blacksheep/testing/simulator.py +++ b/blacksheep/testing/simulator.py @@ -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""" @@ -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