From 56af10bd1a13fa5809bbb63c1e5a2b6cb8a44407 Mon Sep 17 00:00:00 2001 From: Garrett Beatty Date: Thu, 12 Dec 2024 17:34:53 -0500 Subject: [PATCH] add tests for content type --- .../ApiGatewayResponseExtensionsTests.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/ApiGatewayResponseExtensionsTests.cs b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/ApiGatewayResponseExtensionsTests.cs index 7e80f1941..6f52941ef 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/ApiGatewayResponseExtensionsTests.cs +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/ApiGatewayResponseExtensionsTests.cs @@ -343,4 +343,83 @@ public void ToHttpResponse_APIGatewayHttpApiV2ProxyResponse_HandlesNonJsonRespon Assert.Equal("{\"message\":\"Internal Server Error\"}", bodyContent); Assert.Equal(35, httpResponse.ContentLength); } + + [Fact] + public void ToHttpResponse_APIGatewayProxyResponse_DefaultsToTextPlainContentType() + { + var apiResponse = new APIGatewayProxyResponse + { + StatusCode = 200, + Body = "Hello, World!" + }; + + var httpResponse = apiResponse.ToHttpResponse(); + + Assert.Equal("text/plain; charset=utf-8", httpResponse.ContentType); + } + + [Fact] + public void ToHttpResponse_APIGatewayProxyResponse_UsesProvidedContentType() + { + var apiResponse = new APIGatewayProxyResponse + { + StatusCode = 200, + Body = "Hello, World!", + Headers = new Dictionary + { + { "Content-Type", "application/json" } + } + }; + + var httpResponse = apiResponse.ToHttpResponse(); + + Assert.Equal("application/json", httpResponse.ContentType); + } + + [Fact] + public void ToHttpResponse_APIGatewayHttpApiV2ProxyResponse_DefaultsToTextPlainContentType() + { + var apiResponse = new APIGatewayHttpApiV2ProxyResponse + { + StatusCode = 200, + Body = "Hello, World!" + }; + + var httpResponse = apiResponse.ToHttpResponse(); + + Assert.Equal("text/plain; charset=utf-8", httpResponse.ContentType); + } + + [Fact] + public void ToHttpResponse_APIGatewayHttpApiV2ProxyResponse_UsesProvidedContentType() + { + var apiResponse = new APIGatewayHttpApiV2ProxyResponse + { + StatusCode = 200, + Body = "Hello, World!", + Headers = new Dictionary + { + { "Content-Type", "application/xml" } + } + }; + + var httpResponse = apiResponse.ToHttpResponse(); + + Assert.Equal("application/xml", httpResponse.ContentType); + } + + [Fact] + public void ToHttpResponse_APIGatewayHttpApiV2ProxyResponse_DefaultsToApplicationJsonForValidJson() + { + var apiResponse = new APIGatewayHttpApiV2ProxyResponse + { + StatusCode = 0, + Body = "{\"message\":\"Hello, World!\"}" + }; + + var httpResponse = apiResponse.ToHttpResponse(); + + Assert.Equal("application/json", httpResponse.ContentType); + Assert.Equal(200, httpResponse.StatusCode); + } }