Change HttpStatusCodeMapper not to wrap responses

As part of #66 we realised that the implementation of
IErrorToHttpStatusCodeMapper would always return a wrapped StatusCode
within an OK response, in turn meaning that ResponderMiddleware would
never fall into the else branch for returning a 500.

This commit removes the wrapping of the status code and removes the unused
logic for generating the 500 status code, giving the mapper full
responsbility for generating the correct status code.
This commit is contained in:
Marc Denman 2017-03-14 09:15:19 +00:00
parent c09cec67c6
commit 0c33323352
4 changed files with 18 additions and 25 deletions

View File

@ -1,17 +1,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Ocelot.Errors; using Ocelot.Errors;
using Ocelot.Responses;
namespace Ocelot.Responder namespace Ocelot.Responder
{ {
public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
{ {
public Response<int> Map(List<Error> errors) public int Map(List<Error> errors)
{ {
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError)) if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
{ {
return new OkResponse<int>(401); return 401;
} }
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError
@ -19,15 +18,15 @@ namespace Ocelot.Responder
|| e.Code == OcelotErrorCode.UserDoesNotHaveClaimError || e.Code == OcelotErrorCode.UserDoesNotHaveClaimError
|| e.Code == OcelotErrorCode.CannotFindClaimError)) || e.Code == OcelotErrorCode.CannotFindClaimError))
{ {
return new OkResponse<int>(403); return 403;
} }
if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError)) if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError))
{ {
return new OkResponse<int>(503); return 503;
} }
return new OkResponse<int>(404); return 404;
} }
} }
} }

View File

@ -1,11 +1,13 @@
using System.Collections.Generic; using System.Collections.Generic;
using Ocelot.Errors; using Ocelot.Errors;
using Ocelot.Responses;
namespace Ocelot.Responder namespace Ocelot.Responder
{ {
/// <summary>
/// Map a list OceoltErrors to a single appropriate HTTP status code
/// </summary>
public interface IErrorsToHttpStatusCodeMapper public interface IErrorsToHttpStatusCodeMapper
{ {
Response<int> Map(List<Error> errors); int Map(List<Error> errors);
} }
} }

View File

@ -21,7 +21,7 @@ namespace Ocelot.Responder.Middleware
IOcelotLoggerFactory loggerFactory, IOcelotLoggerFactory loggerFactory,
IRequestScopedDataRepository requestScopedDataRepository, IRequestScopedDataRepository requestScopedDataRepository,
IErrorsToHttpStatusCodeMapper codeMapper) IErrorsToHttpStatusCodeMapper codeMapper)
:base(requestScopedDataRepository) : base(requestScopedDataRepository)
{ {
_next = next; _next = next;
_responder = responder; _responder = responder;
@ -60,14 +60,7 @@ namespace Ocelot.Responder.Middleware
{ {
var statusCode = _codeMapper.Map(errors); var statusCode = _codeMapper.Map(errors);
if (!statusCode.IsError) _responder.SetErrorResponseOnContext(context, statusCode);
{
_responder.SetErrorResponseOnContext(context, statusCode.Data);
}
else
{
_responder.SetErrorResponseOnContext(context, 500);
}
} }
} }
} }

View File

@ -4,7 +4,6 @@ using Ocelot.Errors;
using Ocelot.Middleware; using Ocelot.Middleware;
using Ocelot.Requester; using Ocelot.Requester;
using Ocelot.Responder; using Ocelot.Responder;
using Ocelot.Responses;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
@ -14,7 +13,7 @@ namespace Ocelot.UnitTests.Responder
public class ErrorsToHttpStatusCodeMapperTests public class ErrorsToHttpStatusCodeMapperTests
{ {
private readonly IErrorsToHttpStatusCodeMapper _codeMapper; private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
private Response<int> _result; private int _result;
private List<Error> _errors; private List<Error> _errors;
public ErrorsToHttpStatusCodeMapperTests() public ErrorsToHttpStatusCodeMapperTests()
@ -77,7 +76,7 @@ namespace Ocelot.UnitTests.Responder
private void ThenTheResponseIsStatusCodeIs(int expectedCode) private void ThenTheResponseIsStatusCodeIs(int expectedCode)
{ {
_result.Data.ShouldBe(expectedCode); _result.ShouldBe(expectedCode);
} }
} }
} }