mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-25 21:52:50 +08:00

* hacked together load balancing reroutes in fileconfig * some renaming and refactoring * more renames * hacked away the old config json * test for issue 213 * renamed key * dont share ports * oops * updated docs * mvoed docs around * port being used
88 lines
3.2 KiB
C#
88 lines
3.2 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);
|
|
}
|
|
}
|
|
}
|