Skip to content

Commit 66a0c9c

Browse files
committed
PR comments
1 parent 7e88575 commit 66a0c9c

File tree

4 files changed

+64
-70
lines changed

4 files changed

+64
-70
lines changed

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Extensions/ApiGatewayResponseExtensions.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,11 @@ private static void SetContentTypeAndStatusCodeV2(HttpResponse response, IDictio
260260
}
261261
else
262262
{
263-
// Assume for now that when status code is not set (we are assuming 0 means not set) then set the default to application/json
264-
// Tehnically if the user were to return statusCode = 0 explicity in the response body, api gateway should internal server error, but since we don't
265-
// have a way to differentiate it and not returning status code is more common, we will do this way for now.
266-
// API Gateway 2.0 format version assumptions
267-
response.StatusCode = 200;
263+
response.StatusCode = 500;
268264
response.ContentType = "application/json";
269-
// Note: IsBase64Encoded is assumed to be false, which is already the default behavior
265+
var errorBytes = Encoding.UTF8.GetBytes("{\"message\":\"Internal Server Error\"}");
266+
response.Body = new MemoryStream(errorBytes);
267+
response.ContentLength = errorBytes.Length;
270268
}
271269
}
272270
}

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayResponseExtensionsAdditionalTests.cs

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,24 @@ public ApiGatewayResponseExtensionsAdditionalTests(ApiGatewayIntegrationTestFixt
2222
_httpClient = new HttpClient();
2323
}
2424

25-
[Fact]
26-
public async Task V2_SetsContentTypeApplicationJsonWhenNoStatusProvided()
27-
{
28-
var testResponse = new APIGatewayHttpApiV2ProxyResponse
29-
{
30-
Body = "Hello from lambda"
31-
};
32-
33-
var httpContext = new DefaultHttpContext();
34-
testResponse.ToHttpResponse(httpContext);
35-
var actualResponse = await _httpClient.PostAsync(_fixture.ReturnRawRequestBodyHttpApiV2Url, new StringContent("Hello from lambda"));
36-
37-
await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpContext.Response);
38-
Assert.Equal(200, (int)actualResponse.StatusCode);
39-
Assert.Equal("application/json", actualResponse.Content.Headers.ContentType?.ToString());
40-
var content = await actualResponse.Content.ReadAsStringAsync();
41-
Assert.Equal("Hello from lambda", content);
42-
}
43-
44-
[Fact]
45-
public async Task V2_SetsContentTypeApplicationJsonWhenNoStatusProvidedAndDoesntUseOtherType()
46-
{
47-
var payload = "{\"key\" : \"value\", \"headers\" : {\"content-type\": \"application/xml\"}}";
48-
49-
var testResponse = new APIGatewayHttpApiV2ProxyResponse
50-
{
51-
Body = payload
52-
};
53-
54-
var httpContext = new DefaultHttpContext();
55-
testResponse.ToHttpResponse(httpContext);
56-
57-
var actualResponse = await _httpClient.PostAsync(_fixture.ReturnRawRequestBodyHttpApiV2Url, new StringContent(payload));
58-
59-
await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpContext.Response);
60-
Assert.Equal(200, (int)actualResponse.StatusCode);
61-
Assert.Equal("application/json", actualResponse.Content.Headers.ContentType?.ToString());
62-
var content = await actualResponse.Content.ReadAsStringAsync();
63-
64-
var responsePayload = JsonSerializer.Deserialize<Dictionary<string, object>>(content);
65-
Assert.Equal("value", responsePayload?["key"].ToString());
66-
}
25+
//[Fact]
26+
//public async Task V2_SetsContentTypeApplicationJsonWhenNoStatusProvided()
27+
//{
28+
// var testResponse = new APIGatewayHttpApiV2ProxyResponse
29+
// {
30+
// Body = "Hello from lambda"
31+
// };
32+
33+
// var httpContext = new DefaultHttpContext();
34+
// testResponse.ToHttpResponse(httpContext);
35+
// var actualResponse = await _httpClient.PostAsync(_fixture.ReturnRawRequestBodyHttpApiV2Url, new StringContent("Hello from lambda"));
36+
37+
// await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpContext.Response);
38+
// Assert.Equal(200, (int)actualResponse.StatusCode);
39+
// Assert.Equal("application/json", actualResponse.Content.Headers.ContentType?.ToString());
40+
// var content = await actualResponse.Content.ReadAsStringAsync();
41+
// Assert.Equal("Hello from lambda", content);
42+
//}
6743

