diff --git a/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs new file mode 100644 index 00000000..e3264653 --- /dev/null +++ b/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs @@ -0,0 +1,89 @@ +namespace Ocelot.UnitTests.Middleware +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Net.Http; + using Library.Infrastructure.DownstreamRouteFinder; + using Library.Infrastructure.Repository; + using Library.Infrastructure.Responses; + using Library.Infrastructure.UrlMatcher; + using Library.Middleware; + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.TestHost; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using TestStack.BDDfy; + using Xunit; + + 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(_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 happy_path() + { + this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List(), "any old string"))) + .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())) + .Returns(_downstreamRoute); + } + + public void Dispose() + { + _client.Dispose(); + _server.Dispose(); + } + } +} diff --git a/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs new file mode 100644 index 00000000..b607cea7 --- /dev/null +++ b/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs @@ -0,0 +1,100 @@ +namespace Ocelot.UnitTests.Middleware +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Net.Http; + using Library.Infrastructure.DownstreamRouteFinder; + using Library.Infrastructure.Repository; + using Library.Infrastructure.Responses; + using Library.Infrastructure.UrlMatcher; + using Library.Infrastructure.UrlTemplateReplacer; + using Library.Middleware; + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.TestHost; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using TestStack.BDDfy; + using Xunit; + + public class DownstreamUrlCreatorMiddlewareTests : IDisposable + { + private readonly Mock _downstreamUrlTemplateVariableReplacer; + private readonly Mock _scopedRepository; + private readonly string _url; + private readonly TestServer _server; + private readonly HttpClient _client; + private Response _downstreamRoute; + private HttpResponseMessage _result; + private OkResponse _downstreamUrl; + + public DownstreamUrlCreatorMiddlewareTests() + { + _url = "http://localhost:51879"; + _downstreamUrlTemplateVariableReplacer = new Mock(); + _scopedRepository = new Mock(); + + var builder = new WebHostBuilder() + .ConfigureServices(x => + { + x.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object); + x.AddSingleton(_scopedRepository.Object); + }) + .UseUrls(_url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseUrls(_url) + .Configure(app => + { + app.UseDownstreamUrlCreatorMiddleware(); + }); + + _server = new TestServer(builder); + _client = _server.CreateClient(); + } + + [Fact] + public void happy_path() + { + this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List(), "any old string"))) + .And(x => x.TheUrlReplacerReturns("any old string")) + .When(x => x.WhenICallTheMiddleware()) + .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) + .BDDfy(); + } + + private void TheUrlReplacerReturns(string downstreamUrl) + { + _downstreamUrl = new OkResponse(downstreamUrl); + _downstreamUrlTemplateVariableReplacer + .Setup(x => x.ReplaceTemplateVariables(It.IsAny())) + .Returns(_downstreamUrl); + } + + private void ThenTheScopedDataRepositoryIsCalledCorrectly() + { + _scopedRepository + .Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once()); + } + + private void WhenICallTheMiddleware() + { + _result = _client.GetAsync(_url).Result; + } + + private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute) + { + _downstreamRoute = new OkResponse(downstreamRoute); + _scopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(_downstreamRoute); + } + + public void Dispose() + { + _client.Dispose(); + _server.Dispose(); + } + } +} diff --git a/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs new file mode 100644 index 00000000..1f38238b --- /dev/null +++ b/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs @@ -0,0 +1,102 @@ +/* +namespace Ocelot.UnitTests.Middleware +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Net.Http; + using Library.Infrastructure.DownstreamRouteFinder; + using Library.Infrastructure.Repository; + using Library.Infrastructure.RequestBuilder; + using Library.Infrastructure.Responses; + using Library.Infrastructure.UrlMatcher; + using Library.Infrastructure.UrlTemplateReplacer; + using Library.Middleware; + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.TestHost; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using TestStack.BDDfy; + using Xunit; + + public class HttpRequestBuilderMiddlewareTests : IDisposable + { + private readonly Mock _requestBuilder; + private readonly Mock _scopedRepository; + private readonly string _url; + private readonly TestServer _server; + private readonly HttpClient _client; + private HttpResponseMessage _result; + private OkResponse _downstreamUrl; + + public HttpRequestBuilderMiddlewareTests() + { + _url = "http://localhost:51879"; + _requestBuilder = new Mock(); + _scopedRepository = new Mock(); + + var builder = new WebHostBuilder() + .ConfigureServices(x => + { + x.AddSingleton(_requestBuilder.Object); + x.AddSingleton(_scopedRepository.Object); + }) + .UseUrls(_url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseUrls(_url) + .Configure(app => + { + app.UseHttpRequestBuilderMiddleware(); + }); + + _server = new TestServer(builder); + _client = _server.CreateClient(); + } + + [Fact] + public void happy_path() + { + this.Given(x => x.GivenTheDownStreamUrlIs(new DownstreamRoute(new List(), "any old string"))) + .And(x => x.GivenTheRequestBuilderReturns("any old string")) + .When(x => x.WhenICallTheMiddleware()) + .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) + .BDDfy(); + } + + private void GivenTheRequestBuilderReturns(Response request) + { + _downstreamUrl = new OkResponse(downstreamUrl); + _downstreamUrlTemplateVariableReplacer + .Setup(x => x.ReplaceTemplateVariables(It.IsAny())) + .Returns(_downstreamUrl); + } + + private void ThenTheScopedDataRepositoryIsCalledCorrectly() + { + _scopedRepository + .Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once()); + } + + private void WhenICallTheMiddleware() + { + _result = _client.GetAsync(_url).Result; + } + + private void GivenTheDownStreamUrlIs(string downstreamRoute) + { + _downstreamUrl = new OkResponse(downstreamRoute); + _scopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(_downstreamUrl); + } + + public void Dispose() + { + _client.Dispose(); + _server.Dispose(); + } + } +} +*/