|
| 1 | +import pytest |
| 2 | + |
| 3 | +from localstack.http import Request |
| 4 | +from localstack.services.apigateway.next_gen.execute_api.context import ( |
| 5 | + IntegrationRequest, |
| 6 | + RestApiInvocationContext, |
| 7 | +) |
| 8 | +from localstack.services.apigateway.next_gen.execute_api.gateway_response import InternalServerError |
| 9 | +from localstack.services.apigateway.next_gen.execute_api.integrations import RestApiMockIntegration |
| 10 | +from localstack.utils.strings import to_bytes |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def create_default_context(): |
| 15 | + def _create_context(body: str) -> RestApiInvocationContext: |
| 16 | + context = RestApiInvocationContext(request=Request()) |
| 17 | + context.integration_request = IntegrationRequest(body=to_bytes(body)) |
| 18 | + return context |
| 19 | + |
| 20 | + return _create_context |
| 21 | + |
| 22 | + |
| 23 | +class TestMockIntegration: |
| 24 | + def test_mock_integration(self, create_default_context): |
| 25 | + mock_integration = RestApiMockIntegration() |
| 26 | + |
| 27 | + ctx = create_default_context(body='{"statusCode": 200}') |
| 28 | + response = mock_integration.invoke(ctx) |
| 29 | + assert response.status_code == 200 |
| 30 | + |
| 31 | + # It needs to be an integer |
| 32 | + ctx = create_default_context(body='{"statusCode": "200"}') |
| 33 | + with pytest.raises(InternalServerError) as exc_info: |
| 34 | + mock_integration.invoke(ctx) |
| 35 | + assert exc_info.match("Internal server error") |
| 36 | + |
| 37 | + # Any integer will do |
| 38 | + ctx = create_default_context(body='{"statusCode": 0}') |
| 39 | + response = mock_integration.invoke(ctx) |
| 40 | + assert response.status_code == 0 |
| 41 | + |
| 42 | + # Literally any |
| 43 | + ctx = create_default_context(body='{"statusCode": -1000}') |
| 44 | + response = mock_integration.invoke(ctx) |
| 45 | + assert response.status_code == -1000 |
| 46 | + |
| 47 | + # Malformed Json |
| 48 | + ctx = create_default_context(body='{"statusCode": 200') |
| 49 | + with pytest.raises(InternalServerError) as exc_info: |
| 50 | + mock_integration.invoke(ctx) |
| 51 | + assert exc_info.match("Internal server error") |
0 commit comments