namespace Ocelot.UnitTests.DownstreamRouteFinder { using System.Collections.Generic; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; public class DownstreamRouteFinderMiddlewareTests : ServerHostedMiddlewareTest { private readonly Mock _downstreamRouteFinder; private Response _downstreamRoute; public DownstreamRouteFinderMiddlewareTests() { _downstreamRouteFinder = new Mock(); GivenTheTestServerIsConfigured(); } [Fact] public void should_call_scoped_data_repository_correctly() { this.Given(x => x.GivenTheDownStreamRouteFinderReturns( new DownstreamRoute( new List(), new ReRouteBuilder() .WithDownstreamPathTemplate("any old string") .WithUpstreamHttpMethod(new List { "Get" }) .Build()))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) .BDDfy(); } protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services) { services.AddSingleton(); services.AddLogging(); services.AddSingleton(_downstreamRouteFinder.Object); services.AddSingleton(ScopedRepository.Object); } protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app) { app.UseDownstreamRouteFinderMiddleware(); } private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute) { _downstreamRoute = new OkResponse(downstreamRoute); _downstreamRouteFinder .Setup(x => x.FindDownstreamRoute(It.IsAny(), It.IsAny())) .ReturnsAsync(_downstreamRoute); } private void ThenTheScopedDataRepositoryIsCalledCorrectly() { ScopedRepository .Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once()); } } }