Ocelot/test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs
Marc Denman 0c33323352 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.
2017-03-14 09:15:19 +00:00

83 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using Ocelot.Errors;
using Ocelot.Middleware;
using Ocelot.Requester;
using Ocelot.Responder;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Responder
{
public class ErrorsToHttpStatusCodeMapperTests
{
private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
private int _result;
private List<Error> _errors;
public ErrorsToHttpStatusCodeMapperTests()
{
_codeMapper = new ErrorsToHttpStatusCodeMapper();
}
[Fact]
public void should_return_timeout()
{
this.Given(x => x.GivenThereAreErrors(new List<Error>
{
new RequestTimedOutError(new Exception())
}))
.When(x => x.WhenIGetErrorStatusCode())
.Then(x => x.ThenTheResponseIsStatusCodeIs(503))
.BDDfy();
}
[Fact]
public void should_create_unauthenticated_response_code()
{
this.Given(x => x.GivenThereAreErrors(new List<Error>
{
new UnauthenticatedError("no matter")
}))
.When(x => x.WhenIGetErrorStatusCode())
.Then(x => x.ThenTheResponseIsStatusCodeIs(401))
.BDDfy();
}
[Fact]
public void should_create_not_found_response_response_code()
{
this.Given(x => x.GivenThereAreErrors(new List<Error>
{
new AnyError()
}))
.When(x => x.WhenIGetErrorStatusCode())
.Then(x => x.ThenTheResponseIsStatusCodeIs(404))
.BDDfy();
}
class AnyError : Error
{
public AnyError() : base("blahh", OcelotErrorCode.UnknownError)
{
}
}
private void GivenThereAreErrors(List<Error> errors)
{
_errors = errors;
}
private void WhenIGetErrorStatusCode()
{
_result = _codeMapper.Map(_errors);
}
private void ThenTheResponseIsStatusCodeIs(int expectedCode)
{
_result.ShouldBe(expectedCode);
}
}
}