|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from collections import deque |
| 5 | +from copy import copy |
| 6 | + |
| 7 | +from smithy_core.aio.utils import async_list |
| 8 | +from smithy_http import tuples_to_fields |
| 9 | +from smithy_http.aio import HTTPResponse |
| 10 | +from smithy_http.aio.interfaces import HTTPClient, HTTPRequest |
| 11 | +from smithy_http.interfaces import HTTPClientConfiguration, HTTPRequestConfiguration |
| 12 | + |
| 13 | + |
| 14 | +class MockHTTPClient(HTTPClient): |
| 15 | + """Implementation of :py:class:`.interfaces.HTTPClient` solely for testing purposes. |
| 16 | +
|
| 17 | + Simulates HTTP request/response behavior. |
| 18 | + Responses are queued in FIFO order and requests are captured for inspection. |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + *, |
| 24 | + client_config: HTTPClientConfiguration | None = None, |
| 25 | + ) -> None: |
| 26 | + """ |
| 27 | + :param client_config: Configuration that applies to all requests made with this |
| 28 | + client. |
| 29 | + """ |
| 30 | + self._client_config = client_config |
| 31 | + self._response_queue: deque[HTTPResponse] = deque() |
| 32 | + self._captured_requests: list[HTTPRequest] = [] |
| 33 | + |
| 34 | + def add_response( |
| 35 | + self, |
| 36 | + status: int = 200, |
| 37 | + headers: list[tuple[str, str]] | None = None, |
| 38 | + body: bytes = b"", |
| 39 | + ) -> None: |
| 40 | + """Queue a response for the next request. |
| 41 | +
|
| 42 | + :param status: HTTP status code (200, 404, 500, etc.) |
| 43 | + :param headers: HTTP response headers as list of (name, value) tuples |
| 44 | + :param body: Response body as bytes |
| 45 | + """ |
| 46 | + response = HTTPResponse( |
| 47 | + status=status, |
| 48 | + fields=tuples_to_fields(headers or []), |
| 49 | + body=async_list([body]), |
| 50 | + reason=None, |
| 51 | + ) |
| 52 | + self._response_queue.append(response) |
| 53 | + |
| 54 | + async def send( |
| 55 | + self, |
| 56 | + request: HTTPRequest, |
| 57 | + *, |
| 58 | + request_config: HTTPRequestConfiguration | None = None, |
| 59 | + ) -> HTTPResponse: |
| 60 | + """Send HTTP request and return configured response. |
| 61 | +
|
| 62 | + :param request: The request including destination URI, fields, payload. |
| 63 | + :param request_config: Configuration specific to this request. |
| 64 | + :returns: Pre-configured HTTP response from the queue. |
| 65 | + :raises MockHTTPClientError: If no responses are queued. |
| 66 | + """ |
| 67 | + self._captured_requests.append(copy(request)) |
| 68 | + |
| 69 | + # Return next queued response or raise error |
| 70 | + if self._response_queue: |
| 71 | + return self._response_queue.popleft() |
| 72 | + else: |
| 73 | + raise MockHTTPClientError( |
| 74 | + "No responses queued in MockHTTPClient. Use add_response() to queue responses." |
| 75 | + ) |
| 76 | + |
| 77 | + @property |
| 78 | + def call_count(self) -> int: |
| 79 | + """The number of requests made to this client.""" |
| 80 | + return len(self._captured_requests) |
| 81 | + |
| 82 | + @property |
| 83 | + def captured_requests(self) -> list[HTTPRequest]: |
| 84 | + """The list of all requests captured by this client.""" |
| 85 | + return self._captured_requests.copy() |
| 86 | + |
| 87 | + |
| 88 | +class MockHTTPClientError(Exception): |
| 89 | + """Exception raised by MockHTTPClient for test setup issues.""" |
0 commit comments