Ocelot/test/Ocelot.UnitTests/Claims/ClaimsBuilderMiddlewareTests.cs
Tom Pallister fef19ddf98
Changed routing to support a catch all style (#187)
* Changed routing to support a catch all style

* refactoring placeholder tuff

* implemented simple priority in the routing
2018-01-05 21:26:15 +00:00

88 lines
3.1 KiB
C#

namespace Ocelot.UnitTests.Claims
{
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Claims;
using Ocelot.Claims.Middleware;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.DownstreamRouteFinder;
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.Logging;
using Ocelot.Responses;
using TestStack.BDDfy;
using Xunit;
public class ClaimsBuilderMiddlewareTests : ServerHostedMiddlewareTest
{
private readonly Mock<IAddClaimsToRequest> _addHeaders;
private Response<DownstreamRoute> _downstreamRoute;
public ClaimsBuilderMiddlewareTests()
{
_addHeaders = new Mock<IAddClaimsToRequest>();
GivenTheTestServerIsConfigured();
}
[Fact]
public void should_call_claims_to_request_correctly()
{
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamPathTemplate("any old string")
.WithClaimsToClaims(new List<ClaimToThing>
{
new ClaimToThing("sub", "UserType", "|", 0)
})
.WithUpstreamHttpMethod(new List<string> { "Get" })
.Build());
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.And(x => x.GivenTheAddClaimsToRequestReturns())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheClaimsToRequestIsCalledCorrectly())
.BDDfy();
}
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
{
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
services.AddLogging();
services.AddSingleton(_addHeaders.Object);
services.AddSingleton(ScopedRepository.Object);
}
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
{
app.UseClaimsBuilderMiddleware();
}
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
{
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
ScopedRepository
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
.Returns(_downstreamRoute);
}
private void GivenTheAddClaimsToRequestReturns()
{
_addHeaders
.Setup(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
It.IsAny<HttpContext>()))
.Returns(new OkResponse());
}
private void ThenTheClaimsToRequestIsCalledCorrectly()
{
_addHeaders
.Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
It.IsAny<HttpContext>()), Times.Once);
}
}
}