diff --git a/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs b/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs index 67f11815..1d745c1c 100644 --- a/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs +++ b/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs @@ -12,6 +12,7 @@ private readonly RequestDelegate _next; private readonly IScopedRequestDataRepository _scopedRequestDataRepository; private readonly IRouteRequiresAuthentication _requiresAuthentication; + public AuthenticationMiddleware(RequestDelegate next, IScopedRequestDataRepository scopedRequestDataRepository, IRouteRequiresAuthentication requiresAuthentication) diff --git a/src/Ocelot.Library/Middleware/OcelotMiddleware.cs b/src/Ocelot.Library/Middleware/OcelotMiddleware.cs index f20daa96..dda41207 100644 --- a/src/Ocelot.Library/Middleware/OcelotMiddleware.cs +++ b/src/Ocelot.Library/Middleware/OcelotMiddleware.cs @@ -4,11 +4,11 @@ using Ocelot.Library.Infrastructure.Responses; namespace Ocelot.Library.Middleware { - public class OcelotMiddleware + public abstract class OcelotMiddleware { private readonly IScopedRequestDataRepository _scopedRequestDataRepository; - public OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository) + protected OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository) { _scopedRequestDataRepository = scopedRequestDataRepository; } diff --git a/src/Ocelot.Library/Middleware/RequestLoggerExtensions.cs b/src/Ocelot.Library/Middleware/RequestLoggerExtensions.cs deleted file mode 100644 index 0d1a0027..00000000 --- a/src/Ocelot.Library/Middleware/RequestLoggerExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.AspNetCore.Builder; - -namespace Ocelot.Library.Middleware -{ - public static class RequestLoggerExtensions - { - public static IApplicationBuilder UseRequestLogger(this IApplicationBuilder builder) - { - return builder.UseMiddleware(); - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Middleware/RequestLoggerMiddleware.cs b/src/Ocelot.Library/Middleware/RequestLoggerMiddleware.cs deleted file mode 100644 index 554433b2..00000000 --- a/src/Ocelot.Library/Middleware/RequestLoggerMiddleware.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; - -namespace Ocelot.Library.Middleware -{ - public class RequestLoggerMiddleware - { - private readonly RequestDelegate _next; - private readonly ILogger _logger; - - public RequestLoggerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory) - { - _next = next; - _logger = loggerFactory.CreateLogger(); - } - - public async Task Invoke(HttpContext context) - { - _logger.LogInformation("Handling request: " + context.Request.Path); - await _next.Invoke(context); - _logger.LogInformation("Finished handling request."); - } - } -} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs new file mode 100644 index 00000000..4b1d2e07 --- /dev/null +++ b/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs @@ -0,0 +1,98 @@ +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 Moq; +using Ocelot.Library.Infrastructure.Authentication; +using Ocelot.Library.Infrastructure.DownstreamRouteFinder; +using Ocelot.Library.Infrastructure.Repository; +using Ocelot.Library.Infrastructure.Responses; +using Ocelot.Library.Infrastructure.UrlMatcher; +using Ocelot.Library.Middleware; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Middleware +{ + public class AuthenticationMiddlewareTests : IDisposable + { + private readonly Mock _requiresAuth; + private readonly Mock _scopedRepository; + private readonly string _url; + private readonly TestServer _server; + private readonly HttpClient _client; + private HttpResponseMessage _result; + private OkResponse _downstreamRoute; + + public AuthenticationMiddlewareTests() + { + _url = "http://localhost:51879"; + _requiresAuth = new Mock(); + _scopedRepository = new Mock(); + + var builder = new WebHostBuilder() + .ConfigureServices(x => + { + x.AddSingleton(_requiresAuth.Object); + x.AddSingleton(_scopedRepository.Object); + }) + .UseUrls(_url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseUrls(_url) + .Configure(app => + { + app.UseAuthenticationMiddleware(); + }); + + _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.GivenTheRouteIsNotAuthenticated()) + .When(x => x.WhenICallTheMiddleware()) + .Then(x => x.ThenNoExceptionsAreThrown()) + .BDDfy(); + } + + private void ThenNoExceptionsAreThrown() + { + //todo not suck + } + + private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute) + { + _downstreamRoute = new OkResponse(downstreamRoute); + _scopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(_downstreamRoute); + } + + private void GivenTheRouteIsNotAuthenticated() + { + _requiresAuth + .Setup(x => x.IsAuthenticated(It.IsAny(), It.IsAny())) + .Returns(new OkResponse(false)); + } + + private void WhenICallTheMiddleware() + { + _result = _client.GetAsync(_url).Result; + } + + + public void Dispose() + { + _client.Dispose(); + _server.Dispose(); + } + } +} diff --git a/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs index 1f38238b..56142b70 100644 --- a/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Middleware/HttpRequestBuilderMiddlewareTests.cs @@ -1,24 +1,21 @@ -/* +using System; +using System.IO; +using System.Net; +using System.Net.Http; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using Ocelot.Library.Infrastructure.Repository; +using Ocelot.Library.Infrastructure.RequestBuilder; +using Ocelot.Library.Infrastructure.Responses; +using Ocelot.Library.Middleware; +using TestStack.BDDfy; +using Xunit; + 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; @@ -27,6 +24,7 @@ namespace Ocelot.UnitTests.Middleware private readonly TestServer _server; private readonly HttpClient _client; private HttpResponseMessage _result; + private OkResponse _request; private OkResponse _downstreamUrl; public HttpRequestBuilderMiddlewareTests() @@ -58,25 +56,26 @@ namespace Ocelot.UnitTests.Middleware [Fact] public void happy_path() { - this.Given(x => x.GivenTheDownStreamUrlIs(new DownstreamRoute(new List(), "any old string"))) - .And(x => x.GivenTheRequestBuilderReturns("any old string")) + this.Given(x => x.GivenTheDownStreamUrlIs("any old string")) + .And(x => x.GivenTheRequestBuilderReturns(new Request(new HttpRequestMessage(), new CookieContainer()))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) .BDDfy(); } - private void GivenTheRequestBuilderReturns(Response request) + private void GivenTheRequestBuilderReturns(Request request) { - _downstreamUrl = new OkResponse(downstreamUrl); - _downstreamUrlTemplateVariableReplacer - .Setup(x => x.ReplaceTemplateVariables(It.IsAny())) - .Returns(_downstreamUrl); + _request = new OkResponse(request); + _requestBuilder + .Setup(x => x.Build(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(_request); } private void ThenTheScopedDataRepositoryIsCalledCorrectly() { _scopedRepository - .Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once()); + .Verify(x => x.Add("Request", _request.Data), Times.Once()); } private void WhenICallTheMiddleware() @@ -84,9 +83,9 @@ namespace Ocelot.UnitTests.Middleware _result = _client.GetAsync(_url).Result; } - private void GivenTheDownStreamUrlIs(string downstreamRoute) + private void GivenTheDownStreamUrlIs(string downstreamUrl) { - _downstreamUrl = new OkResponse(downstreamRoute); + _downstreamUrl = new OkResponse(downstreamUrl); _scopedRepository .Setup(x => x.Get(It.IsAny())) .Returns(_downstreamUrl); @@ -99,4 +98,3 @@ namespace Ocelot.UnitTests.Middleware } } } -*/ diff --git a/test/Ocelot.UnitTests/Middleware/HttpRequesterMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/HttpRequesterMiddlewareTests.cs new file mode 100644 index 00000000..ecb62b19 --- /dev/null +++ b/test/Ocelot.UnitTests/Middleware/HttpRequesterMiddlewareTests.cs @@ -0,0 +1,99 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Http; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using Ocelot.Library.Infrastructure.Repository; +using Ocelot.Library.Infrastructure.RequestBuilder; +using Ocelot.Library.Infrastructure.Requester; +using Ocelot.Library.Infrastructure.Responses; +using Ocelot.Library.Middleware; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Middleware +{ + public class HttpRequesterMiddlewareTests : IDisposable + { + private readonly Mock _requester; + private readonly Mock _scopedRepository; + private readonly string _url; + private readonly TestServer _server; + private readonly HttpClient _client; + private HttpResponseMessage _result; + private OkResponse _response; + private OkResponse _request; + + public HttpRequesterMiddlewareTests() + { + _url = "http://localhost:51879"; + _requester = new Mock(); + _scopedRepository = new Mock(); + + var builder = new WebHostBuilder() + .ConfigureServices(x => + { + x.AddSingleton(_requester.Object); + x.AddSingleton(_scopedRepository.Object); + }) + .UseUrls(_url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseUrls(_url) + .Configure(app => + { + app.UseHttpRequesterMiddleware(); + }); + + _server = new TestServer(builder); + _client = _server.CreateClient(); + } + + [Fact] + public void happy_path() + { + this.Given(x => x.GivenTheRequestIs(new Request(new HttpRequestMessage(),new CookieContainer()))) + .And(x => x.GivenTheRequesterReturns(new HttpResponseMessage())) + .When(x => x.WhenICallTheMiddleware()) + .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) + .BDDfy(); + } + + private void GivenTheRequesterReturns(HttpResponseMessage response) + { + _response = new OkResponse(response); + _requester + .Setup(x => x.GetResponse(It.IsAny())) + .ReturnsAsync(_response); + } + + private void ThenTheScopedDataRepositoryIsCalledCorrectly() + { + _scopedRepository + .Verify(x => x.Add("Response", _response.Data), Times.Once()); + } + + private void WhenICallTheMiddleware() + { + _result = _client.GetAsync(_url).Result; + } + + private void GivenTheRequestIs(Request request) + { + _request = new OkResponse(request); + _scopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(_request); + } + + public void Dispose() + { + _client.Dispose(); + _server.Dispose(); + } + } +} diff --git a/test/Ocelot.UnitTests/Middleware/HttpResponderMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/HttpResponderMiddlewareTests.cs new file mode 100644 index 00000000..d565dbf8 --- /dev/null +++ b/test/Ocelot.UnitTests/Middleware/HttpResponderMiddlewareTests.cs @@ -0,0 +1,96 @@ +using System; +using System.IO; +using System.Net.Http; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using Ocelot.Library.Infrastructure.Repository; +using Ocelot.Library.Infrastructure.Responder; +using Ocelot.Library.Infrastructure.Responses; +using Ocelot.Library.Middleware; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Middleware +{ + public class HttpResponderMiddlewareTests : IDisposable + { + private readonly Mock _responder; + private readonly Mock _scopedRepository; + private readonly string _url; + private readonly TestServer _server; + private readonly HttpClient _client; + private HttpResponseMessage _result; + private OkResponse _response; + + public HttpResponderMiddlewareTests() + { + _url = "http://localhost:51879"; + _responder = new Mock(); + _scopedRepository = new Mock(); + + var builder = new WebHostBuilder() + .ConfigureServices(x => + { + x.AddSingleton(_responder.Object); + x.AddSingleton(_scopedRepository.Object); + }) + .UseUrls(_url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseUrls(_url) + .Configure(app => + { + app.UseHttpResponderMiddleware(); + }); + + _server = new TestServer(builder); + _client = _server.CreateClient(); + } + + [Fact] + public void happy_path() + { + this.Given(x => x.GivenTheHttpResponseMessageIs(new HttpResponseMessage())) + .And(x => x.GivenThereAreNoPipelineErrors()) + .When(x => x.WhenICallTheMiddleware()) + .Then(x => x.TheResponderIsCalledCorrectly()) + .BDDfy(); + } + + private void GivenThereAreNoPipelineErrors() + { + _scopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(new OkResponse(false)); + } + + private void TheResponderIsCalledCorrectly() + { + _responder + .Verify(x => x.CreateResponse(It.IsAny(), _response.Data), Times.Once); + } + + private void WhenICallTheMiddleware() + { + _result = _client.GetAsync(_url).Result; + } + + private void GivenTheHttpResponseMessageIs(HttpResponseMessage response) + { + _response = new OkResponse(response); + _scopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(_response); + } + + public void Dispose() + { + _client.Dispose(); + _server.Dispose(); + } + } +}