|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using Amazon.Lambda.APIGatewayEvents; |
| 5 | +using Amazon.Lambda.Model; |
| 6 | +using Amazon.Lambda.TestTool.Models; |
| 7 | +using System.Text; |
| 8 | +using System.Text.Json; |
| 9 | + |
| 10 | +namespace Amazon.Lambda.TestTool.IntegrationTests; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Integration tests for InvokeResponseExtensions. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// Developer's Note: |
| 17 | +/// These tests don't have direct access to the intermediate result of the Lambda to API Gateway conversion. |
| 18 | +/// Instead, we test the final API Gateway response object to ensure our conversion methods produce results |
| 19 | +/// that match the actual API Gateway behavior. This approach allows us to verify the correctness of our |
| 20 | +/// conversion methods within the constraints of not having access to AWS's internal conversion process. |
| 21 | +/// </remarks> |
| 22 | +[Collection("ApiGateway Integration Tests")] |
| 23 | +public class InvokeResponseExtensionsIntegrationTests |
| 24 | +{ |
| 25 | + private readonly ApiGatewayIntegrationTestFixture _fixture; |
| 26 | + |
| 27 | + public InvokeResponseExtensionsIntegrationTests(ApiGatewayIntegrationTestFixture fixture) |
| 28 | + { |
| 29 | + _fixture = fixture; |
| 30 | + } |
| 31 | + |
| 32 | + [Theory] |
| 33 | + [InlineData(ApiGatewayEmulatorMode.Rest)] |
| 34 | + [InlineData(ApiGatewayEmulatorMode.HttpV1)] |
| 35 | + public async Task ToApiGatewayProxyResponse_ValidResponse_MatchesDirectConversion(ApiGatewayEmulatorMode emulatorMode) |
| 36 | + { |
| 37 | + // Arrange |
| 38 | + var testResponse = new APIGatewayProxyResponse |
| 39 | + { |
| 40 | + StatusCode = 200, |
| 41 | + Body = JsonSerializer.Serialize(new { message = "Hello, World!" }), |
| 42 | + Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } } |
| 43 | + }; |
| 44 | + var invokeResponse = new InvokeResponse |
| 45 | + { |
| 46 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(testResponse))) |
| 47 | + }; |
| 48 | + |
| 49 | + // Act |
| 50 | + var convertedResponse = invokeResponse.ToApiGatewayProxyResponse(emulatorMode); |
| 51 | + |
| 52 | + // Assert |
| 53 | + var apiUrl = emulatorMode == ApiGatewayEmulatorMode.Rest |
| 54 | + ? _fixture.ParseAndReturnBodyRestApiUrl |
| 55 | + : _fixture.ParseAndReturnBodyHttpApiV1Url; |
| 56 | + var (actualResponse, httpTestResponse) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(convertedResponse, apiUrl, emulatorMode); |
| 57 | + await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpTestResponse); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public async Task ToApiGatewayHttpApiV2ProxyResponse_ValidResponse_MatchesDirectConversion() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + var testResponse = new APIGatewayHttpApiV2ProxyResponse |
| 65 | + { |
| 66 | + StatusCode = 200, |
| 67 | + Body = JsonSerializer.Serialize(new { message = "Hello, World!" }), |
| 68 | + Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } } |
| 69 | + }; |
| 70 | + var invokeResponse = new InvokeResponse |
| 71 | + { |
| 72 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(testResponse))) |
| 73 | + }; |
| 74 | + |
| 75 | + // Act |
| 76 | + var convertedResponse = invokeResponse.ToApiGatewayHttpApiV2ProxyResponse(); |
| 77 | + |
| 78 | + // Assert |
| 79 | + var (actualResponse, httpTestResponse) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(convertedResponse, _fixture.ParseAndReturnBodyHttpApiV2Url); |
| 80 | + await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpTestResponse); |
| 81 | + } |
| 82 | + |
| 83 | + [Theory] |
| 84 | + [InlineData(ApiGatewayEmulatorMode.Rest, 502, "Internal server error")] |
| 85 | + [InlineData(ApiGatewayEmulatorMode.HttpV1, 500, "Internal Server Error")] |
| 86 | + public async Task ToApiGatewayProxyResponse_InvalidJson_ReturnsErrorResponse(ApiGatewayEmulatorMode emulatorMode, int expectedStatusCode, string expectedErrorMessage) |
| 87 | + { |
| 88 | + // Arrange |
| 89 | + var invokeResponse = new InvokeResponse |
| 90 | + { |
| 91 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes("Not a valid proxy response object")) |
| 92 | + }; |
| 93 | + |
| 94 | + // Act |
| 95 | + var convertedResponse = invokeResponse.ToApiGatewayProxyResponse(emulatorMode); |
| 96 | + |
| 97 | + // Assert |
| 98 | + Assert.Equal(expectedStatusCode, convertedResponse.StatusCode); |
| 99 | + Assert.Contains(expectedErrorMessage, convertedResponse.Body); |
| 100 | + |
| 101 | + var apiUrl = emulatorMode == ApiGatewayEmulatorMode.Rest |
| 102 | + ? _fixture.ParseAndReturnBodyRestApiUrl |
| 103 | + : _fixture.ParseAndReturnBodyHttpApiV1Url; |
| 104 | + var (actualResponse, _) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(convertedResponse, apiUrl, emulatorMode); |
| 105 | + Assert.Equal(expectedStatusCode, (int)actualResponse.StatusCode); |
| 106 | + var content = await actualResponse.Content.ReadAsStringAsync(); |
| 107 | + Assert.Contains(expectedErrorMessage, content); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Tests various Lambda return values to verify API Gateway's handling of responses. |
| 112 | + /// </summary> |
| 113 | + /// <param name="responsePayload">The payload returned by the Lambda function.</param> |
| 114 | + /// <remarks> |
| 115 | + /// This test demonstrates a discrepancy between the official AWS documentation |
| 116 | + /// and the actual observed behavior of API Gateway HTTP API v2 with Lambda |
| 117 | + /// proxy integrations (payload format version 2.0). |
| 118 | + /// |
| 119 | + /// Official documentation states: |
| 120 | + /// "If your Lambda function returns valid JSON and doesn't return a statusCode, |
| 121 | + /// API Gateway assumes a 200 status code and treats the entire response as the body." |
| 122 | + /// |
| 123 | + /// However, the observed behavior (which this test verifies) is: |
| 124 | + /// - API Gateway does not validate whether the returned data is valid JSON. |
| 125 | + /// - Any response from the Lambda function that is not a properly formatted |
| 126 | + /// API Gateway response object (i.e., an object with a 'statusCode' property) |
| 127 | + /// is treated as a raw body in a 200 OK response. |
| 128 | + /// - This includes valid JSON objects without a statusCode, JSON arrays, |
| 129 | + /// primitive values, and invalid JSON strings. |
| 130 | + /// |
| 131 | + /// This test ensures that our ToApiGatewayHttpApiV2ProxyResponse method |
| 132 | + /// correctly replicates this observed behavior, rather than the documented behavior. |
| 133 | + /// </remarks> |
| 134 | + [Theory] |
| 135 | + [InlineData("{\"name\": \"John Doe\", \"age\":")] // Invalid JSON (partial object) |
| 136 | + [InlineData("{\"name\": \"John Doe\", \"age\": 30}")] // Valid JSON object without statusCode |
| 137 | + [InlineData("[1, 2, 3, 4, 5]")] // JSON array |
| 138 | + [InlineData("\"Hello, World!\"")] // String primitive |
| 139 | + [InlineData("42")] // Number primitive |
| 140 | + [InlineData("true")] // Boolean primitive |
| 141 | + public async Task ToApiGatewayHttpApiV2ProxyResponse_VariousPayloads_ReturnsAsRawBody(string responsePayload) |
| 142 | + { |
| 143 | + // Arrange |
| 144 | + var invokeResponse = new InvokeResponse |
| 145 | + { |
| 146 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes(responsePayload)) |
| 147 | + }; |
| 148 | + |
| 149 | + // Act |
| 150 | + var convertedResponse = invokeResponse.ToApiGatewayHttpApiV2ProxyResponse(); |
| 151 | + |
| 152 | + // Assert |
| 153 | + Assert.Equal(200, convertedResponse.StatusCode); |
| 154 | + Assert.Equal(responsePayload, convertedResponse.Body); |
| 155 | + Assert.Equal("application/json", convertedResponse.Headers["Content-Type"]); |
| 156 | + |
| 157 | + // Verify against actual API Gateway behavior |
| 158 | + var (actualResponse, httpTestResponse) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(convertedResponse, _fixture.ParseAndReturnBodyHttpApiV2Url); |
| 159 | + await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpTestResponse); |
| 160 | + |
| 161 | + // Additional checks for API Gateway specific behavior |
| 162 | + Assert.Equal(200, (int)actualResponse.StatusCode); |
| 163 | + var content = await actualResponse.Content.ReadAsStringAsync(); |
| 164 | + Assert.Equal(responsePayload, content); |
| 165 | + Assert.Equal("application/json", actualResponse.Content.Headers.ContentType?.ToString()); |
| 166 | + } |
| 167 | + |
| 168 | + [Fact] |
| 169 | + public async Task ToApiGatewayHttpApiV2ProxyResponse_StatusCodeAsFloat_ReturnsInternalServerError() |
| 170 | + { |
| 171 | + // Arrange |
| 172 | + var responsePayload = "{\"statusCode\": 200.5, \"body\": \"Hello\", \"headers\": {\"Content-Type\": \"text/plain\"}}"; |
| 173 | + var invokeResponse = new InvokeResponse |
| 174 | + { |
| 175 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes(responsePayload)) |
| 176 | + }; |
| 177 | + |
| 178 | + // Act |
| 179 | + var convertedResponse = invokeResponse.ToApiGatewayHttpApiV2ProxyResponse(); |
| 180 | + |
| 181 | + // Assert |
| 182 | + Assert.Equal(500, convertedResponse.StatusCode); |
| 183 | + Assert.Equal("{\"message\":\"Internal Server Error\"}", convertedResponse.Body); |
| 184 | + Assert.Equal("application/json", convertedResponse.Headers["Content-Type"]); |
| 185 | + |
| 186 | + // Verify against actual API Gateway behavior |
| 187 | + var (actualResponse, httpTestResponse) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(convertedResponse, _fixture.ParseAndReturnBodyHttpApiV2Url); |
| 188 | + await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpTestResponse); |
| 189 | + |
| 190 | + // Additional checks for API Gateway specific behavior |
| 191 | + Assert.Equal(500, (int)actualResponse.StatusCode); |
| 192 | + var content = await actualResponse.Content.ReadAsStringAsync(); |
| 193 | + Assert.Equal("{\"message\":\"Internal Server Error\"}", content); |
| 194 | + Assert.Equal("application/json", actualResponse.Content.Headers.ContentType?.ToString()); |
| 195 | + } |
| 196 | +} |
0 commit comments