From 304e0b46b87414eb78571861fff64f9fcfb38d1e Mon Sep 17 00:00:00 2001 From: akshara08 Date: Tue, 18 Apr 2023 14:10:00 -0700 Subject: [PATCH] Adds raw_header to MockRawResponse --- moto/core/botocore_stubber.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/moto/core/botocore_stubber.py b/moto/core/botocore_stubber.py index 7c41e6dfadf5..af42d6495235 100644 --- a/moto/core/botocore_stubber.py +++ b/moto/core/botocore_stubber.py @@ -7,7 +7,8 @@ class MockRawResponse(BytesIO): - def __init__(self, response_input: Union[str, bytes]): + def __init__(self, response_input: Union[str, bytes], headers: Dict[Any, Any]): + self.headers = headers if isinstance(response_input, str): response_input = response_input.encode("utf-8") super().__init__(response_input) @@ -18,6 +19,17 @@ def stream(self, **kwargs: Any) -> Any: # pylint: disable=unused-argument yield contents contents = self.read() + @property + def raw_headers(self) -> List[Tuple[bytes, bytes]]: + encoded_headers = [ + ( + str(header).encode(encoding="utf-8"), + str(value).encode(encoding="utf-8"), + ) + for header, value in self.headers.items() + ] + return encoded_headers + class BotocoreStubber: def __init__(self) -> None: @@ -66,7 +78,8 @@ def __call__(self, event_name: str, request: Any, **kwargs: Any) -> AWSResponse: status = e.code # type: ignore[assignment] headers = e.get_headers() # type: ignore[assignment] body = e.get_body() - raw_response = MockRawResponse(body) + + raw_response = MockRawResponse(response_input=body, headers=dict(headers)) response = AWSResponse(request.url, status, headers, raw_response) return response