|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import pytest |
| 5 | +from smithy_http.testing import MockHTTPClient, MockHTTPClientError, create_test_request |
| 6 | + |
| 7 | + |
| 8 | +async def test_default_response(): |
| 9 | + # Test error when no responses are queued |
| 10 | + mock_client = MockHTTPClient() |
| 11 | + request = create_test_request() |
| 12 | + |
| 13 | + with pytest.raises(MockHTTPClientError, match="No responses queued"): |
| 14 | + await mock_client.send(request) |
| 15 | + |
| 16 | + |
| 17 | +async def test_queued_responses_fifo(): |
| 18 | + # Test responses are returned in FIFO order |
| 19 | + mock_client = MockHTTPClient() |
| 20 | + mock_client.add_response(status=404, body=b"not found") |
| 21 | + mock_client.add_response(status=500, body=b"server error") |
| 22 | + |
| 23 | + request = create_test_request() |
| 24 | + |
| 25 | + response1 = await mock_client.send(request) |
| 26 | + assert response1.status == 404 |
| 27 | + assert await response1.consume_body_async() == b"not found" |
| 28 | + |
| 29 | + response2 = await mock_client.send(request) |
| 30 | + assert response2.status == 500 |
| 31 | + assert await response2.consume_body_async() == b"server error" |
| 32 | + |
| 33 | + assert mock_client.call_count == 2 |
| 34 | + |
| 35 | + |
| 36 | +async def test_captured_requests(): |
| 37 | + # Test all requests are captured for inspection |
| 38 | + mock_client = MockHTTPClient() |
| 39 | + mock_client.add_response() |
| 40 | + mock_client.add_response() |
| 41 | + |
| 42 | + request1 = create_test_request( |
| 43 | + method="GET", |
| 44 | + host="test.aws.dev", |
| 45 | + ) |
| 46 | + request2 = create_test_request( |
| 47 | + method="POST", |
| 48 | + host="test.aws.dev", |
| 49 | + body=b'{"name": "test"}', |
| 50 | + ) |
| 51 | + |
| 52 | + await mock_client.send(request1) |
| 53 | + await mock_client.send(request2) |
| 54 | + |
| 55 | + captured = mock_client.captured_requests |
| 56 | + assert len(captured) == 2 |
| 57 | + assert captured[0].method == "GET" |
| 58 | + assert captured[1].method == "POST" |
| 59 | + assert captured[1].body == b'{"name": "test"}' |
| 60 | + |
| 61 | + |
| 62 | +async def test_response_headers(): |
| 63 | + # Test response headers are properly set |
| 64 | + mock_client = MockHTTPClient() |
| 65 | + mock_client.add_response( |
| 66 | + status=201, |
| 67 | + headers=[ |
| 68 | + ("Content-Type", "application/json"), |
| 69 | + ("X-Amz-Custom", "test"), |
| 70 | + ], |
| 71 | + body=b'{"id": 123}', |
| 72 | + ) |
| 73 | + request = create_test_request() |
| 74 | + response = await mock_client.send(request) |
| 75 | + |
| 76 | + assert response.status == 201 |
| 77 | + assert "Content-Type" in response.fields |
| 78 | + assert response.fields["Content-Type"].as_string() == "application/json" |
| 79 | + assert "X-Amz-Custom" in response.fields |
| 80 | + assert response.fields["X-Amz-Custom"].as_string() == "test" |
| 81 | + |
| 82 | + |
| 83 | +async def test_call_count_tracking(): |
| 84 | + # Test call count is tracked correctly |
| 85 | + mock_client = MockHTTPClient() |
| 86 | + mock_client.add_response() |
| 87 | + mock_client.add_response() |
| 88 | + |
| 89 | + request = create_test_request() |
| 90 | + |
| 91 | + assert mock_client.call_count == 0 |
| 92 | + |
| 93 | + await mock_client.send(request) |
| 94 | + assert mock_client.call_count == 1 |
| 95 | + |
| 96 | + await mock_client.send(request) |
| 97 | + assert mock_client.call_count == 2 |
| 98 | + |
| 99 | + |
| 100 | +async def test_captured_requests_copy(): |
| 101 | + # Test that captured_requests returns a copy to prevent modifications |
| 102 | + mock_client = MockHTTPClient() |
| 103 | + mock_client.add_response() |
| 104 | + |
| 105 | + request = create_test_request() |
| 106 | + |
| 107 | + await mock_client.send(request) |
| 108 | + |
| 109 | + captured1 = mock_client.captured_requests |
| 110 | + captured2 = mock_client.captured_requests |
| 111 | + |
| 112 | + # Should be different list objects |
| 113 | + assert captured1 is not captured2 |
| 114 | + # But with same content |
| 115 | + assert len(captured1) == len(captured2) == 1 |
0 commit comments