using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Moq; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; namespace Ocelot.UnitTests.DownstreamRouteFinder { public class DownstreamRouteFinderMiddlewareTests : IDisposable { private readonly Mock _downstreamRouteFinder; private readonly Mock _scopedRepository; private readonly string _url; private readonly TestServer _server; private readonly HttpClient _client; private Response _downstreamRoute; private HttpResponseMessage _result; public DownstreamRouteFinderMiddlewareTests() { _url = "http://localhost:51879"; _downstreamRouteFinder = new Mock(); _scopedRepository = new Mock(); var builder = new WebHostBuilder() .ConfigureServices(x => { x.AddSingleton(); x.AddLogging(); x.AddSingleton(_downstreamRouteFinder.Object); x.AddSingleton(_scopedRepository.Object); }) .UseUrls(_url) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseUrls(_url) .Configure(app => { app.UseDownstreamRouteFinderMiddleware(); }); _server = new TestServer(builder); _client = _server.CreateClient(); } [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("Get") .Build()))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) .BDDfy(); } private void ThenTheScopedDataRepositoryIsCalledCorrectly() { _scopedRepository .Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once()); } private void WhenICallTheMiddleware() { _result = _client.GetAsync(_url).Result; } private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute) { _downstreamRoute = new OkResponse(downstreamRoute); _downstreamRouteFinder .Setup(x => x.FindDownstreamRoute(It.IsAny(), It.IsAny())) .ReturnsAsync(_downstreamRoute); } public void Dispose() { _client.Dispose(); _server.Dispose(); } } }