diff --git a/README.md b/README.md index 4a47ff43..06422a2d 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ to do this. ## How to install -Ocelot is designed to work with ASP.NET core only and is currently built to .NET Standard 1.4 [this](https://docs.microsoft.com/en-us/dotnet/articles/standard/library) documentation may prove helpful when working out if Ocelot would be suitable for you. +Ocelot is designed to work with ASP.NET core only and is currently built to netcoreapp1.4 [this](https://docs.microsoft.com/en-us/dotnet/articles/standard/library) documentation may prove helpful when working out if Ocelot would be suitable for you. Install Ocelot and it's dependecies using nuget. At the moment all we have is the pre version. Once we have something working in a half decent way we will drop a version. @@ -79,7 +79,8 @@ This is pretty much all you need to get going.......more to come! ## Logging -Ocelot uses the standard logging interfaces ILoggerFactory / ILogger as such you can use any logging provider you like such as default, nlog, serilog or whatever you want. +Ocelot uses the standard logging interfaces ILoggerFactory / ILogger at the moment. This is encapsulated in IOcelotLogger with +an implementation for the standard asp.net core logging stuff at the moment. ## Caching diff --git a/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs b/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs index 39d70e38..e683b6ba 100644 --- a/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs +++ b/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs @@ -2,10 +2,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; using Ocelot.Authentication.Handler.Factory; using Ocelot.Configuration; using Ocelot.Errors; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Middleware; namespace Ocelot.Authentication.Middleware @@ -15,45 +17,64 @@ namespace Ocelot.Authentication.Middleware private readonly RequestDelegate _next; private readonly IApplicationBuilder _app; private readonly IAuthenticationHandlerFactory _authHandlerFactory; + private readonly IOcelotLogger _logger; - public AuthenticationMiddleware(RequestDelegate next, + public AuthenticationMiddleware(RequestDelegate next, IApplicationBuilder app, - IRequestScopedDataRepository requestScopedDataRepository, - IAuthenticationHandlerFactory authHandlerFactory) + IRequestScopedDataRepository requestScopedDataRepository, + IAuthenticationHandlerFactory authHandlerFactory, + IOcelotLoggerFactory loggerFactory) : base(requestScopedDataRepository) { _next = next; _authHandlerFactory = authHandlerFactory; _app = app; + _logger = loggerFactory.CreateLogger(); } public async Task Invoke(HttpContext context) { + _logger.LogDebug("started authentication"); + if (IsAuthenticatedRoute(DownstreamRoute.ReRoute)) { var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions); if (!authenticationHandler.IsError) { + _logger.LogDebug("calling authentication handler for ReRoute"); + await authenticationHandler.Data.Handler.Invoke(context); } else { + _logger.LogDebug("there was an error getting authentication handler for ReRoute"); + SetPipelineError(authenticationHandler.Errors); } if (context.User.Identity.IsAuthenticated) { + _logger.LogDebug("the user was authenticated"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); } else - { - SetPipelineError(new List {new UnauthenticatedError($"Request for authenticated route {context.Request.Path} by {context.User.Identity.Name} was unauthenticated")}); - } + { + _logger.LogDebug("the user was not authenticated"); + + SetPipelineError(new List { new UnauthenticatedError($"Request for authenticated route {context.Request.Path} by {context.User.Identity.Name} was unauthenticated") }); + } } else { + _logger.LogDebug("calling next middleware"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); } } diff --git a/src/Ocelot/Authorisation/Middleware/AuthorisationMiddleware.cs b/src/Ocelot/Authorisation/Middleware/AuthorisationMiddleware.cs index 574cd431..547fc7f7 100644 --- a/src/Ocelot/Authorisation/Middleware/AuthorisationMiddleware.cs +++ b/src/Ocelot/Authorisation/Middleware/AuthorisationMiddleware.cs @@ -1,4 +1,6 @@ -using Ocelot.Infrastructure.RequestData; +using Microsoft.Extensions.Logging; +using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Responses; namespace Ocelot.Authorisation.Middleware @@ -13,34 +15,49 @@ namespace Ocelot.Authorisation.Middleware { private readonly RequestDelegate _next; private readonly IAuthoriser _authoriser; + private readonly IOcelotLogger _logger; public AuthorisationMiddleware(RequestDelegate next, IRequestScopedDataRepository requestScopedDataRepository, - IAuthoriser authoriser) + IAuthoriser authoriser, + IOcelotLoggerFactory loggerFactory) : base(requestScopedDataRepository) { _next = next; _authoriser = authoriser; + _logger = loggerFactory.CreateLogger(); } public async Task Invoke(HttpContext context) { + _logger.LogDebug("started authorisation"); + if (DownstreamRoute.ReRoute.IsAuthorised) { + _logger.LogDebug("route is authorised"); + var authorised = _authoriser.Authorise(context.User, DownstreamRoute.ReRoute.RouteClaimsRequirement); if (authorised.IsError) { + _logger.LogDebug("error authorising user"); + SetPipelineError(authorised.Errors); return; } if (IsAuthorised(authorised)) { + _logger.LogDebug("user is authorised calling next middleware"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); } else { + _logger.LogDebug("user is not authorised setting pipeline error"); + SetPipelineError(new List { new UnauthorisedError( @@ -50,7 +67,11 @@ namespace Ocelot.Authorisation.Middleware } else { + _logger.LogDebug("AuthorisationMiddleware.Invoke route is not authorised calling next middleware"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); } } diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 7fbf6424..0e8a2b47 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Middleware; namespace Ocelot.Cache.Middleware @@ -11,11 +12,11 @@ namespace Ocelot.Cache.Middleware public class OutputCacheMiddleware : OcelotMiddleware { private readonly RequestDelegate _next; - private readonly ILogger _logger; + private readonly IOcelotLogger _logger; private readonly IOcelotCache _outputCache; - public OutputCacheMiddleware(RequestDelegate next, - ILoggerFactory loggerFactory, + public OutputCacheMiddleware(RequestDelegate next, + IOcelotLoggerFactory loggerFactory, IRequestScopedDataRepository scopedDataRepository, IOcelotCache outputCache) :base(scopedDataRepository) @@ -35,26 +36,30 @@ namespace Ocelot.Cache.Middleware return; } - _logger.LogDebug("OutputCacheMiddleware.Invoke stared checking cache for downstreamUrlKey", downstreamUrlKey); + _logger.LogDebug("started checking cache for {downstreamUrlKey}", downstreamUrlKey); var cached = _outputCache.Get(downstreamUrlKey); if (cached != null) { + _logger.LogDebug("cache entry exists for {downstreamUrlKey}", downstreamUrlKey); + SetHttpResponseMessageThisRequest(cached); - _logger.LogDebug("OutputCacheMiddleware.Invoke finished returned cached response for downstreamUrlKey", downstreamUrlKey); + _logger.LogDebug("finished returned cached response for {downstreamUrlKey}", downstreamUrlKey); return; } - _logger.LogDebug("OutputCacheMiddleware.Invoke no resonse cached for downstreamUrlKey", downstreamUrlKey); + _logger.LogDebug("no resonse cached for {downstreamUrlKey}", downstreamUrlKey); await _next.Invoke(context); + _logger.LogDebug("succesfully called next middleware"); + if (PipelineError()) { - _logger.LogDebug("OutputCacheMiddleware.Invoke there was a pipeline error for downstreamUrlKey", downstreamUrlKey); + _logger.LogDebug("there was a pipeline error for {downstreamUrlKey}", downstreamUrlKey); return; } @@ -63,7 +68,7 @@ namespace Ocelot.Cache.Middleware _outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds)); - _logger.LogDebug("OutputCacheMiddleware.Invoke finished response added to cache for downstreamUrlKey", downstreamUrlKey); + _logger.LogDebug("finished response added to cache for {downstreamUrlKey}", downstreamUrlKey); } } } diff --git a/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs b/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs index 488fbadd..1b1745e2 100644 --- a/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs +++ b/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Middleware; namespace Ocelot.Claims.Middleware @@ -10,30 +11,43 @@ namespace Ocelot.Claims.Middleware { private readonly RequestDelegate _next; private readonly IAddClaimsToRequest _addClaimsToRequest; + private readonly IOcelotLogger _logger; public ClaimsBuilderMiddleware(RequestDelegate next, IRequestScopedDataRepository requestScopedDataRepository, + IOcelotLoggerFactory loggerFactory, IAddClaimsToRequest addClaimsToRequest) : base(requestScopedDataRepository) { _next = next; _addClaimsToRequest = addClaimsToRequest; + _logger = loggerFactory.CreateLogger(); } public async Task Invoke(HttpContext context) { + _logger.LogDebug("started claims middleware"); + if (DownstreamRoute.ReRoute.ClaimsToClaims.Any()) { + _logger.LogDebug("this route has instructions to convert claims to other claims"); + var result = _addClaimsToRequest.SetClaimsOnContext(DownstreamRoute.ReRoute.ClaimsToClaims, context); if (result.IsError) { + _logger.LogDebug("error converting claims to other claims, setting pipeline error"); + SetPipelineError(result.Errors); return; } } - + + _logger.LogDebug("calling next middleware"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); } } } diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index 753b93e0..04839abb 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -22,6 +22,7 @@ using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; using Ocelot.Headers; using Ocelot.Infrastructure.Claims.Parser; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.QueryStrings; using Ocelot.Request.Builder; using Ocelot.Requester; @@ -57,7 +58,7 @@ namespace Ocelot.DependencyInjection { services.AddMvcCore().AddJsonFormatters(); services.AddLogging(); - + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs b/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs index c25c655c..cdadf5a0 100644 --- a/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs +++ b/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Middleware; namespace Ocelot.DownstreamRouteFinder.Middleware @@ -10,31 +11,47 @@ namespace Ocelot.DownstreamRouteFinder.Middleware { private readonly RequestDelegate _next; private readonly IDownstreamRouteFinder _downstreamRouteFinder; + private readonly IOcelotLogger _logger; - public DownstreamRouteFinderMiddleware(RequestDelegate next, + public DownstreamRouteFinderMiddleware(RequestDelegate next, + IOcelotLoggerFactory loggerFactory, IDownstreamRouteFinder downstreamRouteFinder, IRequestScopedDataRepository requestScopedDataRepository) :base(requestScopedDataRepository) { _next = next; _downstreamRouteFinder = downstreamRouteFinder; + _logger = loggerFactory.CreateLogger(); } public async Task Invoke(HttpContext context) { + _logger.LogDebug("started calling downstream route finder middleware"); + var upstreamUrlPath = context.Request.Path.ToString(); + _logger.LogDebug("upstream url path is {upstreamUrlPath}", upstreamUrlPath); + var downstreamRoute = _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method); if (downstreamRoute.IsError) { + _logger.LogDebug("IDownstreamRouteFinder returned an error, setting pipeline error"); + SetPipelineError(downstreamRoute.Errors); return; } + _logger.LogDebug("downstream template is {downstreamRoute.Data.ReRoute.DownstreamTemplate}", downstreamRoute.Data.ReRoute.DownstreamTemplate); + SetDownstreamRouteForThisRequest(downstreamRoute.Data); + _logger.LogDebug("calling next middleware"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); + } } } \ No newline at end of file diff --git a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs b/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs index 264993c3..c7eae3dc 100644 --- a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs +++ b/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Middleware; namespace Ocelot.DownstreamUrlCreator.Middleware @@ -11,29 +12,42 @@ namespace Ocelot.DownstreamUrlCreator.Middleware { private readonly RequestDelegate _next; private readonly IDownstreamUrlPathPlaceholderReplacer _urlReplacer; + private readonly IOcelotLogger _logger; - public DownstreamUrlCreatorMiddleware(RequestDelegate next, + public DownstreamUrlCreatorMiddleware(RequestDelegate next, + IOcelotLoggerFactory loggerFactory, IDownstreamUrlPathPlaceholderReplacer urlReplacer, IRequestScopedDataRepository requestScopedDataRepository) :base(requestScopedDataRepository) { _next = next; _urlReplacer = urlReplacer; + _logger = loggerFactory.CreateLogger(); } public async Task Invoke(HttpContext context) { + _logger.LogDebug("started calling downstream url creator middleware"); + var downstreamUrl = _urlReplacer.Replace(DownstreamRoute.ReRoute.DownstreamTemplate, DownstreamRoute.TemplatePlaceholderNameAndValues); if (downstreamUrl.IsError) { + _logger.LogDebug("IDownstreamUrlPathPlaceholderReplacer returned an error, setting pipeline error"); + SetPipelineError(downstreamUrl.Errors); return; } + _logger.LogDebug("downstream url is {downstreamUrl.Data.Value}", downstreamUrl.Data.Value); + SetDownstreamUrlForThisRequest(downstreamUrl.Data.Value); + _logger.LogDebug("calling next middleware"); + await _next.Invoke(context); + + _logger.LogDebug("succesfully called next middleware"); } } } \ No newline at end of file diff --git a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs new file mode 100644 index 00000000..0d989881 --- /dev/null +++ b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; + +namespace Ocelot.Errors.Middleware +{ + public class ExceptionHandlerMiddleware + { + private readonly RequestDelegate _next; + private readonly IOcelotLogger _logger; + private readonly IRequestScopedDataRepository _requestScopedDataRepository; + + public ExceptionHandlerMiddleware(RequestDelegate next, + IOcelotLoggerFactory loggerFactory, + IRequestScopedDataRepository requestScopedDataRepository) + { + _next = next; + _requestScopedDataRepository = requestScopedDataRepository; + _logger = loggerFactory.CreateLogger(); + } + + public async Task Invoke(HttpContext context) + { + try + { + _logger.LogDebug("calling middleware"); + + _requestScopedDataRepository.Add("RequestId", context.TraceIdentifier); + + await _next.Invoke(context); + + _logger.LogDebug("succesfully called middleware"); + } + catch (Exception e) + { + _logger.LogDebug("error calling middleware"); + + var message = CreateMessage(context, e); + _logger.LogError(message, e); + await SetInternalServerErrorOnResponse(context); + } + } + + private static async Task SetInternalServerErrorOnResponse(HttpContext context) + { + context.Response.StatusCode = 500; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsync("Internal Server Error"); + } + + private static string CreateMessage(HttpContext context, Exception e) + { + var message = + $"Exception caught in global error handler, exception message: {e.Message}, exception stack: {e.StackTrace}"; + + if (e.InnerException != null) + { + message = + $"{message}, inner exception message {e.InnerException.Message}, inner exception stack {e.InnerException.StackTrace}"; + } + return $"{message} RequestId: {context.TraceIdentifier}"; + } + } +} diff --git a/src/Ocelot/Middleware/ExceptionHandlerMiddlewareExtensions.cs b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddlewareExtensions.cs similarity index 90% rename from src/Ocelot/Middleware/ExceptionHandlerMiddlewareExtensions.cs rename to src/Ocelot/Errors/Middleware/ExceptionHandlerMiddlewareExtensions.cs index c2513f69..e355c7f8 100644 --- a/src/Ocelot/Middleware/ExceptionHandlerMiddlewareExtensions.cs +++ b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddlewareExtensions.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Builder; -namespace Ocelot.Middleware +namespace Ocelot.Errors.Middleware { public static class ExceptionHandlerMiddlewareExtensions { diff --git a/src/Ocelot/Logging/IOcelotLoggerFactory.cs b/src/Ocelot/Logging/IOcelotLoggerFactory.cs new file mode 100644 index 00000000..b8bcf406 --- /dev/null +++ b/src/Ocelot/Logging/IOcelotLoggerFactory.cs @@ -0,0 +1,66 @@ +using System; +using Microsoft.Extensions.Logging; +using Ocelot.Infrastructure.RequestData; + +namespace Ocelot.Logging +{ + public interface IOcelotLoggerFactory + { + IOcelotLogger CreateLogger(); + } + + public class AspDotNetLoggerFactory : IOcelotLoggerFactory + { + private readonly ILoggerFactory _loggerFactory; + private readonly IRequestScopedDataRepository _scopedDataRepository; + + public AspDotNetLoggerFactory(ILoggerFactory loggerFactory, IRequestScopedDataRepository scopedDataRepository) + { + _loggerFactory = loggerFactory; + _scopedDataRepository = scopedDataRepository; + } + + public IOcelotLogger CreateLogger() + { + var logger = _loggerFactory.CreateLogger(); + return new AspDotNetLogger(logger, _scopedDataRepository); + } + } + + public interface IOcelotLogger + { + void LogDebug(string message, params object[] args); + void LogError(string message, Exception exception); + } + + public class AspDotNetLogger : IOcelotLogger + { + private readonly ILogger _logger; + private readonly IRequestScopedDataRepository _scopedDataRepository; + + public AspDotNetLogger(ILogger logger, IRequestScopedDataRepository scopedDataRepository) + { + _logger = logger; + _scopedDataRepository = scopedDataRepository; + } + + public void LogDebug(string message, params object[] args) + { + _logger.LogDebug(GetMessageWithRequestId(message), args); + } + + public void LogError(string message, Exception exception) + { + _logger.LogError(GetMessageWithRequestId(message), exception); + } + + private string GetMessageWithRequestId(string message) + { + var requestId = _scopedDataRepository.Get("RequestId"); + + return requestId.IsError + ? $"{message} : RequestId: Error" + : $"{message} : RequestId: {requestId.Data}"; + } + } +} diff --git a/src/Ocelot/Middleware/ExceptionHandlerMiddleware.cs b/src/Ocelot/Middleware/ExceptionHandlerMiddleware.cs deleted file mode 100644 index 1ee0c93d..00000000 --- a/src/Ocelot/Middleware/ExceptionHandlerMiddleware.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; - -namespace Ocelot.Middleware -{ - public class ExceptionHandlerMiddleware - { - private readonly RequestDelegate _next; - private readonly ILogger _logger; - - public ExceptionHandlerMiddleware(RequestDelegate next, - ILoggerFactory loggerFactory) - { - _next = next; - _logger = loggerFactory.CreateLogger(); - } - - public async Task Invoke(HttpContext context) - { - try - { - await _next.Invoke(context); - } - catch (Exception e) - { - var message = CreateMessage(context, e); - _logger.LogError(new EventId(1, "Ocelot Global Error"), message, e); - context.Response.StatusCode = 500; - context.Response.ContentType = "application/json"; - await context.Response.WriteAsync("Internal Server Error"); - } - } - - private static string CreateMessage(HttpContext context, Exception e) - { - var message = - $"RequestId: {context.TraceIdentifier}, Exception caught in global error handler, exception message: {e.Message}, exception stack: {e.StackTrace}"; - - if (e.InnerException != null) - { - message = - $"{message}, inner exception message {e.InnerException.Message}, inner exception stack {e.InnerException.StackTrace}"; - } - return message; - } - } -} diff --git a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs index cbb904ce..8e85413a 100644 --- a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs +++ b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs @@ -4,6 +4,7 @@ using Ocelot.Cache.Middleware; using Ocelot.Claims.Middleware; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamUrlCreator.Middleware; +using Ocelot.Errors.Middleware; using Ocelot.Headers.Middleware; using Ocelot.QueryStrings.Middleware; using Ocelot.Request.Middleware; diff --git a/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs b/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs index 06a67d0d..3286f6e1 100644 --- a/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs @@ -8,10 +8,12 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Authentication.Handler.Factory; using Ocelot.Authentication.Middleware; +using Ocelot.Cache.Middleware; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; @@ -27,15 +29,19 @@ namespace Ocelot.UnitTests.Authentication private readonly HttpClient _client; private HttpResponseMessage _result; private OkResponse _downstreamRoute; + private Mock _mockLoggerFactory; public AuthenticationMiddlewareTests() { _url = "http://localhost:51879"; _scopedRepository = new Mock(); _authFactory = new Mock(); + SetUpLogger(); + var builder = new WebHostBuilder() .ConfigureServices(x => { + x.AddSingleton(_mockLoggerFactory.Object); x.AddSingleton(_authFactory.Object); x.AddSingleton(_scopedRepository.Object); }) @@ -62,6 +68,17 @@ namespace Ocelot.UnitTests.Authentication .BDDfy(); } + private void SetUpLogger() + { + _mockLoggerFactory = new Mock(); + + var logger = new Mock(); + + _mockLoggerFactory + .Setup(x => x.CreateLogger()) + .Returns(logger.Object); + } + private void ThenNoExceptionsAreThrown() { //todo not suck diff --git a/test/Ocelot.UnitTests/Authorization/AuthorisationMiddlewareTests.cs b/test/Ocelot.UnitTests/Authorization/AuthorisationMiddlewareTests.cs index 77a43505..e6e25057 100644 --- a/test/Ocelot.UnitTests/Authorization/AuthorisationMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Authorization/AuthorisationMiddlewareTests.cs @@ -8,10 +8,12 @@ using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Authorisation; +using Ocelot.Cache.Middleware; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; @@ -29,15 +31,19 @@ namespace Ocelot.UnitTests.Authorization private readonly HttpClient _client; private HttpResponseMessage _result; private OkResponse _downstreamRoute; + private Mock _mockLoggerFactory; public AuthorisationMiddlewareTests() { _url = "http://localhost:51879"; _scopedRepository = new Mock(); _authService = new Mock(); + SetUpLogger(); + var builder = new WebHostBuilder() .ConfigureServices(x => { + x.AddSingleton(_mockLoggerFactory.Object); x.AddSingleton(_authService.Object); x.AddSingleton(_scopedRepository.Object); }) @@ -65,6 +71,17 @@ namespace Ocelot.UnitTests.Authorization .BDDfy(); } + private void SetUpLogger() + { + _mockLoggerFactory = new Mock(); + + var logger = new Mock(); + + _mockLoggerFactory + .Setup(x => x.CreateLogger()) + .Returns(logger.Object); + } + private void GivenTheAuthServiceReturns(Response expected) { _authService diff --git a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs index 24255a18..541ef880 100644 --- a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs @@ -14,6 +14,7 @@ using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; @@ -24,7 +25,7 @@ namespace Ocelot.UnitTests.Cache { private readonly Mock> _cacheManager; private readonly Mock _scopedRepo; - + private Mock _mockLoggerFactory; private readonly string _url; private readonly TestServer _server; private readonly HttpClient _client; @@ -35,11 +36,15 @@ namespace Ocelot.UnitTests.Cache { _cacheManager = new Mock>(); _scopedRepo = new Mock(); + + SetUpLogger(); + _url = "http://localhost:51879"; var builder = new WebHostBuilder() .ConfigureServices(x => { x.AddLogging(); + x.AddSingleton(_mockLoggerFactory.Object); x.AddSingleton(_cacheManager.Object); x.AddSingleton(_scopedRepo.Object); }) @@ -80,6 +85,17 @@ namespace Ocelot.UnitTests.Cache .BDDfy(); } + private void SetUpLogger() + { + _mockLoggerFactory = new Mock(); + + var logger = new Mock(); + + _mockLoggerFactory + .Setup(x => x.CreateLogger()) + .Returns(logger.Object); + } + private void GivenTheDownstreamRouteIs() { var reRoute = new ReRouteBuilder().WithIsCached(true).WithCacheOptions(new CacheOptions(100)).Build(); diff --git a/test/Ocelot.UnitTests/Claims/ClaimsBuilderMiddlewareTests.cs b/test/Ocelot.UnitTests/Claims/ClaimsBuilderMiddlewareTests.cs index 01cb606f..8031e232 100644 --- a/test/Ocelot.UnitTests/Claims/ClaimsBuilderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Claims/ClaimsBuilderMiddlewareTests.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Moq; +using Ocelot.Cache.Middleware; using Ocelot.Claims; using Ocelot.Claims.Middleware; using Ocelot.Configuration; @@ -14,6 +15,7 @@ using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; @@ -29,15 +31,19 @@ namespace Ocelot.UnitTests.Claims private readonly HttpClient _client; private Response _downstreamRoute; private HttpResponseMessage _result; + private Mock _mockLoggerFactory; public ClaimsBuilderMiddlewareTests() { _url = "http://localhost:51879"; _scopedRepository = new Mock(); _addHeaders = new Mock(); + SetUpLogger(); + var builder = new WebHostBuilder() .ConfigureServices(x => { + x.AddSingleton(_mockLoggerFactory.Object); x.AddSingleton(_addHeaders.Object); x.AddSingleton(_scopedRepository.Object); }) @@ -74,6 +80,17 @@ namespace Ocelot.UnitTests.Claims .BDDfy(); } + private void SetUpLogger() + { + _mockLoggerFactory = new Mock(); + + var logger = new Mock(); + + _mockLoggerFactory + .Setup(x => x.CreateLogger()) + .Returns(logger.Object); + } + private void GivenTheAddClaimsToRequestReturns() { _addHeaders diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs index fc2e49e3..87eb6613 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs @@ -6,12 +6,14 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Moq; +using Ocelot.Claims.Middleware; 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; @@ -27,16 +29,19 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private readonly HttpClient _client; private Response _downstreamRoute; private HttpResponseMessage _result; + private Mock _mockLoggerFactory; public DownstreamRouteFinderMiddlewareTests() { _url = "http://localhost:51879"; _downstreamRouteFinder = new Mock(); _scopedRepository = new Mock(); + SetUpLogger(); var builder = new WebHostBuilder() .ConfigureServices(x => { + x.AddSingleton(_mockLoggerFactory.Object); x.AddSingleton(_downstreamRouteFinder.Object); x.AddSingleton(_scopedRepository.Object); }) @@ -63,6 +68,17 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .BDDfy(); } + private void SetUpLogger() + { + _mockLoggerFactory = new Mock(); + + var logger = new Mock(); + + _mockLoggerFactory + .Setup(x => x.CreateLogger()) + .Returns(logger.Object); + } + private void ThenTheScopedDataRepositoryIsCalledCorrectly() { _scopedRepository diff --git a/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs index 4039a4fb..0e9d375e 100644 --- a/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs @@ -8,10 +8,12 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; +using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; using Ocelot.Infrastructure.RequestData; +using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; @@ -28,16 +30,19 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator private Response _downstreamRoute; private HttpResponseMessage _result; private OkResponse _downstreamUrl; + private Mock _mockLoggerFactory; public DownstreamUrlCreatorMiddlewareTests() { _url = "http://localhost:51879"; _downstreamUrlTemplateVariableReplacer = new Mock(); _scopedRepository = new Mock(); + SetUpLogger(); var builder = new WebHostBuilder() .ConfigureServices(x => { + x.AddSingleton(_mockLoggerFactory.Object); x.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object); x.AddSingleton(_scopedRepository.Object); }) @@ -65,6 +70,18 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator .BDDfy(); } + + private void SetUpLogger() + { + _mockLoggerFactory = new Mock(); + + var logger = new Mock(); + + _mockLoggerFactory + .Setup(x => x.CreateLogger()) + .Returns(logger.Object); + } + private void TheUrlReplacerReturns(string downstreamUrl) { _downstreamUrl = new OkResponse(new DownstreamUrl(downstreamUrl));