mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
Added test coverage around responder middleware, and refactored these to not use test server.
This commit is contained in:
parent
c173f3bb45
commit
b0c12431d6
@ -36,7 +36,7 @@ namespace Ocelot.Authentication.Middleware
|
|||||||
{
|
{
|
||||||
if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
|
if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
|
||||||
{
|
{
|
||||||
_logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlwareName} checking if client is authenticated");
|
_logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated");
|
||||||
|
|
||||||
var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
|
var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
|
|||||||
|
|
||||||
if (downstreamRoute.IsError)
|
if (downstreamRoute.IsError)
|
||||||
{
|
{
|
||||||
_logger.LogError($"{MiddlwareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");
|
_logger.LogError($"{MiddlewareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");
|
||||||
|
|
||||||
SetPipelineError(downstreamRoute.Errors);
|
SetPipelineError(downstreamRoute.Errors);
|
||||||
return;
|
return;
|
||||||
|
@ -13,10 +13,10 @@ namespace Ocelot.Middleware
|
|||||||
protected OcelotMiddleware(IRequestScopedDataRepository requestScopedDataRepository)
|
protected OcelotMiddleware(IRequestScopedDataRepository requestScopedDataRepository)
|
||||||
{
|
{
|
||||||
_requestScopedDataRepository = requestScopedDataRepository;
|
_requestScopedDataRepository = requestScopedDataRepository;
|
||||||
MiddlwareName = this.GetType().Name;
|
MiddlewareName = this.GetType().Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string MiddlwareName { get; }
|
public string MiddlewareName { get; }
|
||||||
|
|
||||||
public bool PipelineError => _requestScopedDataRepository.Get<bool>("OcelotMiddlewareError").Data;
|
public bool PipelineError => _requestScopedDataRepository.Get<bool>("OcelotMiddlewareError").Data;
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ namespace Ocelot.Responder.Middleware
|
|||||||
if (PipelineError)
|
if (PipelineError)
|
||||||
{
|
{
|
||||||
var errors = PipelineErrors;
|
var errors = PipelineErrors;
|
||||||
_logger.LogError($"{errors.Count} pipeline errors found in {MiddlwareName}. Setting error response status code");
|
_logger.LogError($"{PipelineErrors.Count} pipeline errors found in {MiddlewareName}. Setting error response status code");
|
||||||
|
|
||||||
SetErrorResponse(context, errors);
|
SetErrorResponse(context, errors);
|
||||||
}
|
}
|
||||||
@ -53,7 +53,6 @@ namespace Ocelot.Responder.Middleware
|
|||||||
private void SetErrorResponse(HttpContext context, List<Error> errors)
|
private void SetErrorResponse(HttpContext context, List<Error> errors)
|
||||||
{
|
{
|
||||||
var statusCode = _codeMapper.Map(errors);
|
var statusCode = _codeMapper.Map(errors);
|
||||||
|
|
||||||
_responder.SetErrorResponseOnContext(context, statusCode);
|
_responder.SetErrorResponseOnContext(context, statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
test/Ocelot.UnitTests/Responder/AnyError.cs
Normal file
15
test/Ocelot.UnitTests/Responder/AnyError.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using Ocelot.Errors;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.Responder
|
||||||
|
{
|
||||||
|
class AnyError : Error
|
||||||
|
{
|
||||||
|
public AnyError() : base("blahh", OcelotErrorCode.UnknownError)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnyError(OcelotErrorCode errorCode) : base("blah", errorCode)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
using Ocelot.Middleware;
|
|
||||||
using Ocelot.Requester;
|
|
||||||
using Ocelot.Responder;
|
using Ocelot.Responder;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
@ -21,47 +20,127 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
_codeMapper = new ErrorsToHttpStatusCodeMapper();
|
_codeMapper = new ErrorsToHttpStatusCodeMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void should_return_timeout()
|
[InlineData(OcelotErrorCode.UnauthenticatedError)]
|
||||||
|
public void should_return_unauthorized(OcelotErrorCode errorCode)
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereAreErrors(new List<Error>
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(OcelotErrorCode.CannotFindClaimError)]
|
||||||
|
[InlineData(OcelotErrorCode.ClaimValueNotAuthorisedError)]
|
||||||
|
[InlineData(OcelotErrorCode.ScopeNotAuthorisedError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnauthorizedError)]
|
||||||
|
[InlineData(OcelotErrorCode.UserDoesNotHaveClaimError)]
|
||||||
|
public void should_return_forbidden(OcelotErrorCode errorCode)
|
||||||
{
|
{
|
||||||
new RequestTimedOutError(new Exception())
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.Forbidden);
|
||||||
}))
|
}
|
||||||
.When(x => x.WhenIGetErrorStatusCode())
|
|
||||||
.Then(x => x.ThenTheResponseIsStatusCodeIs(503))
|
[Theory]
|
||||||
.BDDfy();
|
[InlineData(OcelotErrorCode.RequestTimedOutError)]
|
||||||
|
public void should_return_service_unavailable(OcelotErrorCode errorCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.ServiceUnavailable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(OcelotErrorCode.CannotAddDataError)]
|
||||||
|
[InlineData(OcelotErrorCode.CannotFindDataError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamHostNullOrEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamPathNullOrEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreampathTemplateAlreadyUsedError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamPathTemplateContainsSchemeError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamSchemeNullOrEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.InstructionNotForClaimsError)]
|
||||||
|
[InlineData(OcelotErrorCode.NoInstructionsError)]
|
||||||
|
[InlineData(OcelotErrorCode.ParsingConfigurationHeaderError)]
|
||||||
|
[InlineData(OcelotErrorCode.RateLimitOptionsError)]
|
||||||
|
[InlineData(OcelotErrorCode.ServicesAreEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.ServicesAreNullError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToCompleteRequestError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToCreateAuthenticationHandlerError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindDownstreamRouteError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindLoadBalancerError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindServiceDiscoveryProviderError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindQoSProviderError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToSetConfigInConsulError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnknownError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnmappableRequestError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnsupportedAuthenticationProviderError)]
|
||||||
|
public void should_return_not_found(OcelotErrorCode errorCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_create_unauthenticated_response_code()
|
public void AuthenticationErrorsHaveHighestPriority()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereAreErrors(new List<Error>
|
var errors = new List<OcelotErrorCode>
|
||||||
{
|
{
|
||||||
new UnauthenticatedError("no matter")
|
OcelotErrorCode.CannotAddDataError,
|
||||||
}))
|
OcelotErrorCode.CannotFindClaimError,
|
||||||
.When(x => x.WhenIGetErrorStatusCode())
|
OcelotErrorCode.UnauthenticatedError,
|
||||||
.Then(x => x.ThenTheResponseIsStatusCodeIs(401))
|
OcelotErrorCode.RequestTimedOutError,
|
||||||
.BDDfy();
|
};
|
||||||
|
|
||||||
|
ShouldMapErrorsToStatusCode(errors, HttpStatusCode.Unauthorized);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_create_not_found_response_response_code()
|
public void AuthorisationErrorsHaveSecondHighestPriority()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereAreErrors(new List<Error>
|
var errors = new List<OcelotErrorCode>
|
||||||
{
|
{
|
||||||
new AnyError()
|
OcelotErrorCode.CannotAddDataError,
|
||||||
}))
|
OcelotErrorCode.CannotFindClaimError,
|
||||||
.When(x => x.WhenIGetErrorStatusCode())
|
OcelotErrorCode.RequestTimedOutError
|
||||||
.Then(x => x.ThenTheResponseIsStatusCodeIs(404))
|
};
|
||||||
.BDDfy();
|
|
||||||
|
ShouldMapErrorsToStatusCode(errors, HttpStatusCode.Forbidden);
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnyError : Error
|
[Fact]
|
||||||
|
public void ServiceUnavailableErrorsHaveThirdHighestPriority()
|
||||||
{
|
{
|
||||||
public AnyError() : base("blahh", OcelotErrorCode.UnknownError)
|
var errors = new List<OcelotErrorCode>
|
||||||
{
|
{
|
||||||
|
OcelotErrorCode.CannotAddDataError,
|
||||||
|
OcelotErrorCode.RequestTimedOutError
|
||||||
|
};
|
||||||
|
|
||||||
|
ShouldMapErrorsToStatusCode(errors, HttpStatusCode.ServiceUnavailable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void check_we_have_considered_all_errors_in_these_tests()
|
||||||
|
{
|
||||||
|
// If this test fails then it's because the number of error codes has changed.
|
||||||
|
// You should make the appropriate changes to the test cases here to ensure
|
||||||
|
// they cover all the error codes, and then modify this assertion.
|
||||||
|
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(30, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorsToStatusCode(new List<OcelotErrorCode> { errorCode }, expectedHttpStatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShouldMapErrorsToStatusCode(List<OcelotErrorCode> errorCodes, HttpStatusCode expectedHttpStatusCode)
|
||||||
|
{
|
||||||
|
var errors = new List<Error>();
|
||||||
|
|
||||||
|
foreach(var errorCode in errorCodes)
|
||||||
|
{
|
||||||
|
errors.Add(new AnyError(errorCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Given(x => x.GivenThereAreErrors(errors))
|
||||||
|
.When(x => x.WhenIGetErrorStatusCode())
|
||||||
|
.Then(x => x.ThenTheResponseIsStatusCodeIs(expectedHttpStatusCode))
|
||||||
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThereAreErrors(List<Error> errors)
|
private void GivenThereAreErrors(List<Error> errors)
|
||||||
@ -78,5 +157,10 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
{
|
{
|
||||||
_result.ShouldBe(expectedCode);
|
_result.ShouldBe(expectedCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ThenTheResponseIsStatusCodeIs(HttpStatusCode expectedCode)
|
||||||
|
{
|
||||||
|
_result.ShouldBe((int)expectedCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
140
test/Ocelot.UnitTests/Responder/ResponderMiddlewareTestsV2.cs
Normal file
140
test/Ocelot.UnitTests/Responder/ResponderMiddlewareTestsV2.cs
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Errors;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responder;
|
||||||
|
using Ocelot.Responder.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.Responder
|
||||||
|
{
|
||||||
|
public class ResponderMiddlewareTestsV2
|
||||||
|
{
|
||||||
|
private readonly Mock<IHttpResponder> _responder;
|
||||||
|
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||||
|
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
||||||
|
private readonly Mock<RequestDelegate> _next;
|
||||||
|
private readonly Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
|
private readonly Mock<IOcelotLogger> _logger;
|
||||||
|
private readonly Mock<HttpContext> _httpContext;
|
||||||
|
private ResponderMiddleware _middleware;
|
||||||
|
private OkResponse<HttpResponseMessage> _response;
|
||||||
|
private int _mappedStatusCode;
|
||||||
|
private List<Error> _pipelineErrors;
|
||||||
|
|
||||||
|
public ResponderMiddlewareTestsV2()
|
||||||
|
{
|
||||||
|
_responder = new Mock<IHttpResponder>();
|
||||||
|
_codeMapper = new Mock<IErrorsToHttpStatusCodeMapper>();
|
||||||
|
_next = new Mock<RequestDelegate>();
|
||||||
|
_logger = new Mock<IOcelotLogger>();
|
||||||
|
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
||||||
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||||
|
_httpContext = new Mock<HttpContext>();
|
||||||
|
|
||||||
|
_loggerFactory
|
||||||
|
.Setup(lf => lf.CreateLogger<ResponderMiddleware>())
|
||||||
|
.Returns(_logger.Object);
|
||||||
|
|
||||||
|
_middleware = new ResponderMiddleware(_next.Object, _responder.Object, _loggerFactory.Object, _scopedRepository.Object, _codeMapper.Object);
|
||||||
|
|
||||||
|
GivenTheHttpResponseMessageIs(new HttpResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NoPipelineErrors()
|
||||||
|
{
|
||||||
|
this.Given(x => x.GivenThereAreNoPipelineErrors())
|
||||||
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
.Then(_ => ThenTheNextMiddlewareIsCalled())
|
||||||
|
.And(x => x.ThenThereAreNoErrorsOnTheHttpContext())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void PipelineErrors()
|
||||||
|
{
|
||||||
|
this.Given(_ => GivenThereArePipelineErrors())
|
||||||
|
.And(_ => GivenTheErrorsCanBeMappedToAStatusCode())
|
||||||
|
.When(_ => WhenICallTheMiddleware())
|
||||||
|
.Then(_ => ThenTheNextMiddlewareIsCalled())
|
||||||
|
.And(x => x.ThenTheErrorsAreLogged())
|
||||||
|
.And(_ => ThenTheErrorsAreMappedToAnHttpStatus())
|
||||||
|
.And(_ => ThenAnErrorResponseIsSetOnTheHttpContext())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheHttpResponseMessageIs(HttpResponseMessage response)
|
||||||
|
{
|
||||||
|
_response = new OkResponse<HttpResponseMessage>(response);
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<HttpResponseMessage>(It.IsAny<string>()))
|
||||||
|
.Returns(_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThereAreNoPipelineErrors()
|
||||||
|
{
|
||||||
|
GivenThereArePipelineErrors(new List<Error>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThereArePipelineErrors()
|
||||||
|
{
|
||||||
|
GivenThereArePipelineErrors(new List<Error>() { new AnyError() });
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThereArePipelineErrors(List<Error> pipelineErrors)
|
||||||
|
{
|
||||||
|
_pipelineErrors = pipelineErrors;
|
||||||
|
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<bool>("OcelotMiddlewareError"))
|
||||||
|
.Returns(new OkResponse<bool>(_pipelineErrors.Count != 0));
|
||||||
|
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(sr => sr.Get<List<Error>>("OcelotMiddlewareErrors"))
|
||||||
|
.Returns(new OkResponse<List<Error>>(_pipelineErrors));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheErrorsCanBeMappedToAStatusCode()
|
||||||
|
{
|
||||||
|
_mappedStatusCode = 500; //TODO: autofixture
|
||||||
|
_codeMapper.Setup(cm => cm.Map(It.IsAny<List<Error>>()))
|
||||||
|
.Returns(_mappedStatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICallTheMiddleware()
|
||||||
|
{
|
||||||
|
_middleware.Invoke(_httpContext.Object).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheNextMiddlewareIsCalled()
|
||||||
|
{
|
||||||
|
_next.Verify(n => n(_httpContext.Object), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheErrorsAreMappedToAnHttpStatus()
|
||||||
|
{
|
||||||
|
_codeMapper.Verify(cm => cm.Map(_pipelineErrors), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheErrorsAreLogged()
|
||||||
|
{
|
||||||
|
_logger.Verify(l => l.LogError($"{_pipelineErrors.Count} pipeline errors found in ResponderMiddleware. Setting error response status code"), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenThereAreNoErrorsOnTheHttpContext()
|
||||||
|
{
|
||||||
|
_responder.Verify(r => r.SetErrorResponseOnContext(It.IsAny<HttpContext>(), It.IsAny<int>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenAnErrorResponseIsSetOnTheHttpContext()
|
||||||
|
{
|
||||||
|
_responder.Verify(r => r.SetErrorResponseOnContext(_httpContext.Object, _mappedStatusCode), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user