diff --git a/Ocelot.nuspec b/Ocelot.nuspec deleted file mode 100644 index d236d60d..00000000 --- a/Ocelot.nuspec +++ /dev/null @@ -1,48 +0,0 @@ - - - - Ocelot - 1.0.0 - Tom Pallister - Tom Pallister - https://github.com/TomPallister/Ocelot/blob/develop/LICENSE.md - https://github.com/TomPallister/Ocelot - false - Ocelot Api Gateway - Latest Ocelot - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 6e8b16f6..00000000 --- a/appveyor.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: 1.0.{build} -configuration: -- Release -platform: Any CPU -build_script: -- build.ps1 -cache: -- '%USERPROFILE%\.nuget\packages' \ No newline at end of file diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 77024e70..948a7397 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -2,7 +2,6 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Middleware; diff --git a/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs b/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs index 1f6486f6..1b1745e2 100644 --- a/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs +++ b/src/Ocelot/Claims/Middleware/ClaimsBuilderMiddleware.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Middleware; diff --git a/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs index 382cf374..8282a11c 100644 --- a/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Net.Http; using Ocelot.Values; diff --git a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs index 8025f59d..049c0fff 100644 --- a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs @@ -56,14 +56,21 @@ namespace Ocelot.Configuration.Creator public async Task> Create() { - var config = await SetUpConfiguration(); + var config = await SetUpConfiguration(_options.Value); return new OkResponse(config); } - private async Task SetUpConfiguration() + public async Task> Create(FileConfiguration fileConfiguration) + { + var config = await SetUpConfiguration(fileConfiguration); + + return new OkResponse(config); + } + + private async Task SetUpConfiguration(FileConfiguration fileConfiguration) { - var response = _configurationValidator.IsValid(_options.Value); + var response = _configurationValidator.IsValid(fileConfiguration); if (response.Data.IsError) { diff --git a/src/Ocelot/Configuration/Creator/IOcelotConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/IOcelotConfigurationCreator.cs index 7547d91f..99d1d39d 100644 --- a/src/Ocelot/Configuration/Creator/IOcelotConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/IOcelotConfigurationCreator.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Ocelot.Configuration.File; using Ocelot.Responses; namespace Ocelot.Configuration.Creator @@ -6,5 +7,6 @@ namespace Ocelot.Configuration.Creator public interface IOcelotConfigurationCreator { Task> Create(); + Task> Create(FileConfiguration fileConfiguration); } } \ No newline at end of file diff --git a/src/Ocelot/Configuration/File/FileQoSOptions.cs b/src/Ocelot/Configuration/File/FileQoSOptions.cs index dfac3ac6..876a3fd5 100644 --- a/src/Ocelot/Configuration/File/FileQoSOptions.cs +++ b/src/Ocelot/Configuration/File/FileQoSOptions.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Ocelot.Configuration.File +namespace Ocelot.Configuration.File { public class FileQoSOptions { diff --git a/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs b/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs index 3256e44a..30ded2e9 100644 --- a/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs +++ b/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs @@ -1,10 +1,9 @@ -using System.Threading.Tasks; -using Ocelot.Responses; +using Ocelot.Responses; namespace Ocelot.Configuration.Provider { public interface IOcelotConfigurationProvider { - Task> Get(); + Response Get(); } } diff --git a/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs b/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs index 80fd5697..e416ed8e 100644 --- a/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs +++ b/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs @@ -1,6 +1,4 @@ -using System.Threading.Tasks; -using Ocelot.Configuration.Creator; -using Ocelot.Configuration.Repository; +using Ocelot.Configuration.Repository; using Ocelot.Responses; namespace Ocelot.Configuration.Provider @@ -11,16 +9,13 @@ namespace Ocelot.Configuration.Provider public class OcelotConfigurationProvider : IOcelotConfigurationProvider { private readonly IOcelotConfigurationRepository _repo; - private readonly IOcelotConfigurationCreator _creator; - public OcelotConfigurationProvider(IOcelotConfigurationRepository repo, - IOcelotConfigurationCreator creator) + public OcelotConfigurationProvider(IOcelotConfigurationRepository repo) { _repo = repo; - _creator = creator; } - public async Task> Get() + public Response Get() { var repoConfig = _repo.Get(); @@ -29,20 +24,6 @@ namespace Ocelot.Configuration.Provider return new ErrorResponse(repoConfig.Errors); } - if (repoConfig.Data == null) - { - var creatorConfig = await _creator.Create(); - - if (creatorConfig.IsError) - { - return new ErrorResponse(creatorConfig.Errors); - } - - _repo.AddOrReplace(creatorConfig.Data); - - return new OkResponse(creatorConfig.Data); - } - return new OkResponse(repoConfig.Data); } } diff --git a/src/Ocelot/Configuration/ReRoute.cs b/src/Ocelot/Configuration/ReRoute.cs index ab9405d0..42a0aa79 100644 --- a/src/Ocelot/Configuration/ReRoute.cs +++ b/src/Ocelot/Configuration/ReRoute.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Net.Http; using Ocelot.Values; diff --git a/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs b/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs new file mode 100644 index 00000000..28957cc7 --- /dev/null +++ b/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs @@ -0,0 +1,32 @@ +using System.Threading.Tasks; +using Ocelot.Configuration.Creator; +using Ocelot.Configuration.File; +using Ocelot.Configuration.Repository; +using Ocelot.Responses; + +namespace Ocelot.Configuration.Setter +{ + public class FileConfigurationSetter : IFileConfigurationSetter + { + private readonly IOcelotConfigurationRepository _configRepo; + private readonly IOcelotConfigurationCreator _configCreator; + + public FileConfigurationSetter(IOcelotConfigurationRepository configRepo, IOcelotConfigurationCreator configCreator) + { + _configRepo = configRepo; + _configCreator = configCreator; + } + + public async Task Set(FileConfiguration fileConfig) + { + var config = await _configCreator.Create(fileConfig); + + if(!config.IsError) + { + _configRepo.AddOrReplace(config.Data); + } + + return new ErrorResponse(config.Errors); + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Configuration/Setter/IFileConfigurationSetter.cs b/src/Ocelot/Configuration/Setter/IFileConfigurationSetter.cs new file mode 100644 index 00000000..8ab31d1b --- /dev/null +++ b/src/Ocelot/Configuration/Setter/IFileConfigurationSetter.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; +using Ocelot.Configuration.File; +using Ocelot.Responses; + +namespace Ocelot.Configuration.Setter +{ + public interface IFileConfigurationSetter + { + Task Set(FileConfiguration config); + } +} \ No newline at end of file diff --git a/src/Ocelot/Controllers/FileConfigurationController.cs b/src/Ocelot/Controllers/FileConfigurationController.cs index 3dc22303..a378f530 100644 --- a/src/Ocelot/Controllers/FileConfigurationController.cs +++ b/src/Ocelot/Controllers/FileConfigurationController.cs @@ -1,5 +1,8 @@ +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Ocelot.Configuration.File; +using Ocelot.Configuration.Setter; using Ocelot.Services; namespace Ocelot.Controllers @@ -8,17 +11,39 @@ namespace Ocelot.Controllers [Route("configuration")] public class FileConfigurationController : Controller { - private readonly IGetFileConfiguration _getFileConfig; + private readonly IFileConfigurationProvider _configGetter; + private readonly IFileConfigurationSetter _configSetter; - public FileConfigurationController(IGetFileConfiguration getFileConfig) + public FileConfigurationController(IFileConfigurationProvider getFileConfig, IFileConfigurationSetter configSetter) { - _getFileConfig = getFileConfig; + _configGetter = getFileConfig; + _configSetter = configSetter; } [HttpGet] public IActionResult Get() { - return new OkObjectResult(_getFileConfig.Invoke().Data); + var response = _configGetter.Get(); + + if(response.IsError) + { + return new BadRequestObjectResult(response.Errors); + } + + return new OkObjectResult(response.Data); + } + + [HttpPost] + public async Task Post(FileConfiguration fileConfiguration) + { + var response = await _configSetter.Set(fileConfiguration); + + if(response.IsError) + { + return new BadRequestObjectResult(response.Errors); + } + + return new OkObjectResult(fileConfiguration); } } } \ No newline at end of file diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index c213aa6b..43ffefe3 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -1,14 +1,12 @@ using System; using System.Collections.Generic; using System.Net.Http; -using System.Security.Claims; using CacheManager.Core; using IdentityServer4.Models; using IdentityServer4.Test; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Ocelot.Authentication.Handler.Creator; using Ocelot.Authentication.Handler.Factory; using Ocelot.Authorisation; @@ -46,7 +44,6 @@ namespace Ocelot.DependencyInjection { var cacheManagerOutputCache = CacheFactory.Build("OcelotOutputCache", settings); var ocelotCacheManager = new OcelotCacheManagerCache(cacheManagerOutputCache); - services.AddSingleton>(cacheManagerOutputCache); services.AddSingleton>(ocelotCacheManager); @@ -55,11 +52,9 @@ namespace Ocelot.DependencyInjection public static IServiceCollection AddOcelotFileConfiguration(this IServiceCollection services, IConfigurationRoot configurationRoot) { services.Configure(configurationRoot); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - return services; } @@ -116,7 +111,7 @@ namespace Ocelot.DependencyInjection .AddAuthorization() .AddJsonFormatters(); services.AddLogging(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs index 8b6447c7..6773feb8 100644 --- a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs +++ b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using Ocelot.Configuration.Provider; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Errors; @@ -22,9 +21,9 @@ namespace Ocelot.DownstreamRouteFinder.Finder _urlPathPlaceholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder; } - public async Task> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod) + public Response FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod) { - var configuration = await _configProvider.Get(); + var configuration = _configProvider.Get(); var applicableReRoutes = configuration.Data.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod.Method, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase)); diff --git a/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteFinder.cs b/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteFinder.cs index 7ae3ff79..e351ab2f 100644 --- a/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteFinder.cs +++ b/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteFinder.cs @@ -1,10 +1,9 @@ -using System.Threading.Tasks; -using Ocelot.Responses; +using Ocelot.Responses; namespace Ocelot.DownstreamRouteFinder.Finder { public interface IDownstreamRouteFinder { - Task> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod); + Response FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod); } } diff --git a/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs b/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs index e88bfde8..02e3e4f6 100644 --- a/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs +++ b/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; @@ -34,7 +33,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware _logger.LogDebug("upstream url path is {upstreamUrlPath}", upstreamUrlPath); - var downstreamRoute = await _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method); + var downstreamRoute = _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method); if (downstreamRoute.IsError) { diff --git a/src/Ocelot/DownstreamUrlCreator/IUrlBuilder.cs b/src/Ocelot/DownstreamUrlCreator/IUrlBuilder.cs index 18683e62..8a07a066 100644 --- a/src/Ocelot/DownstreamUrlCreator/IUrlBuilder.cs +++ b/src/Ocelot/DownstreamUrlCreator/IUrlBuilder.cs @@ -1,6 +1,4 @@ -using Ocelot.Configuration; -using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; -using Ocelot.Responses; +using Ocelot.Responses; using Ocelot.Values; namespace Ocelot.DownstreamUrlCreator diff --git a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs b/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs index 80365074..631e278a 100644 --- a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs +++ b/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamUrlCreatorMiddleware.cs @@ -1,12 +1,9 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; -using Ocelot.Configuration; using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Middleware; -using Ocelot.Values; namespace Ocelot.DownstreamUrlCreator.Middleware { diff --git a/src/Ocelot/DownstreamUrlCreator/UrlBuilder.cs b/src/Ocelot/DownstreamUrlCreator/UrlBuilder.cs index 2124ce3b..43a63715 100644 --- a/src/Ocelot/DownstreamUrlCreator/UrlBuilder.cs +++ b/src/Ocelot/DownstreamUrlCreator/UrlBuilder.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using Ocelot.Configuration; -using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; using Ocelot.Errors; using Ocelot.Responses; using Ocelot.Values; diff --git a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs index 6e6b511e..ea5d2c84 100644 --- a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs +++ b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs @@ -1,8 +1,6 @@ using System; -using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; diff --git a/src/Ocelot/Headers/Middleware/HttpRequestHeadersBuilderMiddleware.cs b/src/Ocelot/Headers/Middleware/HttpRequestHeadersBuilderMiddleware.cs index cc262048..a89d2ec2 100644 --- a/src/Ocelot/Headers/Middleware/HttpRequestHeadersBuilderMiddleware.cs +++ b/src/Ocelot/Headers/Middleware/HttpRequestHeadersBuilderMiddleware.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Middleware; diff --git a/src/Ocelot/Headers/RemoveOutputHeaders.cs b/src/Ocelot/Headers/RemoveOutputHeaders.cs index 7a40534b..f3020036 100644 --- a/src/Ocelot/Headers/RemoveOutputHeaders.cs +++ b/src/Ocelot/Headers/RemoveOutputHeaders.cs @@ -3,8 +3,6 @@ using Ocelot.Responses; namespace Ocelot.Headers { - using System; - public class RemoveOutputHeaders : IRemoveOutputHeaders { /// diff --git a/src/Ocelot/LoadBalancer/LoadBalancers/ILoadBalancer.cs b/src/Ocelot/LoadBalancer/LoadBalancers/ILoadBalancer.cs index 73d25d48..77dc328d 100644 --- a/src/Ocelot/LoadBalancer/LoadBalancers/ILoadBalancer.cs +++ b/src/Ocelot/LoadBalancer/LoadBalancers/ILoadBalancer.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using Ocelot.Responses; using Ocelot.Values; diff --git a/src/Ocelot/LoadBalancer/LoadBalancers/LoadBalancerHouse.cs b/src/Ocelot/LoadBalancer/LoadBalancers/LoadBalancerHouse.cs index cc6ea73b..68e42fe2 100644 --- a/src/Ocelot/LoadBalancer/LoadBalancers/LoadBalancerHouse.cs +++ b/src/Ocelot/LoadBalancer/LoadBalancers/LoadBalancerHouse.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; using Ocelot.Responses; namespace Ocelot.LoadBalancer.LoadBalancers diff --git a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs index fa113771..9aa7442f 100644 --- a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs +++ b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs @@ -20,8 +20,11 @@ namespace Ocelot.Middleware using System.Threading.Tasks; using Authorisation.Middleware; using Microsoft.AspNetCore.Http; + using Microsoft.Extensions.Options; using Ocelot.Configuration; + using Ocelot.Configuration.File; using Ocelot.Configuration.Provider; + using Ocelot.Configuration.Setter; using Ocelot.LoadBalancer.Middleware; public static class OcelotMiddlewareExtensions @@ -127,17 +130,27 @@ namespace Ocelot.Middleware private static async Task CreateConfiguration(IApplicationBuilder builder) { + var fileConfig = (IOptions)builder.ApplicationServices.GetService(typeof(IOptions)); + + var configSetter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter)); + var configProvider = (IOcelotConfigurationProvider)builder.ApplicationServices.GetService(typeof(IOcelotConfigurationProvider)); - var config = await configProvider.Get(); + var config = await configSetter.Set(fileConfig.Value); - //todo move this to config validators - if(config == null || config.Data == null || config.IsError) + if(config == null || config.IsError) { - throw new Exception("Unable to start Ocelot: configuration was invalid"); + throw new Exception("Unable to start Ocelot: configuration was not set up correctly."); } - return config.Data; + var ocelotConfiguration = configProvider.Get(); + + if(ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError) + { + throw new Exception("Unable to start Ocelot: ocelot configuration was not returned by provider."); + } + + return ocelotConfiguration.Data; } private static async Task CreateAdministrationArea(IApplicationBuilder builder) diff --git a/src/Ocelot/Properties/AssemblyInfo.cs b/src/Ocelot/Properties/AssemblyInfo.cs index ad12027e..dd8b7610 100644 --- a/src/Ocelot/Properties/AssemblyInfo.cs +++ b/src/Ocelot/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/src/Ocelot/QueryStrings/Middleware/QueryStringBuilderMiddleware.cs b/src/Ocelot/QueryStrings/Middleware/QueryStringBuilderMiddleware.cs index 1424d713..edeee51c 100644 --- a/src/Ocelot/QueryStrings/Middleware/QueryStringBuilderMiddleware.cs +++ b/src/Ocelot/QueryStrings/Middleware/QueryStringBuilderMiddleware.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Middleware; diff --git a/src/Ocelot/Responses/ErrorResponse.cs b/src/Ocelot/Responses/ErrorResponse.cs index 9f541507..bf20dbd1 100644 --- a/src/Ocelot/Responses/ErrorResponse.cs +++ b/src/Ocelot/Responses/ErrorResponse.cs @@ -5,6 +5,9 @@ namespace Ocelot.Responses { public class ErrorResponse : Response { + public ErrorResponse(Error error) : base(new List{error}) + { + } public ErrorResponse(List errors) : base(errors) { } diff --git a/src/Ocelot/Services/GetFileConfiguration.cs b/src/Ocelot/Services/FileConfigurationProvider.cs similarity index 79% rename from src/Ocelot/Services/GetFileConfiguration.cs rename to src/Ocelot/Services/FileConfigurationProvider.cs index 536a0b46..abac93f9 100644 --- a/src/Ocelot/Services/GetFileConfiguration.cs +++ b/src/Ocelot/Services/FileConfigurationProvider.cs @@ -6,9 +6,9 @@ using Ocelot.Responses; namespace Ocelot.Services { - public class GetFileConfiguration : IGetFileConfiguration + public class FileConfigurationProvider : IFileConfigurationProvider { - public Response Invoke() + public Response Get() { var configFilePath = $"{AppContext.BaseDirectory}/configuration.json"; var json = File.ReadAllText(configFilePath); diff --git a/src/Ocelot/Services/IGetFileConfiguration.cs b/src/Ocelot/Services/IFileConfigurationProvider.cs similarity index 52% rename from src/Ocelot/Services/IGetFileConfiguration.cs rename to src/Ocelot/Services/IFileConfigurationProvider.cs index 08b950a8..725f7463 100644 --- a/src/Ocelot/Services/IGetFileConfiguration.cs +++ b/src/Ocelot/Services/IFileConfigurationProvider.cs @@ -3,8 +3,8 @@ using Ocelot.Responses; namespace Ocelot.Services { - public interface IGetFileConfiguration + public interface IFileConfigurationProvider { - Response Invoke(); + Response Get(); } } \ No newline at end of file diff --git a/src/Ocelot/project.json b/src/Ocelot/project.json index 7b169503..1cb4b0c9 100644 --- a/src/Ocelot/project.json +++ b/src/Ocelot/project.json @@ -34,6 +34,7 @@ "runtimes": { "win10-x64": {}, "osx.10.11-x64": {}, + "osx.10.12-x64": {}, "win7-x64": {} }, "frameworks": { diff --git a/test/Ocelot.AcceptanceTests/project.json b/test/Ocelot.AcceptanceTests/project.json index f1aa378b..b77fe483 100644 --- a/test/Ocelot.AcceptanceTests/project.json +++ b/test/Ocelot.AcceptanceTests/project.json @@ -38,6 +38,7 @@ "runtimes": { "win10-x64": {}, "osx.10.11-x64": {}, + "osx.10.12-x64": {}, "win7-x64": {} }, "frameworks": { diff --git a/test/Ocelot.Benchmarks/project.json b/test/Ocelot.Benchmarks/project.json index 061a2223..98a414f1 100644 --- a/test/Ocelot.Benchmarks/project.json +++ b/test/Ocelot.Benchmarks/project.json @@ -11,6 +11,7 @@ "runtimes": { "win10-x64": {}, "osx.10.11-x64":{}, + "osx.10.12-x64": {}, "win7-x64": {} }, "frameworks": { diff --git a/test/Ocelot.IntegrationTests/project.json b/test/Ocelot.IntegrationTests/project.json index 7454b3a0..578f9a7f 100644 --- a/test/Ocelot.IntegrationTests/project.json +++ b/test/Ocelot.IntegrationTests/project.json @@ -39,6 +39,7 @@ "runtimes": { "win10-x64": {}, "osx.10.11-x64": {}, + "osx.10.12-x64": {}, "win7-x64": {} }, "frameworks": { diff --git a/test/Ocelot.ManualTest/project.json b/test/Ocelot.ManualTest/project.json index 04dba415..a31fdca2 100644 --- a/test/Ocelot.ManualTest/project.json +++ b/test/Ocelot.ManualTest/project.json @@ -23,6 +23,7 @@ "runtimes": { "win10-x64": {}, "osx.10.11-x64":{}, + "osx.10.12-x64": {}, "win7-x64": {} }, "frameworks": { diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs index 2573e32f..c7d240f8 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs @@ -16,14 +16,12 @@ namespace Ocelot.UnitTests.Configuration { private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider; private readonly Mock _configurationRepository; - private readonly Mock _creator; private Response _result; public FileConfigurationProviderTests() { - _creator = new Mock(); _configurationRepository = new Mock(); - _ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object, _creator.Object); + _ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object); } [Fact] @@ -35,16 +33,6 @@ namespace Ocelot.UnitTests.Configuration .BDDfy(); } - [Fact] - public void should_create_config_if_it_doesnt_exist() - { - this.Given(x => x.GivenTheRepoReturns(new OkResponse(null))) - .And(x => x.GivenTheCreatorReturns(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) - .When(x => x.WhenIGetTheConfig()) - .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) - .BDDfy(); - } - [Fact] public void should_return_error() { @@ -61,29 +49,6 @@ namespace Ocelot.UnitTests.Configuration .BDDfy(); } - [Fact] - public void should_return_error_if_creator_errors() - { - this.Given(x => x.GivenTheRepoReturns(new OkResponse(null))) - .And(x => x.GivenTheCreatorReturns(new ErrorResponse(new List - { - new AnyError() - }))) - .When(x => x.WhenIGetTheConfig()) - .Then(x => x.TheFollowingIsReturned(new ErrorResponse(new List - { - new AnyError() - }))) - .BDDfy(); - } - - private void GivenTheCreatorReturns(Response config) - { - _creator - .Setup(x => x.Create()) - .ReturnsAsync(config); - } - private void GivenTheRepoReturns(Response config) { _configurationRepository @@ -93,7 +58,7 @@ namespace Ocelot.UnitTests.Configuration private void WhenIGetTheConfig() { - _result = _ocelotConfigurationProvider.Get().Result; + _result = _ocelotConfigurationProvider.Get(); } private void TheFollowingIsReturned(Response expected) diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs new file mode 100644 index 00000000..aa9b935a --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; +using Moq; +using Ocelot.Configuration; +using Ocelot.Configuration.Creator; +using Ocelot.Configuration.File; +using Ocelot.Configuration.Repository; +using Ocelot.Configuration.Setter; +using Ocelot.Errors; +using Ocelot.Responses; +using Shouldly; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Configuration +{ + public class FileConfigurationSetterTests + { + private FileConfiguration _fileConfiguration; + private FileConfigurationSetter _configSetter; + private Mock _configRepo; + private Mock _configCreator; + private Response _configuration; + private object _result; + + public FileConfigurationSetterTests() + { + _configRepo = new Mock(); + _configCreator = new Mock(); + _configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object); + } + + [Fact] + public void should_set_configuration() + { + var fileConfig = new FileConfiguration(); + var config = new OcelotConfiguration(new List(), string.Empty); + + this.Given(x => GivenTheFollowingConfiguration(fileConfig)) + .And(x => GivenTheCreatorReturns(new OkResponse(config))) + .When(x => WhenISetTheConfiguration()) + .Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly()) + .BDDfy(); + } + + [Fact] + public void should_return_error_if_unable_to_set_configuration() + { + var fileConfig = new FileConfiguration(); + + this.Given(x => GivenTheFollowingConfiguration(fileConfig)) + .And(x => GivenTheCreatorReturns(new ErrorResponse(It.IsAny()))) + .When(x => WhenISetTheConfiguration()) + .And(x => ThenAnErrorResponseIsReturned()) + .BDDfy(); + } + + private void ThenAnErrorResponseIsReturned() + { + _result.ShouldBeOfType(); + } + + private void GivenTheCreatorReturns(Response configuration) + { + _configuration = configuration; + _configCreator + .Setup(x => x.Create(_fileConfiguration)) + .ReturnsAsync(_configuration); + } + + private void GivenTheFollowingConfiguration(FileConfiguration fileConfiguration) + { + _fileConfiguration = fileConfiguration; + } + + private void WhenISetTheConfiguration() + { + _result = _configSetter.Set(_fileConfiguration).Result; + } + + private void ThenTheConfigurationRepositoryIsCalledCorrectly() + { + _configRepo + .Verify(x => x.AddOrReplace(_configuration.Data), Times.Once); + } + } +} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs index cd2ab4ce..2638b4a5 100644 --- a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs @@ -1,28 +1,36 @@ +using System; +using System.Net; using Microsoft.AspNetCore.Mvc; using Moq; using Ocelot.Configuration.File; +using Ocelot.Configuration.Setter; using Ocelot.Controllers; +using Ocelot.Errors; using Ocelot.Responses; using Ocelot.Services; using TestStack.BDDfy; using Xunit; +using Shouldly; namespace Ocelot.UnitTests.Controllers { public class FileConfigurationControllerTests { private FileConfigurationController _controller; - private Mock _getFileConfig; + private Mock _configGetter; + private Mock _configSetter; private IActionResult _result; + private FileConfiguration _fileConfiguration; public FileConfigurationControllerTests() { - _getFileConfig = new Mock(); - _controller = new FileConfigurationController(_getFileConfig.Object); + _configGetter = new Mock(); + _configSetter = new Mock(); + _controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object); } [Fact] - public void should_return_file_configuration() + public void should_get_file_configuration() { var expected = new OkResponse(new FileConfiguration()); @@ -32,10 +40,75 @@ namespace Ocelot.UnitTests.Controllers .BDDfy(); } + [Fact] + public void should_return_error_when_cannot_get_config() + { + var expected = new ErrorResponse(It.IsAny()); + + this.Given(x => x.GivenTheGetConfigurationReturns(expected)) + .When(x => x.WhenIGetTheFileConfiguration()) + .Then(x => x.TheTheGetFileConfigurationIsCalledCorrectly()) + .And(x => x.ThenTheResponseIs()) + .BDDfy(); + } + + [Fact] + public void should_post_file_configuration() + { + var expected = new FileConfiguration(); + + this.Given(x => GivenTheFileConfiguration(expected)) + .And(x => GivenTheConfigSetterReturnsAnError(new OkResponse())) + .When(x => WhenIPostTheFileConfiguration()) + .Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly()) + .BDDfy(); + } + + [Fact] + public void should_return_error_when_cannot_set_config() + { + var expected = new FileConfiguration(); + + this.Given(x => GivenTheFileConfiguration(expected)) + .And(x => GivenTheConfigSetterReturnsAnError(new ErrorResponse(new FakeError()))) + .When(x => WhenIPostTheFileConfiguration()) + .Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly()) + .And(x => ThenTheResponseIs()) + .BDDfy(); + } + + private void GivenTheConfigSetterReturnsAnError(Response response) + { + _configSetter + .Setup(x => x.Set(It.IsAny())) + .ReturnsAsync(response); + } + + private void ThenTheConfigrationSetterIsCalledCorrectly() + { + _configSetter + .Verify(x => x.Set(_fileConfiguration), Times.Once); + } + + private void WhenIPostTheFileConfiguration() + { + _result = _controller.Post(_fileConfiguration).Result; + } + + private void GivenTheFileConfiguration(FileConfiguration fileConfiguration) + { + _fileConfiguration = fileConfiguration; + } + + private void ThenTheResponseIs() + { + _result.ShouldBeOfType(); + } + private void GivenTheGetConfigurationReturns(Response fileConfiguration) { - _getFileConfig - .Setup(x => x.Invoke()) + _configGetter + .Setup(x => x.Get()) .Returns(fileConfiguration); } @@ -46,8 +119,15 @@ namespace Ocelot.UnitTests.Controllers private void TheTheGetFileConfigurationIsCalledCorrectly() { - _getFileConfig - .Verify(x => x.Invoke(), Times.Once); + _configGetter + .Verify(x => x.Get(), Times.Once); + } + + class FakeError : Error + { + public FakeError() : base(string.Empty, OcelotErrorCode.CannotAddDataError) + { + } } } } \ No newline at end of file diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs index e7718f37..90a35ad8 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs @@ -90,7 +90,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder _downstreamRoute = new OkResponse(downstreamRoute); _downstreamRouteFinder .Setup(x => x.FindDownstreamRoute(It.IsAny(), It.IsAny())) - .ReturnsAsync(_downstreamRoute); + .Returns(_downstreamRoute); } public void Dispose() diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs index 0d272c63..49e9b01b 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs @@ -199,7 +199,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder _reRoutesConfig = reRoutesConfig; _mockConfig .Setup(x => x.Get()) - .ReturnsAsync(new OkResponse(new OcelotConfiguration(_reRoutesConfig, adminPath))); + .Returns(new OkResponse(new OcelotConfiguration(_reRoutesConfig, adminPath))); } private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath) @@ -209,7 +209,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private void WhenICallTheFinder() { - _result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod).Result; + _result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod); } private void ThenTheFollowingIsReturned(DownstreamRoute expected) diff --git a/test/Ocelot.UnitTests/Services/GetFileConfigurationTests.cs b/test/Ocelot.UnitTests/Services/FileConfigurationGetterTests.cs similarity index 95% rename from test/Ocelot.UnitTests/Services/GetFileConfigurationTests.cs rename to test/Ocelot.UnitTests/Services/FileConfigurationGetterTests.cs index fa86ced6..32266c44 100644 --- a/test/Ocelot.UnitTests/Services/GetFileConfigurationTests.cs +++ b/test/Ocelot.UnitTests/Services/FileConfigurationGetterTests.cs @@ -15,12 +15,12 @@ namespace Ocelot.UnitTests.Services { public class GetFileConfigurationTests { - private readonly IGetFileConfiguration _getReRoutes; + private readonly IFileConfigurationProvider _getReRoutes; private FileConfiguration _result; public GetFileConfigurationTests() { - _getReRoutes = new GetFileConfiguration(); + _getReRoutes = new FileConfigurationProvider(); } [Fact] @@ -74,7 +74,7 @@ namespace Ocelot.UnitTests.Services private void WhenIGetTheReRoutes() { - _result = _getReRoutes.Invoke().Data; + _result = _getReRoutes.Get().Data; } private void ThenTheFollowingIsReturned(FileConfiguration expected) diff --git a/test/Ocelot.UnitTests/project.json b/test/Ocelot.UnitTests/project.json index 3151ac57..e61760d6 100644 --- a/test/Ocelot.UnitTests/project.json +++ b/test/Ocelot.UnitTests/project.json @@ -29,6 +29,7 @@ "runtimes": { "win10-x64": {}, "osx.10.11-x64":{}, + "osx.10.12-x64": {}, "win7-x64": {} }, "frameworks": {