6844

6945
[Fact]

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/ApiGatewayResponseExtensionsTests.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,6 @@ public void ToHttpResponse_ConvertsCorrectlyV2(string testName, ApiGatewayRespon
5151
testCase.Assertions(httpContext.Response, ApiGatewayEmulatorMode.HttpV2);
5252
}
5353

54-
[Fact]
55-
public void ToHttpResponse_APIGatewayHttpApiV2ProxyResponse_InfersResponseFormatWhenStatusCodeNotSet()
56-
{
57-
var jsonBody = "{\"key\":\"value\"}";
58-
var apiResponse = new APIGatewayHttpApiV2ProxyResponse
59-
{
60-
Body = jsonBody,
61-
StatusCode = 0 // No status code set
62-
};
63-
64-
var httpContext = new DefaultHttpContext();
65-
apiResponse.ToHttpResponse(httpContext);
66-
67-
Assert.Equal(200, httpContext.Response.StatusCode);
68-
Assert.Equal("application/json", httpContext.Response.ContentType);
69-
70-
httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
71-
using var reader = new StreamReader(httpContext.Response.Body);
72-
var bodyContent = reader.ReadToEnd();
73-
Assert.Equal(jsonBody, bodyContent);
74-
}
75-
7654
[Theory]
7755
[InlineData(ApiGatewayEmulatorMode.HttpV1)]
7856
[InlineData(ApiGatewayEmulatorMode.Rest)]

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/ApiGatewayResponseTestCases.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,48 @@ public static IEnumerable<object[]> V2TestCases()
482482
}
483483
};
484484

485+
yield return new object[]
486+
{
487+
"V2_HandlesZeroStatusCode",
488+
new ApiGatewayResponseTestCase
489+
{
490+
Response = new APIGatewayHttpApiV2ProxyResponse
491+
{
492+
StatusCode = 0,
493+
Body = "{\"key\":\"This body should be replaced\"}"
494+
},
495+
Assertions = (response, emulatorMode) =>
496+
{
497+
string error;
498+
int contentLength;
499+
int statusCode;
500+
error = "\"Internal Server Error\"}";
501+
contentLength = 35;
502+
statusCode = 500;
503+
Assert.Equal(statusCode, response.StatusCode);
504+
Assert.Equal("application/json", response.ContentType);
505+
Assert.Equal("{\"message\":"+error, ReadResponseBody(response));
506+
Assert.Equal(contentLength, response.ContentLength);
507+
},
508+
IntegrationAssertions = async (response, emulatorMode) =>
509+
{
510+
string error;
511+
int contentLength;
512+
int statusCode;
513+
514+
error = "\"Internal Server Error\"}";
515+
contentLength = 35;
516+
statusCode = 500;
517+
Assert.Equal(statusCode, (int)response.StatusCode);
518+
Assert.Equal("application/json", response.Content.Headers.ContentType?.ToString());
519+
var content = await response.Content.ReadAsStringAsync();
520+
Assert.Equal("{\"message\":"+error, content);
521+
Assert.Equal(contentLength, response.Content.Headers.ContentLength);
522+
await Task.CompletedTask;
523+
}
524+
}
525+
};
526+
485527
yield return new object[]
486528
{
487529
"V2_SetsHeaders",

0 commit comments

Comments
 (0)