Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AspNetCoreServer: Update for default 500 error code on exception. #33

Merged
merged 1 commit into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected HostingApplication.Context CreateContext(IFeatureCollection features)
/// <param name="features">An <see cref="InvokeFeatures"/> instance.</param>
protected async Task<APIGatewayProxyResponse> ProcessRequest(ILambdaContext lambdaContext, HostingApplication.Context context, InvokeFeatures features)
{
var defaultStatusCode = 200;
try
{
await this._server.Application.ProcessRequestAsync(context);
Expand All @@ -104,14 +105,15 @@ protected async Task<APIGatewayProxyResponse> ProcessRequest(ILambdaContext lamb
{
lambdaContext?.Logger.Log($"Unknown error responding to request: {this.ErrorReport(e)}");
this._server.Application.DisposeContext(context, e);
defaultStatusCode = 500;
}

var response = this.MarshallResponse(features);

// ASP.NET Core Web API does not always set the status code if the request was
// successful
if (response.StatusCode == 0)
response.StatusCode = 200;
response.StatusCode = defaultStatusCode;

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async Task TestGetAllValues()
var request = JsonConvert.DeserializeObject<APIGatewayProxyRequest>(requestStr);
var response = await lambdaFunction.FunctionHandlerAsync(request, null);

Assert.Equal(response.StatusCode, 200);
Assert.Equal("[\"value1\",\"value2\"]", response.Body);
Assert.True(response.Headers.ContainsKey("Content-Type"));
Assert.Equal("application/json; charset=utf-8", response.Headers["Content-Type"]);
Expand Down Expand Up @@ -74,5 +75,18 @@ public async Task TestPutWithBody()
Assert.True(response.Headers.ContainsKey("Content-Type"));
Assert.Equal("text/plain; charset=utf-8", response.Headers["Content-Type"]);
}

[Fact]
public async Task TestDefaultResponseErrorCode()
{
var lambdaFunction = new LambdaFunction();

var requestStr = File.ReadAllText("values-get-error-apigatway-request.json");
var request = JsonConvert.DeserializeObject<APIGatewayProxyRequest>(requestStr);
var response = await lambdaFunction.FunctionHandlerAsync(request, null);

Assert.Equal(response.StatusCode, 500);
Assert.Equal(string.Empty, response.Body);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"resource": "/{proxy+}",
"path": "/api/errortests",
"httpMethod": "GET",
"headers": null,
"queryStringParameters": {
"id": "error-expected"
},
"pathParameters": {
"proxy": "api/values"
},
"stageVariables": null,
"requestContext": {
"accountId": "AAAAAAAAAAAA",
"resourceId": "5agfss",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": "AAAAAAAAAAAA",
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_102)",
"user": "AAAAAAAAAAAA"
},
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"apiId": "t2yh6sjnmk"
},
"body": null
}
15 changes: 15 additions & 0 deletions Libraries/test/TestWebApp/Controllers/ErrorTestsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Microsoft.AspNetCore.Mvc;

namespace TestWebApp.Controllers
{
[Route("api/[controller]")]
public class ErrorTestsController
{
[HttpGet]
public string Get(string id)
{
throw new Exception("Unit test exception, for test conditions.");
}
}
}