using Microsoft.AspNetCore.Http; namespace Ocelot.UnitTests.DownstreamPathManipulation { using Ocelot.DownstreamPathManipulation.Middleware; using Ocelot.Infrastructure.RequestData; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Logging; using Ocelot.Middleware; using Ocelot.PathManipulation; using Ocelot.Request.Middleware; using Ocelot.Responses; using Ocelot.Values; using System.Collections.Generic; using System.Net.Http; using System.Security.Claims; using System.Threading.Tasks; using TestStack.BDDfy; using Xunit; using Ocelot.DownstreamRouteFinder.Middleware; public class ClaimsToDownstreamPathMiddlewareTests { private readonly Mock _changePath; private Mock _loggerFactory; private Mock _logger; private ClaimsToDownstreamPathMiddleware _middleware; private RequestDelegate _next; private HttpContext _httpContext; public ClaimsToDownstreamPathMiddlewareTests() { _httpContext = new DefaultHttpContext(); _loggerFactory = new Mock(); _logger = new Mock(); _loggerFactory.Setup(x => x.CreateLogger()).Returns(_logger.Object); _next = context => Task.CompletedTask; _changePath = new Mock(); _httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"))); _middleware = new ClaimsToDownstreamPathMiddleware(_next, _loggerFactory.Object, _changePath.Object); } [Fact] public void should_call_add_queries_correctly() { var downstreamRoute = new DownstreamRoute(new List(), new ReRouteBuilder() .WithDownstreamReRoute(new DownstreamReRouteBuilder() .WithDownstreamPathTemplate("any old string") .WithClaimsToDownstreamPath(new List { new ClaimToThing("UserId", "Subject", "", 0), }) .WithUpstreamHttpMethod(new List { "Get" }) .Build()) .WithUpstreamHttpMethod(new List { "Get" }) .Build()); this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute)) .And(x => x.GivenTheChangeDownstreamPathReturnsOk()) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenChangeDownstreamPathIsCalledCorrectly()) .BDDfy(); } private void WhenICallTheMiddleware() { _middleware.Invoke(_httpContext).GetAwaiter().GetResult(); } private void GivenTheChangeDownstreamPathReturnsOk() { _changePath .Setup(x => x.ChangeDownstreamPath( It.IsAny>(), It.IsAny>(), It.IsAny(), It.IsAny>())) .Returns(new OkResponse()); } private void ThenChangeDownstreamPathIsCalledCorrectly() { _changePath .Verify(x => x.ChangeDownstreamPath( It.IsAny>(), It.IsAny>(), _httpContext.Items.DownstreamReRoute().DownstreamPathTemplate, _httpContext.Items.TemplatePlaceholderNameAndValues()), Times.Once); } private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute) { _httpContext.Items.UpsertTemplatePlaceholderNameAndValues(downstreamRoute.TemplatePlaceholderNameAndValues); _httpContext.Items.UpsertDownstreamReRoute(downstreamRoute.ReRoute.DownstreamReRoute[0]); } } }