From 58b82f0fc7a69324d1dcde35e84c4f04fe3647f5 Mon Sep 17 00:00:00 2001 From: Pitming Date: Fri, 8 Nov 2019 12:59:34 +0100 Subject: [PATCH 01/24] Add DownstreamHttpMethodCreatorMiddleware --- .../Builder/DownstreamReRouteBuilder.cs | 10 ++++++- .../Configuration/Creator/ReRoutesCreator.cs | 1 + src/Ocelot/Configuration/DownstreamReRoute.cs | 5 +++- src/Ocelot/Configuration/File/FileReRoute.cs | 1 + .../DownstreamHttpMethodCreatorMiddleware.cs | 27 +++++++++++++++++++ .../Request/Middleware/DownstreamRequest.cs | 4 ++- .../HttpRequestBuilderMiddlewareExtensions.cs | 4 ++- 7 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index 4b3e6ea3..ed978dde 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -41,6 +41,7 @@ namespace Ocelot.Configuration.Builder private List _addHeadersToUpstream; private bool _dangerousAcceptAnyServerCertificateValidator; private SecurityOptions _securityOptions; + private string _downstreamHttpMethod; public DownstreamReRouteBuilder() { @@ -56,6 +57,12 @@ namespace Ocelot.Configuration.Builder return this; } + public DownstreamReRouteBuilder WithDownStreamHttpMethod(string method) + { + _downstreamHttpMethod = method; + return this; + } + public DownstreamReRouteBuilder WithLoadBalancerOptions(LoadBalancerOptions loadBalancerOptions) { _loadBalancerOptions = loadBalancerOptions; @@ -282,7 +289,8 @@ namespace Ocelot.Configuration.Builder _addHeadersToDownstream, _addHeadersToUpstream, _dangerousAcceptAnyServerCertificateValidator, - _securityOptions); + _securityOptions, + _downstreamHttpMethod); } } } diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index 4443e201..1cc463ef 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -138,6 +138,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) + .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); return reRoute; diff --git a/src/Ocelot/Configuration/DownstreamReRoute.cs b/src/Ocelot/Configuration/DownstreamReRoute.cs index e8dfade5..1c41f648 100644 --- a/src/Ocelot/Configuration/DownstreamReRoute.cs +++ b/src/Ocelot/Configuration/DownstreamReRoute.cs @@ -38,7 +38,8 @@ namespace Ocelot.Configuration List addHeadersToDownstream, List addHeadersToUpstream, bool dangerousAcceptAnyServerCertificateValidator, - SecurityOptions securityOptions) + SecurityOptions securityOptions, + string downstreamHttpMethod) { DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator; AddHeadersToDownstream = addHeadersToDownstream; @@ -72,6 +73,7 @@ namespace Ocelot.Configuration LoadBalancerKey = loadBalancerKey; AddHeadersToUpstream = addHeadersToUpstream; SecurityOptions = securityOptions; + DownstreamHttpMethod = downstreamHttpMethod; } public string Key { get; } @@ -106,5 +108,6 @@ namespace Ocelot.Configuration public List AddHeadersToUpstream { get; } public bool DangerousAcceptAnyServerCertificateValidator { get; } public SecurityOptions SecurityOptions { get; } + public string DownstreamHttpMethod { get; } } } diff --git a/src/Ocelot/Configuration/File/FileReRoute.cs b/src/Ocelot/Configuration/File/FileReRoute.cs index b15653ea..1bae31dd 100644 --- a/src/Ocelot/Configuration/File/FileReRoute.cs +++ b/src/Ocelot/Configuration/File/FileReRoute.cs @@ -29,6 +29,7 @@ namespace Ocelot.Configuration.File public string DownstreamPathTemplate { get; set; } public string UpstreamPathTemplate { get; set; } public List UpstreamHttpMethod { get; set; } + public string DownstreamHttpMethod { get; set; } public Dictionary AddHeadersToRequest { get; set; } public Dictionary UpstreamHeaderTransform { get; set; } public Dictionary DownstreamHeaderTransform { get; set; } diff --git a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs b/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs new file mode 100644 index 00000000..6689c0c1 --- /dev/null +++ b/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs @@ -0,0 +1,27 @@ +using Ocelot.Logging; +using Ocelot.Middleware; +using System.Threading.Tasks; + +namespace Ocelot.DownstreamUrlCreator.Middleware +{ + public class DownstreamHttpMethodCreatorMiddleware : OcelotMiddleware + { + private readonly OcelotRequestDelegate _next; + + public DownstreamHttpMethodCreatorMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory) + : base(loggerFactory.CreateLogger()) + { + _next = next; + } + + public async Task Invoke(DownstreamContext context) + { + if (context.DownstreamReRoute.DownstreamHttpMethod != null) + { + context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod; + } + + await _next.Invoke(context); + } + } +} diff --git a/src/Ocelot/Request/Middleware/DownstreamRequest.cs b/src/Ocelot/Request/Middleware/DownstreamRequest.cs index 256436e5..1715b387 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequest.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequest.cs @@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware public HttpRequestHeaders Headers { get; } - public string Method { get; } + public string Method { get; set; } public string OriginalString { get; } @@ -52,6 +52,8 @@ namespace Ocelot.Request.Middleware }; _request.RequestUri = uriBuilder.Uri; + _request.Method = new HttpMethod(Method); + _request.Content = Content; return _request; } diff --git a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs index 2d803e1e..6c43507c 100644 --- a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs +++ b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Middleware.Pipeline; namespace Ocelot.Request.Middleware @@ -7,7 +8,8 @@ namespace Ocelot.Request.Middleware { public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder) { - return builder.UseMiddleware(); + return builder.UseMiddleware() + .UseMiddleware(); } } } From ec622dc0add3474e3288c94a328652cbc9e71f8d Mon Sep 17 00:00:00 2001 From: Pitming Date: Fri, 8 Nov 2019 13:08:01 +0100 Subject: [PATCH 02/24] Add DownstreamHttpMethodCreatorMiddleware --- .../Middleware/DownstreamMethodTransformerMiddleware.cs} | 8 ++++---- .../Middleware/Pipeline/OcelotPipelineExtensions.cs | 4 ++++ src/Ocelot/Request/Middleware/DownstreamRequest.cs | 1 - .../Middleware/HttpRequestBuilderMiddlewareExtensions.cs | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) rename src/Ocelot/{DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs => DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs} (68%) diff --git a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs b/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs similarity index 68% rename from src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs rename to src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs index 6689c0c1..a17e9167 100644 --- a/src/Ocelot/DownstreamUrlCreator/Middleware/DownstreamHttpMethodCreatorMiddleware.cs +++ b/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs @@ -2,14 +2,14 @@ using Ocelot.Middleware; using System.Threading.Tasks; -namespace Ocelot.DownstreamUrlCreator.Middleware +namespace Ocelot.DownstreamMethodTransformer.Middleware { - public class DownstreamHttpMethodCreatorMiddleware : OcelotMiddleware + public class DownstreamMethodTransformerMiddleware : OcelotMiddleware { private readonly OcelotRequestDelegate _next; - public DownstreamHttpMethodCreatorMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory) - : base(loggerFactory.CreateLogger()) + public DownstreamMethodTransformerMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory) + : base(loggerFactory.CreateLogger()) { _next = next; } diff --git a/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs b/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs index d52cfb0d..02ee07f4 100644 --- a/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs +++ b/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs @@ -2,6 +2,7 @@ using Ocelot.Authorisation.Middleware; using Ocelot.Cache.Middleware; using Ocelot.Claims.Middleware; +using Ocelot.DownstreamMethodTransformer.Middleware; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Errors.Middleware; @@ -68,6 +69,9 @@ namespace Ocelot.Middleware.Pipeline // Initialises downstream request builder.UseDownstreamRequestInitialiser(); + //change Http Method + builder.UseMiddleware(); + // We check whether the request is ratelimit, and if there is no continue processing builder.UseRateLimiting(); diff --git a/src/Ocelot/Request/Middleware/DownstreamRequest.cs b/src/Ocelot/Request/Middleware/DownstreamRequest.cs index 1715b387..24d96cfe 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequest.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequest.cs @@ -53,7 +53,6 @@ namespace Ocelot.Request.Middleware _request.RequestUri = uriBuilder.Uri; _request.Method = new HttpMethod(Method); - _request.Content = Content; return _request; } diff --git a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs index 6c43507c..e6eff7e0 100644 --- a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs +++ b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs @@ -9,7 +9,7 @@ namespace Ocelot.Request.Middleware public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder) { return builder.UseMiddleware() - .UseMiddleware(); + .UseMiddleware(); } } } From e45071fa1070b026137f51f6fec94f9fa077a4be Mon Sep 17 00:00:00 2001 From: EL Aisati Ahmed Date: Tue, 4 Feb 2020 17:59:39 +0100 Subject: [PATCH 03/24] Fix issue #1088 (Ocelot Administration doesn't work with .NET Core 3.x) Fixed OcelotMiddlewareConfigurationDelegate configuration --- samples/AdministrationApi/AdministrationApi.csproj | 9 +++++---- samples/AdministrationApi/Program.cs | 13 ++++--------- ...IdentityServerMiddlewareConfigurationProvider.cs | 8 +++++++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/samples/AdministrationApi/AdministrationApi.csproj b/samples/AdministrationApi/AdministrationApi.csproj index 3fcf5dc8..7f8a351c 100644 --- a/samples/AdministrationApi/AdministrationApi.csproj +++ b/samples/AdministrationApi/AdministrationApi.csproj @@ -3,11 +3,12 @@ netcoreapp3.1 - + - - - + + + + \ No newline at end of file diff --git a/samples/AdministrationApi/Program.cs b/samples/AdministrationApi/Program.cs index a5e46fda..a69f1dc6 100644 --- a/samples/AdministrationApi/Program.cs +++ b/samples/AdministrationApi/Program.cs @@ -1,19 +1,14 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using Ocelot.Administration; using Ocelot.DependencyInjection; using Ocelot.Middleware; -using Ocelot.Administration; +using System.IO; namespace AdministrationApi { - public class Program + public class Program { public static void Main(string[] args) { diff --git a/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs b/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs index b4b8994a..0d263463 100644 --- a/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs +++ b/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs @@ -27,7 +27,13 @@ } app.UseAuthentication(); - app.UseMvc(); + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => + { + endpoints.MapDefaultControllerRoute(); + endpoints.MapControllers(); + }); }); } From 157737ef01b33ba784814392d080d442e273a808 Mon Sep 17 00:00:00 2001 From: EL Aisati Ahmed Date: Tue, 4 Feb 2020 18:11:38 +0100 Subject: [PATCH 04/24] Revert "Merge branch 'feature/issue#1088'" This reverts commit 914a386b0e79612a832a0ddc4c1b7fda7e8cf4a2. --- samples/AdministrationApi/AdministrationApi.csproj | 9 ++++----- samples/AdministrationApi/Program.cs | 13 +++++++++---- ...IdentityServerMiddlewareConfigurationProvider.cs | 8 +------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/samples/AdministrationApi/AdministrationApi.csproj b/samples/AdministrationApi/AdministrationApi.csproj index 7f8a351c..3fcf5dc8 100644 --- a/samples/AdministrationApi/AdministrationApi.csproj +++ b/samples/AdministrationApi/AdministrationApi.csproj @@ -3,12 +3,11 @@ netcoreapp3.1 - + - - - - + + + \ No newline at end of file diff --git a/samples/AdministrationApi/Program.cs b/samples/AdministrationApi/Program.cs index a69f1dc6..a5e46fda 100644 --- a/samples/AdministrationApi/Program.cs +++ b/samples/AdministrationApi/Program.cs @@ -1,14 +1,19 @@ -using Microsoft.AspNetCore.Hosting; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; -using Ocelot.Administration; using Ocelot.DependencyInjection; using Ocelot.Middleware; -using System.IO; +using Ocelot.Administration; namespace AdministrationApi { - public class Program + public class Program { public static void Main(string[] args) { diff --git a/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs b/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs index 0d263463..b4b8994a 100644 --- a/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs +++ b/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs @@ -27,13 +27,7 @@ } app.UseAuthentication(); - app.UseRouting(); - app.UseAuthorization(); - app.UseEndpoints(endpoints => - { - endpoints.MapDefaultControllerRoute(); - endpoints.MapControllers(); - }); + app.UseMvc(); }); } From f4eb93b66ca072d5f1b93a5d95eb64d54d1b5977 Mon Sep 17 00:00:00 2001 From: EL Aisati Ahmed Date: Tue, 4 Feb 2020 18:20:33 +0100 Subject: [PATCH 05/24] Fix issue #1088 (Ocelot Administration doesn't work with .NET Core 3.x) --- samples/AdministrationApi/AdministrationApi.csproj | 12 +++++++----- samples/AdministrationApi/Program.cs | 13 ++++--------- ...IdentityServerMiddlewareConfigurationProvider.cs | 8 +++++++- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/samples/AdministrationApi/AdministrationApi.csproj b/samples/AdministrationApi/AdministrationApi.csproj index 3fcf5dc8..4366ca06 100644 --- a/samples/AdministrationApi/AdministrationApi.csproj +++ b/samples/AdministrationApi/AdministrationApi.csproj @@ -1,13 +1,15 @@ - + netcoreapp3.1 - + + + + + - - - + \ No newline at end of file diff --git a/samples/AdministrationApi/Program.cs b/samples/AdministrationApi/Program.cs index a5e46fda..a69f1dc6 100644 --- a/samples/AdministrationApi/Program.cs +++ b/samples/AdministrationApi/Program.cs @@ -1,19 +1,14 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using Ocelot.Administration; using Ocelot.DependencyInjection; using Ocelot.Middleware; -using Ocelot.Administration; +using System.IO; namespace AdministrationApi { - public class Program + public class Program { public static void Main(string[] args) { diff --git a/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs b/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs index b4b8994a..0d263463 100644 --- a/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs +++ b/src/Ocelot.Administration/IdentityServerMiddlewareConfigurationProvider.cs @@ -27,7 +27,13 @@ } app.UseAuthentication(); - app.UseMvc(); + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => + { + endpoints.MapDefaultControllerRoute(); + endpoints.MapControllers(); + }); }); } From b5dc618780b362f449ecbf28140053500d4d30b7 Mon Sep 17 00:00:00 2001 From: eitamal <4740959+eitamal@users.noreply.github.com> Date: Wed, 5 Feb 2020 04:22:33 +1000 Subject: [PATCH 06/24] Fix URL for the Ocelot logo in README.md (#1117) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a655d3d2..4de2f08d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[](https://threemammals.com/ocelot) +[](https://threemammals.com/ocelot) [![CircleCI](https://circleci.com/gh/ThreeMammals/Ocelot/tree/master.svg?style=svg)](https://circleci.com/gh/ThreeMammals/Ocelot/tree/master) From 73e56322e0bf469c1f3b9e0d240daa644cb15a4c Mon Sep 17 00:00:00 2001 From: WebMed Date: Tue, 4 Feb 2020 20:50:13 +0100 Subject: [PATCH 07/24] Removed direct reference to Ocelot.Administration project --- samples/AdministrationApi/AdministrationApi.csproj | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/samples/AdministrationApi/AdministrationApi.csproj b/samples/AdministrationApi/AdministrationApi.csproj index 4366ca06..e634ba2c 100644 --- a/samples/AdministrationApi/AdministrationApi.csproj +++ b/samples/AdministrationApi/AdministrationApi.csproj @@ -5,11 +5,9 @@ - + + - - - \ No newline at end of file From 473d50ff361fc98d131f2e5409f883741e710c6d Mon Sep 17 00:00:00 2001 From: Jonathan Mezach Date: Tue, 4 Feb 2020 21:10:25 +0100 Subject: [PATCH 08/24] Update README.md (#1110) Fix link to Slack ;) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4de2f08d..57a93697 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Coverage Status](https://coveralls.io/repos/github/ThreeMammals/Ocelot/badge.svg?branch=master)](https://coveralls.io/github/ThreeMammals/Ocelot?branch=master) -[Slack](threemammals.slack.com) +[Slack](https://threemammals.slack.com) # Ocelot From 86e8d66daf1365e87ad9ba9b00724e65b129c560 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Tue, 4 Feb 2020 20:50:40 +0000 Subject: [PATCH 09/24] Activate ChangeToken when Ocelot's configuration changes #1037 * Add configuration change token (#1036) * Add IOptionsMonitor * Activate change token from *ConfigurationRepository instead of FileAndInternalConfigurationSetter; add acceptance & integration tests * Update documentation * Use IWebHostEnvironment as IHostingEnvironment deprecated Co-authored-by: Chris Swinchatt --- docs/features/configuration.rst | 510 ++--- .../IOcelotConfigurationChangeTokenSource.cs | 14 + .../OcelotConfigurationChangeToken.cs | 74 + .../OcelotConfigurationChangeTokenSource.cs | 16 + .../OcelotConfigurationMonitor.cs | 30 + .../DiskFileConfigurationRepository.cs | 6 +- ...InMemoryInternalConfigurationRepository.cs | 10 +- .../DependencyInjection/OcelotBuilder.cs | 9 +- src/Ocelot/Properties/AssemblyInfo.cs | 2 +- .../ConfigurationReloadTests.cs | 28 + test/Ocelot.AcceptanceTests/Steps.cs | 15 +- .../AdministrationTests.cs | 1774 +++++++++-------- ...elotConfigurationChangeTokenSourceTests.cs | 35 + .../OcelotConfigurationChangeTokenTests.cs | 91 + .../DiskFileConfigurationRepositoryTests.cs | 20 +- .../FileConfigurationSetterTests.cs | 8 +- .../InMemoryConfigurationRepositoryTests.cs | 15 +- 17 files changed, 1546 insertions(+), 1111 deletions(-) create mode 100644 src/Ocelot/Configuration/ChangeTracking/IOcelotConfigurationChangeTokenSource.cs create mode 100644 src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeToken.cs create mode 100644 src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSource.cs create mode 100644 src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationMonitor.cs create mode 100644 test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSourceTests.cs create mode 100644 test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenTests.cs diff --git a/docs/features/configuration.rst b/docs/features/configuration.rst index 7983af39..cc4afa0c 100644 --- a/docs/features/configuration.rst +++ b/docs/features/configuration.rst @@ -1,230 +1,280 @@ -Configuration -============ - -An example configuration can be found `here `_. -There are two sections to the configuration. An array of ReRoutes and a GlobalConfiguration. -The ReRoutes are the objects that tell Ocelot how to treat an upstream request. The Global -configuration is a bit hacky and allows overrides of ReRoute specific settings. It's useful -if you don't want to manage lots of ReRoute specific settings. - -.. code-block:: json - - { - "ReRoutes": [], - "GlobalConfiguration": {} - } - -Here is an example ReRoute configuration, You don't need to set all of these things but this is everything that is available at the moment: - -.. code-block:: json - - { - "DownstreamPathTemplate": "/", - "UpstreamPathTemplate": "/", - "UpstreamHttpMethod": [ - "Get" - ], - "AddHeadersToRequest": {}, - "AddClaimsToRequest": {}, - "RouteClaimsRequirement": {}, - "AddQueriesToRequest": {}, - "RequestIdKey": "", - "FileCacheOptions": { - "TtlSeconds": 0, - "Region": "" - }, - "ReRouteIsCaseSensitive": false, - "ServiceName": "", - "DownstreamScheme": "http", - "DownstreamHostAndPorts": [ - { - "Host": "localhost", - "Port": 51876, - } - ], - "QoSOptions": { - "ExceptionsAllowedBeforeBreaking": 0, - "DurationOfBreak": 0, - "TimeoutValue": 0 - }, - "LoadBalancer": "", - "RateLimitOptions": { - "ClientWhitelist": [], - "EnableRateLimiting": false, - "Period": "", - "PeriodTimespan": 0, - "Limit": 0 - }, - "AuthenticationOptions": { - "AuthenticationProviderKey": "", - "AllowedScopes": [] - }, - "HttpHandlerOptions": { - "AllowAutoRedirect": true, - "UseCookieContainer": true, - "UseTracing": true, - "MaxConnectionsPerServer": 100 - }, - "DangerousAcceptAnyServerCertificateValidator": false - } - -More information on how to use these options is below.. - -Multiple environments -^^^^^^^^^^^^^^^^^^^^^ - -Like any other asp.net core project Ocelot supports configuration file names such as configuration.dev.json, configuration.test.json etc. In order to implement this add the following -to you - -.. code-block:: csharp - - .ConfigureAppConfiguration((hostingContext, config) => - { - config - .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) - .AddJsonFile("appsettings.json", true, true) - .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) - .AddJsonFile("ocelot.json") - .AddJsonFile($"configuration.{hostingContext.HostingEnvironment.EnvironmentName}.json") - .AddEnvironmentVariables(); - }) - -Ocelot will now use the environment specific configuration and fall back to ocelot.json if there isn't one. - -You also need to set the corresponding environment variable which is ASPNETCORE_ENVIRONMENT. More info on this can be found in the `asp.net core docs `_. - -Merging configuration files -^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -This feature was requested in `Issue 296 `_ and allows users to have multiple configuration files to make managing large configurations easier. - -Instead of adding the configuration directly e.g. AddJsonFile("ocelot.json") you can call AddOcelot() like below. - -.. code-block:: csharp - - .ConfigureAppConfiguration((hostingContext, config) => - { - config - .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) - .AddJsonFile("appsettings.json", true, true) - .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) - .AddOcelot(hostingContext.HostingEnvironment) - .AddEnvironmentVariables(); - }) - -In this scenario Ocelot will look for any files that match the pattern (?i)ocelot.([a-zA-Z0-9]*).json and then merge these together. If you want to set the GlobalConfiguration property you must have a file called ocelot.global.json. - -The way Ocelot merges the files is basically load them, loop over them, add any ReRoutes, add any AggregateReRoutes and if the file is called ocelot.global.json add the GlobalConfiguration aswell as any ReRoutes or AggregateReRoutes. Ocelot will then save the merged configuration to a file called ocelot.json and this will be used as the source of truth while ocelot is running. - -At the moment there is no validation at this stage it only happens when Ocelot validates the final merged configuration. This is something to be aware of when you are investigating problems. I would advise always checking what is in ocelot.json if you have any problems. - -You can also give Ocelot a specific path to look in for the configuration files like below. - -.. code-block:: csharp - - .ConfigureAppConfiguration((hostingContext, config) => - { - config - .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) - .AddJsonFile("appsettings.json", true, true) - .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) - .AddOcelot("/foo/bar", hostingContext.HostingEnvironment) - .AddEnvironmentVariables(); - }) - -Ocelot needs the HostingEnvironment so it knows to exclude anything environment specific from the algorithm. - -Store configuration in consul -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -The first thing you need to do is install the NuGet package that provides Consul support in Ocelot. - -``Install-Package Ocelot.Provider.Consul`` - -Then you add the following when you register your services Ocelot will attempt to store and retrieve its configuration in consul KV store. - -.. code-block:: csharp - - services - .AddOcelot() - .AddConsul() - .AddConfigStoredInConsul(); - -You also need to add the following to your ocelot.json. This is how Ocelot -finds your Consul agent and interacts to load and store the configuration from Consul. - -.. code-block:: json - - "GlobalConfiguration": { - "ServiceDiscoveryProvider": { - "Host": "localhost", - "Port": 9500 - } - } - -I decided to create this feature after working on the Raft consensus algorithm and finding out its super hard. Why not take advantage of the fact Consul already gives you this! -I guess it means if you want to use Ocelot to its fullest you take on Consul as a dependency for now. - -This feature has a 3 second ttl cache before making a new request to your local consul agent. - -Reload JSON config on change -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Ocelot supports reloading the json configuration file on change. e.g. the following will recreate Ocelots internal configuration when the ocelot.json file is updated -manually. - -.. code-block:: json - - config.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); - -Configuration Key ------------------ - -If you are using Consul for configuration (or other providers in the future) you might want to key your configurations so you can have multiple configurations :) This feature was requested in `issue 346 `_! In order to specify the key you need to set the ConfigurationKey property in the ServiceDiscoveryProvider section of the configuration json file e.g. - -.. code-block:: json - - "GlobalConfiguration": { - "ServiceDiscoveryProvider": { - "Host": "localhost", - "Port": 9500, - "ConfigurationKey": "Oceolot_A" - } - } - -In this example Ocelot will use Oceolot_A as the key for your configuration when looking it up in Consul. - -If you do not set the ConfigurationKey Ocelot will use the string InternalConfiguration as the key. - -Follow Redirects / Use CookieContainer -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Use HttpHandlerOptions in ReRoute configuration to set up HttpHandler behavior: - -1. AllowAutoRedirect is a value that indicates whether the request should follow redirection responses. Set it true if the request should automatically -follow redirection responses from the Downstream resource; otherwise false. The default value is false. - -2. UseCookieContainer is a value that indicates whether the handler uses the CookieContainer -property to store server cookies and uses these cookies when sending requests. The default value is false. Please note -that if you are using the CookieContainer Ocelot caches the HttpClient for each downstream service. This means that all requests -to that DownstreamService will share the same cookies. `Issue 274 `_ was created because a user -noticed that the cookies were being shared. I tried to think of a nice way to handle this but I think it is impossible. If you don't cache the clients -that means each request gets a new client and therefore a new cookie container. If you clear the cookies from the cached client container you get race conditions due to inflight -requests. This would also mean that subsequent requests don't use the cookies from the previous response! All in all not a great situation. I would avoid setting -UseCookieContainer to true unless you have a really really good reason. Just look at your response headers and forward the cookies back with your next request! - -SSL Errors -^^^^^^^^^^ - -If you want to ignore SSL warnings / errors set the following in your ReRoute config. - -.. code-block:: json - - "DangerousAcceptAnyServerCertificateValidator": true - -I don't recommend doing this, I suggest creating your own certificate and then getting it trusted by your local / remote machine if you can. - -MaxConnectionsPerServer -^^^^^^^^^^^^^^^^^^^^^^^ - -This controls how many connections the internal HttpClient will open. This can be set at ReRoute or global level. \ No newline at end of file +Configuration +============ + +An example configuration can be found `here `_. +There are two sections to the configuration. An array of ReRoutes and a GlobalConfiguration. +The ReRoutes are the objects that tell Ocelot how to treat an upstream request. The Global +configuration is a bit hacky and allows overrides of ReRoute specific settings. It's useful +if you don't want to manage lots of ReRoute specific settings. + +.. code-block:: json + + { + "ReRoutes": [], + "GlobalConfiguration": {} + } + +Here is an example ReRoute configuration, You don't need to set all of these things but this is everything that is available at the moment: + +.. code-block:: json + + { + "DownstreamPathTemplate": "/", + "UpstreamPathTemplate": "/", + "UpstreamHttpMethod": [ + "Get" + ], + "AddHeadersToRequest": {}, + "AddClaimsToRequest": {}, + "RouteClaimsRequirement": {}, + "AddQueriesToRequest": {}, + "RequestIdKey": "", + "FileCacheOptions": { + "TtlSeconds": 0, + "Region": "" + }, + "ReRouteIsCaseSensitive": false, + "ServiceName": "", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 51876, + } + ], + "QoSOptions": { + "ExceptionsAllowedBeforeBreaking": 0, + "DurationOfBreak": 0, + "TimeoutValue": 0 + }, + "LoadBalancer": "", + "RateLimitOptions": { + "ClientWhitelist": [], + "EnableRateLimiting": false, + "Period": "", + "PeriodTimespan": 0, + "Limit": 0 + }, + "AuthenticationOptions": { + "AuthenticationProviderKey": "", + "AllowedScopes": [] + }, + "HttpHandlerOptions": { + "AllowAutoRedirect": true, + "UseCookieContainer": true, + "UseTracing": true, + "MaxConnectionsPerServer": 100 + }, + "DangerousAcceptAnyServerCertificateValidator": false + } + +More information on how to use these options is below.. + +Multiple environments +^^^^^^^^^^^^^^^^^^^^^ + +Like any other asp.net core project Ocelot supports configuration file names such as configuration.dev.json, configuration.test.json etc. In order to implement this add the following +to you + +.. code-block:: csharp + + .ConfigureAppConfiguration((hostingContext, config) => + { + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddJsonFile("ocelot.json") + .AddJsonFile($"configuration.{hostingContext.HostingEnvironment.EnvironmentName}.json") + .AddEnvironmentVariables(); + }) + +Ocelot will now use the environment specific configuration and fall back to ocelot.json if there isn't one. + +You also need to set the corresponding environment variable which is ASPNETCORE_ENVIRONMENT. More info on this can be found in the `asp.net core docs `_. + +Merging configuration files +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +This feature was requested in `Issue 296 `_ and allows users to have multiple configuration files to make managing large configurations easier. + +Instead of adding the configuration directly e.g. AddJsonFile("ocelot.json") you can call AddOcelot() like below. + +.. code-block:: csharp + + .ConfigureAppConfiguration((hostingContext, config) => + { + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddOcelot(hostingContext.HostingEnvironment) + .AddEnvironmentVariables(); + }) + +In this scenario Ocelot will look for any files that match the pattern (?i)ocelot.([a-zA-Z0-9]*).json and then merge these together. If you want to set the GlobalConfiguration property you must have a file called ocelot.global.json. + +The way Ocelot merges the files is basically load them, loop over them, add any ReRoutes, add any AggregateReRoutes and if the file is called ocelot.global.json add the GlobalConfiguration aswell as any ReRoutes or AggregateReRoutes. Ocelot will then save the merged configuration to a file called ocelot.json and this will be used as the source of truth while ocelot is running. + +At the moment there is no validation at this stage it only happens when Ocelot validates the final merged configuration. This is something to be aware of when you are investigating problems. I would advise always checking what is in ocelot.json if you have any problems. + +You can also give Ocelot a specific path to look in for the configuration files like below. + +.. code-block:: csharp + + .ConfigureAppConfiguration((hostingContext, config) => + { + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddOcelot("/foo/bar", hostingContext.HostingEnvironment) + .AddEnvironmentVariables(); + }) + +Ocelot needs the HostingEnvironment so it knows to exclude anything environment specific from the algorithm. + +Store configuration in consul +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The first thing you need to do is install the NuGet package that provides Consul support in Ocelot. + +``Install-Package Ocelot.Provider.Consul`` + +Then you add the following when you register your services Ocelot will attempt to store and retrieve its configuration in consul KV store. + +.. code-block:: csharp + + services + .AddOcelot() + .AddConsul() + .AddConfigStoredInConsul(); + +You also need to add the following to your ocelot.json. This is how Ocelot +finds your Consul agent and interacts to load and store the configuration from Consul. + +.. code-block:: json + + "GlobalConfiguration": { + "ServiceDiscoveryProvider": { + "Host": "localhost", + "Port": 9500 + } + } + +I decided to create this feature after working on the Raft consensus algorithm and finding out its super hard. Why not take advantage of the fact Consul already gives you this! +I guess it means if you want to use Ocelot to its fullest you take on Consul as a dependency for now. + +This feature has a 3 second ttl cache before making a new request to your local consul agent. + +Reload JSON config on change +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Ocelot supports reloading the json configuration file on change. e.g. the following will recreate Ocelots internal configuration when the ocelot.json file is updated +manually. + +.. code-block:: json + + config.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); + +Configuration Key +----------------- + +If you are using Consul for configuration (or other providers in the future) you might want to key your configurations so you can have multiple configurations :) This feature was requested in `issue 346 `_! In order to specify the key you need to set the ConfigurationKey property in the ServiceDiscoveryProvider section of the configuration json file e.g. + +.. code-block:: json + + "GlobalConfiguration": { + "ServiceDiscoveryProvider": { + "Host": "localhost", + "Port": 9500, + "ConfigurationKey": "Oceolot_A" + } + } + +In this example Ocelot will use Oceolot_A as the key for your configuration when looking it up in Consul. + +If you do not set the ConfigurationKey Ocelot will use the string InternalConfiguration as the key. + +Follow Redirects / Use CookieContainer +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Use HttpHandlerOptions in ReRoute configuration to set up HttpHandler behavior: + +1. AllowAutoRedirect is a value that indicates whether the request should follow redirection responses. Set it true if the request should automatically +follow redirection responses from the Downstream resource; otherwise false. The default value is false. + +2. UseCookieContainer is a value that indicates whether the handler uses the CookieContainer +property to store server cookies and uses these cookies when sending requests. The default value is false. Please note +that if you are using the CookieContainer Ocelot caches the HttpClient for each downstream service. This means that all requests +to that DownstreamService will share the same cookies. `Issue 274 `_ was created because a user +noticed that the cookies were being shared. I tried to think of a nice way to handle this but I think it is impossible. If you don't cache the clients +that means each request gets a new client and therefore a new cookie container. If you clear the cookies from the cached client container you get race conditions due to inflight +requests. This would also mean that subsequent requests don't use the cookies from the previous response! All in all not a great situation. I would avoid setting +UseCookieContainer to true unless you have a really really good reason. Just look at your response headers and forward the cookies back with your next request! + +SSL Errors +^^^^^^^^^^ + +If you want to ignore SSL warnings / errors set the following in your ReRoute config. + +.. code-block:: json + + "DangerousAcceptAnyServerCertificateValidator": true + +I don't recommend doing this, I suggest creating your own certificate and then getting it trusted by your local / remote machine if you can. + +MaxConnectionsPerServer +^^^^^^^^^^^^^^^^^^^^^^^ + +This controls how many connections the internal HttpClient will open. This can be set at ReRoute or global level. + +React to Configuration Changes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Resolve IOcelotConfigurationChangeTokenSource from the DI container if you wish to react to changes to the Ocelot configuration via the Ocelot.Administration API or ocelot.json being reloaded from the disk. You may either poll the change token's HasChanged property, or register a callback with the RegisterChangeCallback method. + +Polling the HasChanged property +------------------------------- + +.. code-block:: csharp + public class ConfigurationNotifyingService : BackgroundService + { + private readonly IOcelotConfigurationChangeTokenSource _tokenSource; + private readonly ILogger _logger; + public ConfigurationNotifyingService(IOcelotConfigurationChangeTokenSource tokenSource, ILogger logger) + { + _tokenSource = tokenSource; + _logger = logger; + } + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + if (_tokenSource.ChangeToken.HasChanged) + { + _logger.LogInformation("Configuration updated"); + } + await Task.Delay(1000, stoppingToken); + } + } + } + +Registering a callback +---------------------- + +.. code-block:: csharp + public class MyDependencyInjectedClass : IDisposable + { + private readonly IOcelotConfigurationChangeTokenSource _tokenSource; + private readonly IDisposable _callbackHolder; + public MyClass(IOcelotConfigurationChangeTokenSource tokenSource) + { + _tokenSource = tokenSource; + _callbackHolder = tokenSource.ChangeToken.RegisterChangeCallback(_ => Console.WriteLine("Configuration changed"), null); + } + public void Dispose() + { + _callbackHolder.Dispose(); + } + } \ No newline at end of file diff --git a/src/Ocelot/Configuration/ChangeTracking/IOcelotConfigurationChangeTokenSource.cs b/src/Ocelot/Configuration/ChangeTracking/IOcelotConfigurationChangeTokenSource.cs new file mode 100644 index 00000000..f69d91af --- /dev/null +++ b/src/Ocelot/Configuration/ChangeTracking/IOcelotConfigurationChangeTokenSource.cs @@ -0,0 +1,14 @@ +namespace Ocelot.Configuration.ChangeTracking +{ + using Microsoft.Extensions.Primitives; + + /// + /// source which is activated when Ocelot's configuration is changed. + /// + public interface IOcelotConfigurationChangeTokenSource + { + IChangeToken ChangeToken { get; } + + void Activate(); + } +} diff --git a/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeToken.cs b/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeToken.cs new file mode 100644 index 00000000..ecefcf9b --- /dev/null +++ b/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeToken.cs @@ -0,0 +1,74 @@ +namespace Ocelot.Configuration.ChangeTracking +{ + using System; + using System.Collections.Generic; + using Microsoft.Extensions.Primitives; + + public class OcelotConfigurationChangeToken : IChangeToken + { + public const double PollingIntervalSeconds = 1; + + private readonly ICollection _callbacks = new List(); + private readonly object _lock = new object(); + private DateTime? _timeChanged; + + public IDisposable RegisterChangeCallback(Action callback, object state) + { + lock (_lock) + { + var wrapper = new CallbackWrapper(callback, state, _callbacks, _lock); + _callbacks.Add(wrapper); + return wrapper; + } + } + + public void Activate() + { + lock (_lock) + { + _timeChanged = DateTime.UtcNow; + foreach (var wrapper in _callbacks) + { + wrapper.Invoke(); + } + } + } + + // Token stays active for PollingIntervalSeconds after a change (could be parameterised) - otherwise HasChanged would be true forever. + // Taking suggestions for better ways to reset HasChanged back to false. + public bool HasChanged => _timeChanged.HasValue && (DateTime.UtcNow - _timeChanged.Value).TotalSeconds < PollingIntervalSeconds; + + public bool ActiveChangeCallbacks => true; + + private class CallbackWrapper : IDisposable + { + private readonly ICollection _callbacks; + private readonly object _lock; + + public CallbackWrapper(Action callback, object state, ICollection callbacks, object @lock) + { + _callbacks = callbacks; + _lock = @lock; + Callback = callback; + State = state; + } + + public void Invoke() + { + Callback.Invoke(State); + } + + public void Dispose() + { + lock (_lock) + { + _callbacks.Remove(this); + } + } + + public Action Callback { get; } + + public object State { get; } + } + } +} diff --git a/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSource.cs b/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSource.cs new file mode 100644 index 00000000..422864d2 --- /dev/null +++ b/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSource.cs @@ -0,0 +1,16 @@ +namespace Ocelot.Configuration.ChangeTracking +{ + using Microsoft.Extensions.Primitives; + + public class OcelotConfigurationChangeTokenSource : IOcelotConfigurationChangeTokenSource + { + private readonly OcelotConfigurationChangeToken _changeToken = new OcelotConfigurationChangeToken(); + + public IChangeToken ChangeToken => _changeToken; + + public void Activate() + { + _changeToken.Activate(); + } + } +} diff --git a/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationMonitor.cs b/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationMonitor.cs new file mode 100644 index 00000000..4e5536d0 --- /dev/null +++ b/src/Ocelot/Configuration/ChangeTracking/OcelotConfigurationMonitor.cs @@ -0,0 +1,30 @@ +namespace Ocelot.Configuration.ChangeTracking +{ + using System; + using Microsoft.Extensions.Options; + using Ocelot.Configuration.Repository; + + public class OcelotConfigurationMonitor : IOptionsMonitor + { + private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource; + private readonly IInternalConfigurationRepository _repo; + + public OcelotConfigurationMonitor(IInternalConfigurationRepository repo, IOcelotConfigurationChangeTokenSource changeTokenSource) + { + _changeTokenSource = changeTokenSource; + _repo = repo; + } + + public IInternalConfiguration Get(string name) + { + return _repo.Get().Data; + } + + public IDisposable OnChange(Action listener) + { + return _changeTokenSource.ChangeToken.RegisterChangeCallback(_ => listener(CurrentValue, ""), null); + } + + public IInternalConfiguration CurrentValue => _repo.Get().Data; + } +} diff --git a/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs index 770d7b7a..965efaae 100644 --- a/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs @@ -4,18 +4,21 @@ using Ocelot.Configuration.File; using Ocelot.Responses; using System; using System.Threading.Tasks; +using Ocelot.Configuration.ChangeTracking; namespace Ocelot.Configuration.Repository { public class DiskFileConfigurationRepository : IFileConfigurationRepository { + private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource; private readonly string _environmentFilePath; private readonly string _ocelotFilePath; private static readonly object _lock = new object(); private const string ConfigurationFileName = "ocelot"; - public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment) + public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, IOcelotConfigurationChangeTokenSource changeTokenSource) { + _changeTokenSource = changeTokenSource; _environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json"; _ocelotFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}.json"; @@ -56,6 +59,7 @@ namespace Ocelot.Configuration.Repository System.IO.File.WriteAllText(_ocelotFilePath, jsonConfiguration); } + _changeTokenSource.Activate(); return Task.FromResult(new OkResponse()); } } diff --git a/src/Ocelot/Configuration/Repository/InMemoryInternalConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/InMemoryInternalConfigurationRepository.cs index 2ac954e3..9daee78b 100644 --- a/src/Ocelot/Configuration/Repository/InMemoryInternalConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/InMemoryInternalConfigurationRepository.cs @@ -1,4 +1,5 @@ -using Ocelot.Responses; +using Ocelot.Configuration.ChangeTracking; +using Ocelot.Responses; namespace Ocelot.Configuration.Repository { @@ -10,6 +11,12 @@ namespace Ocelot.Configuration.Repository private static readonly object LockObject = new object(); private IInternalConfiguration _internalConfiguration; + private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource; + + public InMemoryInternalConfigurationRepository(IOcelotConfigurationChangeTokenSource changeTokenSource) + { + _changeTokenSource = changeTokenSource; + } public Response Get() { @@ -23,6 +30,7 @@ namespace Ocelot.Configuration.Repository _internalConfiguration = internalConfiguration; } + _changeTokenSource.Activate(); return new OkResponse(); } } diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 8f120997..7f92d50d 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -1,9 +1,12 @@ +using Ocelot.Configuration.ChangeTracking; + namespace Ocelot.DependencyInjection { using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; + using Microsoft.Extensions.Options; using Ocelot.Authorisation; using Ocelot.Cache; using Ocelot.Claims; @@ -112,6 +115,8 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton, OcelotConfigurationMonitor>(); // see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc // could maybe use a scoped data repository @@ -215,7 +220,7 @@ namespace Ocelot.DependencyInjection { // see: https://greatrexpectations.com/2018/10/25/decorators-in-net-core-with-dependency-injection var wrappedDescriptor = Services.First(x => x.ServiceType == typeof(IPlaceholders)); - + var objectFactory = ActivatorUtilities.CreateFactory( typeof(ConfigAwarePlaceholders), new[] { typeof(IPlaceholders) }); @@ -229,7 +234,7 @@ namespace Ocelot.DependencyInjection return this; } - + private static object CreateInstance(IServiceProvider services, ServiceDescriptor descriptor) { if (descriptor.ImplementationInstance != null) diff --git a/src/Ocelot/Properties/AssemblyInfo.cs b/src/Ocelot/Properties/AssemblyInfo.cs index c2a11a5b..cf28a510 100644 --- a/src/Ocelot/Properties/AssemblyInfo.cs +++ b/src/Ocelot/Properties/AssemblyInfo.cs @@ -15,4 +15,4 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("d6df4206-0dba-41d8-884d-c3e08290fdbb")] +[assembly: Guid("d6df4206-0dba-41d8-884d-c3e08290fdbb")] diff --git a/test/Ocelot.AcceptanceTests/ConfigurationReloadTests.cs b/test/Ocelot.AcceptanceTests/ConfigurationReloadTests.cs index 90d57504..58a2c797 100644 --- a/test/Ocelot.AcceptanceTests/ConfigurationReloadTests.cs +++ b/test/Ocelot.AcceptanceTests/ConfigurationReloadTests.cs @@ -1,5 +1,6 @@ using Ocelot.Configuration.File; using System; +using Ocelot.Configuration.ChangeTracking; using TestStack.BDDfy; using Xunit; @@ -54,6 +55,33 @@ namespace Ocelot.AcceptanceTests .BDDfy(); } + [Fact] + public void should_trigger_change_token_on_change() + { + this.Given(x => _steps.GivenThereIsAConfiguration(_initialConfig)) + .And(x => _steps.GivenOcelotIsRunningReloadingConfig(true)) + .And(x => _steps.GivenIHaveAChangeToken()) + .And(x => _steps.GivenThereIsAConfiguration(_anotherConfig)) + .And(x => _steps.GivenIWait(MillisecondsToWaitForChangeToken)) + .Then(x => _steps.TheChangeTokenShouldBeActive(true)) + .BDDfy(); + } + + [Fact] + public void should_not_trigger_change_token_with_no_change() + { + this.Given(x => _steps.GivenThereIsAConfiguration(_initialConfig)) + .And(x => _steps.GivenOcelotIsRunningReloadingConfig(false)) + .And(x => _steps.GivenIHaveAChangeToken()) + .And(x => _steps.GivenIWait(MillisecondsToWaitForChangeToken)) // Wait for prior activation to expire. + .And(x => _steps.GivenThereIsAConfiguration(_anotherConfig)) + .And(x => _steps.GivenIWait(MillisecondsToWaitForChangeToken)) + .Then(x => _steps.TheChangeTokenShouldBeActive(false)) + .BDDfy(); + } + + private const int MillisecondsToWaitForChangeToken = (int) (OcelotConfigurationChangeToken.PollingIntervalSeconds*1000) - 100; + public void Dispose() { _steps.Dispose(); diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index 9bfb2091..341daa36 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -1,4 +1,6 @@ -namespace Ocelot.AcceptanceTests +using Ocelot.Configuration.ChangeTracking; + +namespace Ocelot.AcceptanceTests { using Caching; using Configuration.Repository; @@ -54,6 +56,7 @@ private IWebHostBuilder _webHostBuilder; private WebHostBuilder _ocelotBuilder; private IWebHost _ocelotHost; + private IOcelotConfigurationChangeTokenSource _changeToken; public Steps() { @@ -216,6 +219,11 @@ _ocelotClient = _ocelotServer.CreateClient(); } + public void GivenIHaveAChangeToken() + { + _changeToken = _ocelotServer.Host.Services.GetRequiredService(); + } + /// /// This is annoying cos it should be in the constructor but we need to set up the file before calling startup so its a step. /// @@ -1123,6 +1131,11 @@ _ocelotClient = _ocelotServer.CreateClient(); } + public void TheChangeTokenShouldBeActive(bool itShouldBeActive) + { + _changeToken.ChangeToken.HasChanged.ShouldBe(itShouldBeActive); + } + public void GivenOcelotIsRunningWithLogger() { _webHostBuilder = new WebHostBuilder(); diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index fe18965d..8c378ece 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -1,864 +1,910 @@ -using IdentityServer4.AccessTokenValidation; -using IdentityServer4.Models; -using IdentityServer4.Test; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; -using Ocelot.Administration; -using Ocelot.Cache; -using Ocelot.Configuration.File; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; -using Shouldly; -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using TestStack.BDDfy; -using Xunit; - -namespace Ocelot.IntegrationTests -{ - public class AdministrationTests : IDisposable - { - private HttpClient _httpClient; - private readonly HttpClient _httpClientTwo; - private HttpResponseMessage _response; - private IHost _builder; - private IHostBuilder _webHostBuilder; - private string _ocelotBaseUrl; - private BearerToken _token; - private IHostBuilder _webHostBuilderTwo; - private IHost _builderTwo; - private IHost _identityServerBuilder; - private IHost _fooServiceBuilder; - private IHost _barServiceBuilder; - - public AdministrationTests() - { - _httpClient = new HttpClient(); - _httpClientTwo = new HttpClient(); - _ocelotBaseUrl = "http://localhost:5000"; - _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); - } - - [Fact] - public void should_return_response_401_with_call_re_routes_controller() - { - var configuration = new FileConfiguration(); - - this.Given(x => GivenThereIsAConfiguration(configuration)) - .And(x => GivenOcelotIsRunning()) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized)) - .BDDfy(); - } - - [Fact] - public void should_return_response_200_with_call_re_routes_controller() - { - var configuration = new FileConfiguration(); - - this.Given(x => GivenThereIsAConfiguration(configuration)) - .And(x => GivenOcelotIsRunning()) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .BDDfy(); - } - - [Fact] - public void should_return_response_200_with_call_re_routes_controller_using_base_url_added_in_file_config() - { - _httpClient = new HttpClient(); - _ocelotBaseUrl = "http://localhost:5011"; - _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); - - var configuration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - BaseUrl = _ocelotBaseUrl - } - }; - - this.Given(x => GivenThereIsAConfiguration(configuration)) - .And(x => GivenOcelotIsRunningWithNoWebHostBuilder(_ocelotBaseUrl)) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .BDDfy(); - } - - [Fact] - public void should_be_able_to_use_token_from_ocelot_a_on_ocelot_b() - { - var configuration = new FileConfiguration(); - - this.Given(x => GivenThereIsAConfiguration(configuration)) - .And(x => GivenIdentityServerSigningEnvironmentalVariablesAreSet()) - .And(x => GivenOcelotIsRunning()) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenAnotherOcelotIsRunning("http://localhost:5017")) - .When(x => WhenIGetUrlOnTheSecondOcelot("/administration/configuration")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .BDDfy(); - } - - [Fact] - public void should_return_file_configuration() - { - var configuration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - RequestIdKey = "RequestId", - ServiceDiscoveryProvider = new FileServiceDiscoveryProvider - { - Host = "127.0.0.1", - } - }, - ReRoutes = new List() - { - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/", - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 10, - Region = "Geoff" - } - }, - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/test", - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 10, - Region = "Dave" - } - } - } - }; - - this.Given(x => GivenThereIsAConfiguration(configuration)) - .And(x => GivenOcelotIsRunning()) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => ThenTheResponseShouldBe(configuration)) - .BDDfy(); - } - - [Fact] - public void should_get_file_configuration_edit_and_post_updated_version() - { - var initialConfiguration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - }, - ReRoutes = new List() - { - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/" - }, - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/test" - } - }, - }; - - var updatedConfiguration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - }, - ReRoutes = new List() - { - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "http", - DownstreamPathTemplate = "/geoffrey", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/" - }, - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "123.123.123", - Port = 443, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/blooper/{productId}", - UpstreamHttpMethod = new List { "post" }, - UpstreamPathTemplate = "/test" - } - } - }; - - this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) - .And(x => GivenOcelotIsRunning()) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration)) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => ThenTheResponseShouldBe(updatedConfiguration)) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .And(x => ThenTheResponseShouldBe(updatedConfiguration)) - .And(_ => ThenTheConfigurationIsSavedCorrectly(updatedConfiguration)) - .BDDfy(); - } - - private void ThenTheConfigurationIsSavedCorrectly(FileConfiguration expected) - { - var ocelotJsonPath = $"{AppContext.BaseDirectory}ocelot.json"; - var resultText = File.ReadAllText(ocelotJsonPath); - var expectedText = JsonConvert.SerializeObject(expected, Formatting.Indented); - resultText.ShouldBe(expectedText); - - var environmentSpecificPath = $"{AppContext.BaseDirectory}/ocelot.Production.json"; - resultText = File.ReadAllText(environmentSpecificPath); - expectedText = JsonConvert.SerializeObject(expected, Formatting.Indented); - resultText.ShouldBe(expectedText); - } - - [Fact] - public void should_get_file_configuration_edit_and_post_updated_version_redirecting_reroute() - { - var fooPort = 47689; - var barPort = 47690; - - var initialConfiguration = new FileConfiguration - { - ReRoutes = new List() - { - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = fooPort, - } - }, - DownstreamScheme = "http", - DownstreamPathTemplate = "/foo", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/foo" - } - } - }; - - var updatedConfiguration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - }, - ReRoutes = new List() - { - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = barPort, - } - }, - DownstreamScheme = "http", - DownstreamPathTemplate = "/bar", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/foo" - } - } - }; - - this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) - .And(x => GivenThereIsAFooServiceRunningOn($"http://localhost:{fooPort}")) - .And(x => GivenThereIsABarServiceRunningOn($"http://localhost:{barPort}")) - .And(x => GivenOcelotIsRunning()) - .And(x => WhenIGetUrlOnTheApiGateway("/foo")) - .Then(x => ThenTheResponseBodyShouldBe("foo")) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration)) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => ThenTheResponseShouldBe(updatedConfiguration)) - .And(x => WhenIGetUrlOnTheApiGateway("/foo")) - .Then(x => ThenTheResponseBodyShouldBe("bar")) - .When(x => WhenIPostOnTheApiGateway("/administration/configuration", initialConfiguration)) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => ThenTheResponseShouldBe(initialConfiguration)) - .And(x => WhenIGetUrlOnTheApiGateway("/foo")) - .Then(x => ThenTheResponseBodyShouldBe("foo")) - .BDDfy(); - } - - [Fact] - public void should_clear_region() - { - var initialConfiguration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - }, - ReRoutes = new List() - { - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/", - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 10 - } - }, - new FileReRoute() - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 80, - } - }, - DownstreamScheme = "https", - DownstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "get" }, - UpstreamPathTemplate = "/test", - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 10 - } - } - } - }; - - var regionToClear = "gettest"; - - this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) - .And(x => GivenOcelotIsRunning()) - .And(x => GivenIHaveAnOcelotToken("/administration")) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIDeleteOnTheApiGateway($"/administration/outputcache/{regionToClear}")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.NoContent)) - .BDDfy(); - } - - [Fact] - public void should_return_response_200_with_call_re_routes_controller_when_using_own_identity_server_to_secure_admin_area() - { - var configuration = new FileConfiguration(); - - var identityServerRootUrl = "http://localhost:5123"; - - Action options = o => - { - o.Authority = identityServerRootUrl; - o.ApiName = "api"; - o.RequireHttpsMetadata = false; - o.SupportedTokens = SupportedTokens.Both; - o.ApiSecret = "secret"; - }; - - this.Given(x => GivenThereIsAConfiguration(configuration)) - .And(x => GivenThereIsAnIdentityServerOn(identityServerRootUrl, "api")) - .And(x => GivenOcelotIsRunningWithIdentityServerSettings(options)) - .And(x => GivenIHaveAToken(identityServerRootUrl)) - .And(x => GivenIHaveAddedATokenToMyRequest()) - .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) - .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .BDDfy(); - } - - private void GivenIHaveAToken(string url) - { - var formData = new List> - { - new KeyValuePair("client_id", "api"), - new KeyValuePair("client_secret", "secret"), - new KeyValuePair("scope", "api"), - new KeyValuePair("username", "test"), - new KeyValuePair("password", "test"), - new KeyValuePair("grant_type", "password") - }; - var content = new FormUrlEncodedContent(formData); - - using (var httpClient = new HttpClient()) - { - var response = httpClient.PostAsync($"{url}/connect/token", content).Result; - var responseContent = response.Content.ReadAsStringAsync().Result; - response.EnsureSuccessStatusCode(); - _token = JsonConvert.DeserializeObject(responseContent); - } - } - - private void GivenThereIsAnIdentityServerOn(string url, string apiName) - { - _identityServerBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(url) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureServices(services => - { - services.AddLogging(); - services.AddIdentityServer() - .AddDeveloperSigningCredential() - .AddInMemoryApiResources(new List - { - new ApiResource - { - Name = apiName, - Description = apiName, - Enabled = true, - DisplayName = apiName, - Scopes = new List() - { - new Scope(apiName), - }, - }, - }) - .AddInMemoryClients(new List - { - new Client - { - ClientId = apiName, - AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, - ClientSecrets = new List { new Secret("secret".Sha256()) }, - AllowedScopes = new List { apiName }, - AccessTokenType = AccessTokenType.Jwt, - Enabled = true - }, - }) - .AddTestUsers(new List - { - new TestUser - { - Username = "test", - Password = "test", - SubjectId = "1231231" - }, - }); - }) - .Configure(app => - { - app.UseIdentityServer(); - } - ); - }).Build(); - - _identityServerBuilder.Start(); - - using (var httpClient = new HttpClient()) - { - var response = httpClient.GetAsync($"{url}/.well-known/openid-configuration").Result; - response.EnsureSuccessStatusCode(); - } - } - - private void GivenAnotherOcelotIsRunning(string baseUrl) - { - _httpClientTwo.BaseAddress = new Uri(baseUrl); - - _webHostBuilderTwo = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(baseUrl) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); - config.AddJsonFile("ocelot.json", false, false); - config.AddEnvironmentVariables(); - }) - .ConfigureServices(x => - { - x.AddMvc(option => option.EnableEndpointRouting = false); - x.AddOcelot() - .AddAdministration("/administration", "secret"); - }) - .Configure(app => - { - app.UseOcelot().Wait(); - }); - }); - - _builderTwo = _webHostBuilderTwo.Build(); - - _builderTwo.Start(); - } - - private void GivenIdentityServerSigningEnvironmentalVariablesAreSet() - { - Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", "idsrv3test.pfx"); - Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", "idsrv3test"); - } - - private void WhenIGetUrlOnTheSecondOcelot(string url) - { - _httpClientTwo.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); - _response = _httpClientTwo.GetAsync(url).Result; - } - - private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) - { - var json = JsonConvert.SerializeObject(updatedConfiguration); - var content = new StringContent(json); - content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - _response = _httpClient.PostAsync(url, content).Result; - } - - private void ThenTheResponseShouldBe(List expected) - { - var content = _response.Content.ReadAsStringAsync().Result; - var result = JsonConvert.DeserializeObject(content); - result.Value.ShouldBe(expected); - } - - private void ThenTheResponseBodyShouldBe(string expected) - { - var content = _response.Content.ReadAsStringAsync().Result; - content.ShouldBe(expected); - } - - private void ThenTheResponseShouldBe(FileConfiguration expecteds) - { - var response = JsonConvert.DeserializeObject(_response.Content.ReadAsStringAsync().Result); - - response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey); - response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host); - response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port); - - for (var i = 0; i < response.ReRoutes.Count; i++) - { - for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++) - { - var result = response.ReRoutes[i].DownstreamHostAndPorts[j]; - var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j]; - result.Host.ShouldBe(expected.Host); - result.Port.ShouldBe(expected.Port); - } - - response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate); - response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme); - response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].UpstreamPathTemplate); - response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.ReRoutes[i].UpstreamHttpMethod); - } - } - - private void GivenIHaveAddedATokenToMyRequest() - { - _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); - } - - private void GivenIHaveAnOcelotToken(string adminPath) - { - var tokenUrl = $"{adminPath}/connect/token"; - var formData = new List> - { - new KeyValuePair("client_id", "admin"), - new KeyValuePair("client_secret", "secret"), - new KeyValuePair("scope", "admin"), - new KeyValuePair("grant_type", "client_credentials") - }; - var content = new FormUrlEncodedContent(formData); - - var response = _httpClient.PostAsync(tokenUrl, content).Result; - var responseContent = response.Content.ReadAsStringAsync().Result; - response.EnsureSuccessStatusCode(); - _token = JsonConvert.DeserializeObject(responseContent); - var configPath = $"{adminPath}/.well-known/openid-configuration"; - response = _httpClient.GetAsync(configPath).Result; - response.EnsureSuccessStatusCode(); - } - - private void GivenOcelotIsRunningWithIdentityServerSettings(Action configOptions) - { - _webHostBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(_ocelotBaseUrl) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); - config.AddJsonFile("ocelot.json", false, false); - config.AddEnvironmentVariables(); - }) - .ConfigureServices(x => - { - x.AddMvc(option => option.EnableEndpointRouting = false); - x.AddSingleton(_webHostBuilder); - x.AddOcelot() - .AddAdministration("/administration", configOptions); - }) - .Configure(app => - { - app.UseOcelot().Wait(); - }); - }); - - _builder = _webHostBuilder.Build(); - - _builder.Start(); - } - - private void GivenOcelotIsRunning() - { - _webHostBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(_ocelotBaseUrl) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); - config.AddJsonFile("ocelot.json", false, false); - config.AddEnvironmentVariables(); - }) - .ConfigureServices(x => - { - x.AddMvc(s => s.EnableEndpointRouting = false); - x.AddOcelot() - .AddAdministration("/administration", "secret"); - }) - .Configure(app => - { - app.UseOcelot().Wait(); - }); - }); - - _builder = _webHostBuilder.Build(); - - _builder.Start(); - } - - private void GivenOcelotIsRunningWithNoWebHostBuilder(string baseUrl) - { - _webHostBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(_ocelotBaseUrl) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); - config.AddJsonFile("ocelot.json", false, false); - config.AddEnvironmentVariables(); - }) - .ConfigureServices(x => - { - x.AddMvc(option => option.EnableEndpointRouting = false); - x.AddSingleton(_webHostBuilder); - x.AddOcelot() - .AddAdministration("/administration", "secret"); - }) - .Configure(app => - { - app.UseOcelot().Wait(); - }); - }); - - _builder = _webHostBuilder.Build(); - - _builder.Start(); - } - - private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration) - { - var configurationPath = $"{Directory.GetCurrentDirectory()}/ocelot.json"; - - var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); - - if (File.Exists(configurationPath)) - { - File.Delete(configurationPath); - } - - File.WriteAllText(configurationPath, jsonConfiguration); - - var text = File.ReadAllText(configurationPath); - - configurationPath = $"{AppContext.BaseDirectory}/ocelot.json"; - - if (File.Exists(configurationPath)) - { - File.Delete(configurationPath); - } - - File.WriteAllText(configurationPath, jsonConfiguration); - - text = File.ReadAllText(configurationPath); - } - - private void WhenIGetUrlOnTheApiGateway(string url) - { - _response = _httpClient.GetAsync(url).Result; - } - - private void WhenIDeleteOnTheApiGateway(string url) - { - _response = _httpClient.DeleteAsync(url).Result; - } - - private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) - { - _response.StatusCode.ShouldBe(expectedHttpStatusCode); - } - - public void Dispose() - { - Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", ""); - Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", ""); - _builder?.Dispose(); - _httpClient?.Dispose(); - _identityServerBuilder?.Dispose(); - } - - private void GivenThereIsAFooServiceRunningOn(string baseUrl) - { - _fooServiceBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(baseUrl) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .Configure(app => - { - app.UsePathBase("/foo"); - app.Run(async context => - { - context.Response.StatusCode = 200; - await context.Response.WriteAsync("foo"); - }); - }); - }).Build(); - - _fooServiceBuilder.Start(); - } - - private void GivenThereIsABarServiceRunningOn(string baseUrl) - { - _barServiceBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHost(webBuilder => - { - webBuilder.UseUrls(baseUrl) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .Configure(app => - { - app.UsePathBase("/bar"); - app.Run(async context => - { - context.Response.StatusCode = 200; - await context.Response.WriteAsync("bar"); - }); - }); - }).Build(); - - _barServiceBuilder.Start(); - } - } -} +using IdentityServer4.AccessTokenValidation; +using IdentityServer4.Models; +using IdentityServer4.Test; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Newtonsoft.Json; +using Ocelot.Administration; +using Ocelot.Cache; +using Ocelot.Configuration.File; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; +using Shouldly; +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using TestStack.BDDfy; +using Ocelot.Configuration.ChangeTracking; +using Xunit; + +namespace Ocelot.IntegrationTests +{ + public class AdministrationTests : IDisposable + { + private HttpClient _httpClient; + private readonly HttpClient _httpClientTwo; + private HttpResponseMessage _response; + private IHost _builder; + private IHostBuilder _webHostBuilder; + private string _ocelotBaseUrl; + private BearerToken _token; + private IHostBuilder _webHostBuilderTwo; + private IHost _builderTwo; + private IHost _identityServerBuilder; + private IHost _fooServiceBuilder; + private IHost _barServiceBuilder; + + public AdministrationTests() + { + _httpClient = new HttpClient(); + _httpClientTwo = new HttpClient(); + _ocelotBaseUrl = "http://localhost:5000"; + _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); + } + + [Fact] + public void should_return_response_401_with_call_re_routes_controller() + { + var configuration = new FileConfiguration(); + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenOcelotIsRunning()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_with_call_re_routes_controller() + { + var configuration = new FileConfiguration(); + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_with_call_re_routes_controller_using_base_url_added_in_file_config() + { + _httpClient = new HttpClient(); + _ocelotBaseUrl = "http://localhost:5011"; + _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); + + var configuration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + BaseUrl = _ocelotBaseUrl + } + }; + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenOcelotIsRunningWithNoWebHostBuilder(_ocelotBaseUrl)) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + [Fact] + public void should_be_able_to_use_token_from_ocelot_a_on_ocelot_b() + { + var configuration = new FileConfiguration(); + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenIdentityServerSigningEnvironmentalVariablesAreSet()) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenAnotherOcelotIsRunning("http://localhost:5017")) + .When(x => WhenIGetUrlOnTheSecondOcelot("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + [Fact] + public void should_return_file_configuration() + { + var configuration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + RequestIdKey = "RequestId", + ServiceDiscoveryProvider = new FileServiceDiscoveryProvider + { + Host = "127.0.0.1", + } + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/", + FileCacheOptions = new FileCacheOptions + { + TtlSeconds = 10, + Region = "Geoff" + } + }, + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/test", + FileCacheOptions = new FileCacheOptions + { + TtlSeconds = 10, + Region = "Dave" + } + } + } + }; + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => ThenTheResponseShouldBe(configuration)) + .BDDfy(); + } + + [Fact] + public void should_get_file_configuration_edit_and_post_updated_version() + { + var initialConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/" + }, + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/test" + } + }, + }; + + var updatedConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "http", + DownstreamPathTemplate = "/geoffrey", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/" + }, + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "123.123.123", + Port = 443, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/blooper/{productId}", + UpstreamHttpMethod = new List { "post" }, + UpstreamPathTemplate = "/test" + } + } + }; + + this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration)) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => ThenTheResponseShouldBe(updatedConfiguration)) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .And(x => ThenTheResponseShouldBe(updatedConfiguration)) + .And(_ => ThenTheConfigurationIsSavedCorrectly(updatedConfiguration)) + .BDDfy(); + } + + [Fact] + public void should_activate_change_token_when_configuration_is_updated() + { + var configuration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration(), + ReRoutes = new List + { + new FileReRoute + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + }, + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/", + }, + }, + }; + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIPostOnTheApiGateway("/administration/configuration", configuration)) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => TheChangeTokenShouldBeActive()) + .And(x => ThenTheResponseShouldBe(configuration)) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .And(x => ThenTheResponseShouldBe(configuration)) + .And(_ => ThenTheConfigurationIsSavedCorrectly(configuration)) + .BDDfy(); + } + + private void TheChangeTokenShouldBeActive() + { + _builder.Services.GetRequiredService().ChangeToken.HasChanged.ShouldBeTrue(); + } + + private void ThenTheConfigurationIsSavedCorrectly(FileConfiguration expected) + { + var ocelotJsonPath = $"{AppContext.BaseDirectory}ocelot.json"; + var resultText = File.ReadAllText(ocelotJsonPath); + var expectedText = JsonConvert.SerializeObject(expected, Formatting.Indented); + resultText.ShouldBe(expectedText); + + var environmentSpecificPath = $"{AppContext.BaseDirectory}/ocelot.Production.json"; + resultText = File.ReadAllText(environmentSpecificPath); + expectedText = JsonConvert.SerializeObject(expected, Formatting.Indented); + resultText.ShouldBe(expectedText); + } + + [Fact] + public void should_get_file_configuration_edit_and_post_updated_version_redirecting_reroute() + { + var fooPort = 47689; + var barPort = 47690; + + var initialConfiguration = new FileConfiguration + { + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = fooPort, + } + }, + DownstreamScheme = "http", + DownstreamPathTemplate = "/foo", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/foo" + } + } + }; + + var updatedConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = barPort, + } + }, + DownstreamScheme = "http", + DownstreamPathTemplate = "/bar", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/foo" + } + } + }; + + this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) + .And(x => GivenThereIsAFooServiceRunningOn($"http://localhost:{fooPort}")) + .And(x => GivenThereIsABarServiceRunningOn($"http://localhost:{barPort}")) + .And(x => GivenOcelotIsRunning()) + .And(x => WhenIGetUrlOnTheApiGateway("/foo")) + .Then(x => ThenTheResponseBodyShouldBe("foo")) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration)) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => ThenTheResponseShouldBe(updatedConfiguration)) + .And(x => WhenIGetUrlOnTheApiGateway("/foo")) + .Then(x => ThenTheResponseBodyShouldBe("bar")) + .When(x => WhenIPostOnTheApiGateway("/administration/configuration", initialConfiguration)) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => ThenTheResponseShouldBe(initialConfiguration)) + .And(x => WhenIGetUrlOnTheApiGateway("/foo")) + .Then(x => ThenTheResponseBodyShouldBe("foo")) + .BDDfy(); + } + + [Fact] + public void should_clear_region() + { + var initialConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/", + FileCacheOptions = new FileCacheOptions + { + TtlSeconds = 10 + } + }, + new FileReRoute() + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 80, + } + }, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/test", + FileCacheOptions = new FileCacheOptions + { + TtlSeconds = 10 + } + } + } + }; + + var regionToClear = "gettest"; + + this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIDeleteOnTheApiGateway($"/administration/outputcache/{regionToClear}")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.NoContent)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_with_call_re_routes_controller_when_using_own_identity_server_to_secure_admin_area() + { + var configuration = new FileConfiguration(); + + var identityServerRootUrl = "http://localhost:5123"; + + Action options = o => + { + o.Authority = identityServerRootUrl; + o.ApiName = "api"; + o.RequireHttpsMetadata = false; + o.SupportedTokens = SupportedTokens.Both; + o.ApiSecret = "secret"; + }; + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenThereIsAnIdentityServerOn(identityServerRootUrl, "api")) + .And(x => GivenOcelotIsRunningWithIdentityServerSettings(options)) + .And(x => GivenIHaveAToken(identityServerRootUrl)) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + private void GivenIHaveAToken(string url) + { + var formData = new List> + { + new KeyValuePair("client_id", "api"), + new KeyValuePair("client_secret", "secret"), + new KeyValuePair("scope", "api"), + new KeyValuePair("username", "test"), + new KeyValuePair("password", "test"), + new KeyValuePair("grant_type", "password") + }; + var content = new FormUrlEncodedContent(formData); + + using (var httpClient = new HttpClient()) + { + var response = httpClient.PostAsync($"{url}/connect/token", content).Result; + var responseContent = response.Content.ReadAsStringAsync().Result; + response.EnsureSuccessStatusCode(); + _token = JsonConvert.DeserializeObject(responseContent); + } + } + + private void GivenThereIsAnIdentityServerOn(string url, string apiName) + { + _identityServerBuilder = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureServices(services => + { + services.AddLogging(); + services.AddIdentityServer() + .AddDeveloperSigningCredential() + .AddInMemoryApiResources(new List + { + new ApiResource + { + Name = apiName, + Description = apiName, + Enabled = true, + DisplayName = apiName, + Scopes = new List() + { + new Scope(apiName), + }, + }, + }) + .AddInMemoryClients(new List + { + new Client + { + ClientId = apiName, + AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, + ClientSecrets = new List { new Secret("secret".Sha256()) }, + AllowedScopes = new List { apiName }, + AccessTokenType = AccessTokenType.Jwt, + Enabled = true + }, + }) + .AddTestUsers(new List + { + new TestUser + { + Username = "test", + Password = "test", + SubjectId = "1231231" + }, + }); + }) + .Configure(app => + { + app.UseIdentityServer(); + } + ); + }).Build(); + + _identityServerBuilder.Start(); + + using (var httpClient = new HttpClient()) + { + var response = httpClient.GetAsync($"{url}/.well-known/openid-configuration").Result; + response.EnsureSuccessStatusCode(); + } + } + + private void GivenAnotherOcelotIsRunning(string baseUrl) + { + _httpClientTwo.BaseAddress = new Uri(baseUrl); + + _webHostBuilderTwo = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(baseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); + config.AddJsonFile("ocelot.json", false, false); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => + { + x.AddMvc(option => option.EnableEndpointRouting = false); + x.AddOcelot() + .AddAdministration("/administration", "secret"); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); + }); + + _builderTwo = _webHostBuilderTwo.Build(); + + _builderTwo.Start(); + } + + private void GivenIdentityServerSigningEnvironmentalVariablesAreSet() + { + Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", "idsrv3test.pfx"); + Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", "idsrv3test"); + } + + private void WhenIGetUrlOnTheSecondOcelot(string url) + { + _httpClientTwo.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); + _response = _httpClientTwo.GetAsync(url).Result; + } + + private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) + { + var json = JsonConvert.SerializeObject(updatedConfiguration); + var content = new StringContent(json); + content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + _response = _httpClient.PostAsync(url, content).Result; + } + + private void ThenTheResponseShouldBe(List expected) + { + var content = _response.Content.ReadAsStringAsync().Result; + var result = JsonConvert.DeserializeObject(content); + result.Value.ShouldBe(expected); + } + + private void ThenTheResponseBodyShouldBe(string expected) + { + var content = _response.Content.ReadAsStringAsync().Result; + content.ShouldBe(expected); + } + + private void ThenTheResponseShouldBe(FileConfiguration expecteds) + { + var response = JsonConvert.DeserializeObject(_response.Content.ReadAsStringAsync().Result); + + response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey); + response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host); + response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port); + + for (var i = 0; i < response.ReRoutes.Count; i++) + { + for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++) + { + var result = response.ReRoutes[i].DownstreamHostAndPorts[j]; + var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j]; + result.Host.ShouldBe(expected.Host); + result.Port.ShouldBe(expected.Port); + } + + response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate); + response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme); + response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].UpstreamPathTemplate); + response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.ReRoutes[i].UpstreamHttpMethod); + } + } + + private void GivenIHaveAddedATokenToMyRequest() + { + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); + } + + private void GivenIHaveAnOcelotToken(string adminPath) + { + var tokenUrl = $"{adminPath}/connect/token"; + var formData = new List> + { + new KeyValuePair("client_id", "admin"), + new KeyValuePair("client_secret", "secret"), + new KeyValuePair("scope", "admin"), + new KeyValuePair("grant_type", "client_credentials") + }; + var content = new FormUrlEncodedContent(formData); + + var response = _httpClient.PostAsync(tokenUrl, content).Result; + var responseContent = response.Content.ReadAsStringAsync().Result; + response.EnsureSuccessStatusCode(); + _token = JsonConvert.DeserializeObject(responseContent); + var configPath = $"{adminPath}/.well-known/openid-configuration"; + response = _httpClient.GetAsync(configPath).Result; + response.EnsureSuccessStatusCode(); + } + + private void GivenOcelotIsRunningWithIdentityServerSettings(Action configOptions) + { + _webHostBuilder = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(_ocelotBaseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); + config.AddJsonFile("ocelot.json", false, false); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => + { + x.AddMvc(option => option.EnableEndpointRouting = false); + x.AddSingleton(_webHostBuilder); + x.AddOcelot() + .AddAdministration("/administration", configOptions); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); + }); + + _builder = _webHostBuilder.Build(); + + _builder.Start(); + } + + private void GivenOcelotIsRunning() + { + _webHostBuilder = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(_ocelotBaseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); + config.AddJsonFile("ocelot.json", false, false); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => + { + x.AddMvc(s => s.EnableEndpointRouting = false); + x.AddOcelot() + .AddAdministration("/administration", "secret"); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); + }); + + _builder = _webHostBuilder.Build(); + + _builder.Start(); + } + + private void GivenOcelotIsRunningWithNoWebHostBuilder(string baseUrl) + { + _webHostBuilder = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(_ocelotBaseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); + config.AddJsonFile("ocelot.json", false, false); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => + { + x.AddMvc(option => option.EnableEndpointRouting = false); + x.AddSingleton(_webHostBuilder); + x.AddOcelot() + .AddAdministration("/administration", "secret"); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); + }); + + _builder = _webHostBuilder.Build(); + + _builder.Start(); + } + + private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration) + { + var configurationPath = $"{Directory.GetCurrentDirectory()}/ocelot.json"; + + var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); + + if (File.Exists(configurationPath)) + { + File.Delete(configurationPath); + } + + File.WriteAllText(configurationPath, jsonConfiguration); + + var text = File.ReadAllText(configurationPath); + + configurationPath = $"{AppContext.BaseDirectory}/ocelot.json"; + + if (File.Exists(configurationPath)) + { + File.Delete(configurationPath); + } + + File.WriteAllText(configurationPath, jsonConfiguration); + + text = File.ReadAllText(configurationPath); + } + + private void WhenIGetUrlOnTheApiGateway(string url) + { + _response = _httpClient.GetAsync(url).Result; + } + + private void WhenIDeleteOnTheApiGateway(string url) + { + _response = _httpClient.DeleteAsync(url).Result; + } + + private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) + { + _response.StatusCode.ShouldBe(expectedHttpStatusCode); + } + + public void Dispose() + { + Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", ""); + Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", ""); + _builder?.Dispose(); + _httpClient?.Dispose(); + _identityServerBuilder?.Dispose(); + } + + private void GivenThereIsAFooServiceRunningOn(string baseUrl) + { + _fooServiceBuilder = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(baseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .Configure(app => + { + app.UsePathBase("/foo"); + app.Run(async context => + { + context.Response.StatusCode = 200; + await context.Response.WriteAsync("foo"); + }); + }); + }).Build(); + + _fooServiceBuilder.Start(); + } + + private void GivenThereIsABarServiceRunningOn(string baseUrl) + { + _barServiceBuilder = Host.CreateDefaultBuilder() + .ConfigureWebHost(webBuilder => + { + webBuilder.UseUrls(baseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .Configure(app => + { + app.UsePathBase("/bar"); + app.Run(async context => + { + context.Response.StatusCode = 200; + await context.Response.WriteAsync("bar"); + }); + }); + }).Build(); + + _barServiceBuilder.Start(); + } + } +} diff --git a/test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSourceTests.cs b/test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSourceTests.cs new file mode 100644 index 00000000..dfcf23f2 --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenSourceTests.cs @@ -0,0 +1,35 @@ +namespace Ocelot.UnitTests.Configuration.ChangeTracking +{ + using Ocelot.Configuration.ChangeTracking; + using Shouldly; + using TestStack.BDDfy; + using Xunit; + + public class OcelotConfigurationChangeTokenSourceTests + { + private readonly IOcelotConfigurationChangeTokenSource _source; + + public OcelotConfigurationChangeTokenSourceTests() + { + _source = new OcelotConfigurationChangeTokenSource(); + } + + [Fact] + public void should_activate_change_token() + { + this.Given(_ => GivenIActivateTheChangeTokenSource()) + .Then(_ => ThenTheChangeTokenShouldBeActivated()) + .BDDfy(); + } + + private void GivenIActivateTheChangeTokenSource() + { + _source.Activate(); + } + + private void ThenTheChangeTokenShouldBeActivated() + { + _source.ChangeToken.HasChanged.ShouldBeTrue(); + } + } +} diff --git a/test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenTests.cs b/test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenTests.cs new file mode 100644 index 00000000..0b651030 --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/ChangeTracking/OcelotConfigurationChangeTokenTests.cs @@ -0,0 +1,91 @@ +using Xunit; + +namespace Ocelot.UnitTests.Configuration.ChangeTracking +{ + using System; + using Shouldly; + using Ocelot.Configuration.ChangeTracking; + using TestStack.BDDfy; + + public class OcelotConfigurationChangeTokenTests + { + [Fact] + public void should_call_callback_with_state() + { + this.Given(_ => GivenIHaveAChangeToken()) + .And(_ => AndIRegisterACallback()) + .Then(_ => ThenIShouldGetADisposableWrapper()) + .Given(_ => GivenIActivateTheToken()) + .Then(_ => ThenTheCallbackShouldBeCalled()) + .BDDfy(); + } + + [Fact] + public void should_not_call_callback_if_it_is_disposed() + { + this.Given(_ => GivenIHaveAChangeToken()) + .And(_ => AndIRegisterACallback()) + .Then(_ => ThenIShouldGetADisposableWrapper()) + .And(_ => GivenIActivateTheToken()) + .And(_ => AndIDisposeTheCallbackWrapper()) + .And(_ => GivenIActivateTheToken()) + .Then(_ => ThenTheCallbackShouldNotBeCalled()) + .BDDfy(); + } + + private OcelotConfigurationChangeToken _changeToken; + private IDisposable _callbackWrapper; + private int _callbackCounter; + private readonly object _callbackInitialState = new object(); + private object _callbackState; + + private void Callback(object state) + { + _callbackCounter++; + _callbackState = state; + _changeToken.HasChanged.ShouldBeTrue(); + } + + private void GivenIHaveAChangeToken() + { + _changeToken = new OcelotConfigurationChangeToken(); + } + + private void AndIRegisterACallback() + { + _callbackWrapper = _changeToken.RegisterChangeCallback(Callback, _callbackInitialState); + } + + private void ThenIShouldGetADisposableWrapper() + { + _callbackWrapper.ShouldNotBeNull(); + } + + private void GivenIActivateTheToken() + { + _callbackCounter = 0; + _callbackState = null; + _changeToken.Activate(); + } + + private void ThenTheCallbackShouldBeCalled() + { + _callbackCounter.ShouldBe(1); + _callbackState.ShouldNotBeNull(); + _callbackState.ShouldBeSameAs(_callbackInitialState); + } + + private void ThenTheCallbackShouldNotBeCalled() + { + _callbackCounter.ShouldBe(0); + _callbackState.ShouldBeNull(); + } + + private void AndIDisposeTheCallbackWrapper() + { + _callbackState = null; + _callbackCounter = 0; + _callbackWrapper.Dispose(); + } + } +} diff --git a/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs index b2eb06b5..7c00c2f4 100644 --- a/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs @@ -3,6 +3,7 @@ namespace Ocelot.UnitTests.Configuration using Microsoft.AspNetCore.Hosting; using Moq; using Newtonsoft.Json; + using Ocelot.Configuration.ChangeTracking; using Ocelot.Configuration.File; using Ocelot.Configuration.Repository; using Shouldly; @@ -16,6 +17,7 @@ namespace Ocelot.UnitTests.Configuration public class DiskFileConfigurationRepositoryTests : IDisposable { private readonly Mock _hostingEnvironment; + private readonly Mock _changeTokenSource; private IFileConfigurationRepository _repo; private string _environmentSpecificPath; private string _ocelotJsonPath; @@ -35,7 +37,9 @@ namespace Ocelot.UnitTests.Configuration _semaphore.Wait(); _hostingEnvironment = new Mock(); _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName); - _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object); + _changeTokenSource = new Mock(MockBehavior.Strict); + _changeTokenSource.Setup(m => m.Activate()); + _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object); } [Fact] @@ -70,6 +74,7 @@ namespace Ocelot.UnitTests.Configuration .When(_ => WhenISetTheConfiguration()) .Then(_ => ThenTheConfigurationIsStoredAs(config)) .And(_ => ThenTheConfigurationJsonIsIndented(config)) + .And(x => AndTheChangeTokenIsActivated()) .BDDfy(); } @@ -117,7 +122,7 @@ namespace Ocelot.UnitTests.Configuration { _environmentName = null; _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName); - _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object); + _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object); } private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration) @@ -210,6 +215,11 @@ namespace Ocelot.UnitTests.Configuration } } + private void AndTheChangeTokenIsActivated() + { + _changeTokenSource.Verify(m => m.Activate(), Times.Once); + } + private FileConfiguration FakeFileConfigurationForSet() { var reRoutes = new List @@ -222,11 +232,11 @@ namespace Ocelot.UnitTests.Configuration { Host = "123.12.12.12", Port = 80, - } + }, }, DownstreamScheme = "https", - DownstreamPathTemplate = "/asdfs/test/{test}" - } + DownstreamPathTemplate = "/asdfs/test/{test}", + }, }; var globalConfiguration = new FileGlobalConfiguration diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs index 53289434..85fdd53c 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs @@ -9,6 +9,7 @@ using Ocelot.Errors; using Ocelot.Responses; using Shouldly; using System.Collections.Generic; +using Ocelot.Configuration.ChangeTracking; using TestStack.BDDfy; using Xunit; @@ -21,7 +22,7 @@ namespace Ocelot.UnitTests.Configuration private Mock _configRepo; private Mock _configCreator; private Response _configuration; - private object _result; + private object _result; private Mock _repo; public FileConfigurationSetterTests() @@ -104,8 +105,7 @@ namespace Ocelot.UnitTests.Configuration private void ThenTheConfigurationRepositoryIsCalledCorrectly() { - _configRepo - .Verify(x => x.AddOrReplace(_configuration.Data), Times.Once); + _configRepo.Verify(x => x.AddOrReplace(_configuration.Data), Times.Once); } } -} +} diff --git a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs index 30f89916..508f25e0 100644 --- a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs @@ -5,6 +5,8 @@ using Ocelot.Responses; using Shouldly; using System; using System.Collections.Generic; +using Moq; +using Ocelot.Configuration.ChangeTracking; using TestStack.BDDfy; using Xunit; @@ -16,10 +18,13 @@ namespace Ocelot.UnitTests.Configuration private IInternalConfiguration _config; private Response _result; private Response _getResult; + private readonly Mock _changeTokenSource; public InMemoryConfigurationRepositoryTests() { - _repo = new InMemoryInternalConfigurationRepository(); + _changeTokenSource = new Mock(MockBehavior.Strict); + _changeTokenSource.Setup(m => m.Activate()); + _repo = new InMemoryInternalConfigurationRepository(_changeTokenSource.Object); } [Fact] @@ -28,6 +33,7 @@ namespace Ocelot.UnitTests.Configuration this.Given(x => x.GivenTheConfigurationIs(new FakeConfig("initial", "adminath"))) .When(x => x.WhenIAddOrReplaceTheConfig()) .Then(x => x.ThenNoErrorsAreReturned()) + .And(x => AndTheChangeTokenIsActivated()) .BDDfy(); } @@ -71,6 +77,11 @@ namespace Ocelot.UnitTests.Configuration _result.IsError.ShouldBeFalse(); } + private void AndTheChangeTokenIsActivated() + { + _changeTokenSource.Verify(m => m.Activate(), Times.Once); + } + private class FakeConfig : IInternalConfiguration { private readonly string _downstreamTemplatePath; @@ -111,4 +122,4 @@ namespace Ocelot.UnitTests.Configuration public HttpHandlerOptions HttpHandlerOptions { get; } } } -} +} From 4147aee587d18b0fc6da6295d2c01ca0128782e2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 9 Feb 2020 15:05:07 +0100 Subject: [PATCH 10/24] RABC typo fix (#1123) --- docs/features/kubernetes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/kubernetes.rst b/docs/features/kubernetes.rst index 75568ec4..088ea3ba 100644 --- a/docs/features/kubernetes.rst +++ b/docs/features/kubernetes.rst @@ -14,7 +14,7 @@ Then add the following to your ConfigureServices method. s.AddOcelot() .AddKubernetes(); -If you have services deployed in kubernetes you will normally use the naming service to access them. Default usePodServiceAccount = True, which means that ServiceAccount using Pod to access the service of the k8s cluster needs to be ServiceAccount based on RABC authorization +If you have services deployed in kubernetes you will normally use the naming service to access them. Default usePodServiceAccount = True, which means that ServiceAccount using Pod to access the service of the k8s cluster needs to be ServiceAccount based on RBAC authorization .. code-block::csharp public static class OcelotBuilderExtensions From 6bd903bc8782eef72230684daaec7b1832d63d35 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Sun, 9 Feb 2020 15:37:27 +0000 Subject: [PATCH 11/24] tests passing --- samples/OcelotKube/ApiGateway/Startup.cs | 3 +- .../OcelotKube/DownstreamService/Startup.cs | 3 +- .../DownstreamMethodTransformerMiddleware.cs | 27 --------------- .../Pipeline/OcelotPipelineExtensions.cs | 4 --- .../DownstreamRequestInitialiserMiddleware.cs | 5 +++ .../HttpRequestBuilderMiddlewareExtensions.cs | 5 +-- ...streamRequestInitialiserMiddlewareTests.cs | 34 +++++++++++++++++-- 7 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs diff --git a/samples/OcelotKube/ApiGateway/Startup.cs b/samples/OcelotKube/ApiGateway/Startup.cs index d7b2473c..3e37ffa3 100644 --- a/samples/OcelotKube/ApiGateway/Startup.cs +++ b/samples/OcelotKube/ApiGateway/Startup.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Ocelot.DependencyInjection; using Ocelot.Middleware; using Ocelot.Provider.Kubernetes; @@ -18,7 +19,7 @@ namespace ApiGateway } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { diff --git a/samples/OcelotKube/DownstreamService/Startup.cs b/samples/OcelotKube/DownstreamService/Startup.cs index 9a927a37..a8abb5d4 100644 --- a/samples/OcelotKube/DownstreamService/Startup.cs +++ b/samples/OcelotKube/DownstreamService/Startup.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -28,7 +29,7 @@ namespace DownstreamService } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { diff --git a/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs b/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs deleted file mode 100644 index a17e9167..00000000 --- a/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Ocelot.Logging; -using Ocelot.Middleware; -using System.Threading.Tasks; - -namespace Ocelot.DownstreamMethodTransformer.Middleware -{ - public class DownstreamMethodTransformerMiddleware : OcelotMiddleware - { - private readonly OcelotRequestDelegate _next; - - public DownstreamMethodTransformerMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory) - : base(loggerFactory.CreateLogger()) - { - _next = next; - } - - public async Task Invoke(DownstreamContext context) - { - if (context.DownstreamReRoute.DownstreamHttpMethod != null) - { - context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod; - } - - await _next.Invoke(context); - } - } -} diff --git a/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs b/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs index 02ee07f4..d52cfb0d 100644 --- a/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs +++ b/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs @@ -2,7 +2,6 @@ using Ocelot.Authorisation.Middleware; using Ocelot.Cache.Middleware; using Ocelot.Claims.Middleware; -using Ocelot.DownstreamMethodTransformer.Middleware; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Errors.Middleware; @@ -69,9 +68,6 @@ namespace Ocelot.Middleware.Pipeline // Initialises downstream request builder.UseDownstreamRequestInitialiser(); - //change Http Method - builder.UseMiddleware(); - // We check whether the request is ratelimit, and if there is no continue processing builder.UseRateLimiting(); diff --git a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs index 442bb4b9..c9f6f859 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs @@ -34,6 +34,11 @@ namespace Ocelot.Request.Middleware context.DownstreamRequest = _creator.Create(downstreamRequest.Data); + if (!string.IsNullOrEmpty(context.DownstreamReRoute?.DownstreamHttpMethod)) + { + context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod; + } + await _next.Invoke(context); } } diff --git a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs index e6eff7e0..a2931cbb 100644 --- a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs +++ b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs @@ -1,5 +1,3 @@ -using Microsoft.AspNetCore.Builder; -using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Middleware.Pipeline; namespace Ocelot.Request.Middleware @@ -8,8 +6,7 @@ namespace Ocelot.Request.Middleware { public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder) { - return builder.UseMiddleware() - .UseMiddleware(); + return builder.UseMiddleware(); } } } diff --git a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs index 93d963b6..aadc551c 100644 --- a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs @@ -1,6 +1,4 @@ -using Ocelot.Middleware; - -namespace Ocelot.UnitTests.Request +namespace Ocelot.UnitTests.Request { using Microsoft.AspNetCore.Http; using Moq; @@ -9,6 +7,8 @@ namespace Ocelot.UnitTests.Request using Ocelot.Request.Creator; using Ocelot.Request.Mapper; using Ocelot.Request.Middleware; + using Ocelot.Configuration.Builder; + using Ocelot.Middleware; using Ocelot.Responses; using Shouldly; using System.Net.Http; @@ -65,6 +65,24 @@ namespace Ocelot.UnitTests.Request .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) .And(_ => ThenTheDownstreamRequestIsStored()) .And(_ => ThenTheNextMiddlewareIsInvoked()) + .And(_ => ThenTheDownstreamRequestMethodIs("GET")) + .BDDfy(); + } + + [Theory] + [InlineData("POST", "POST")] + [InlineData(null, "GET")] + [InlineData("", "GET")] + public void Should_map_downstream_reroute_method_to_downstream_request(string input, string expected) + { + this.Given(_ => GivenTheHttpContextContainsARequest()) + .And(_ => GivenTheDownstreamReRouteMethodIs(input)) + .And(_ => GivenTheMapperWillReturnAMappedRequest()) + .When(_ => WhenTheMiddlewareIsInvoked()) + .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) + .And(_ => ThenTheDownstreamRequestIsStored()) + .And(_ => ThenTheNextMiddlewareIsInvoked()) + .And(_ => ThenTheDownstreamRequestMethodIs(expected)) .BDDfy(); } @@ -80,6 +98,16 @@ namespace Ocelot.UnitTests.Request .BDDfy(); } + private void GivenTheDownstreamReRouteMethodIs(string input) + { + _downstreamContext.DownstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build(); + } + + private void ThenTheDownstreamRequestMethodIs(string expected) + { + _downstreamContext.DownstreamRequest.Method.ShouldBe(expected); + } + private void GivenTheHttpContextContainsARequest() { _httpContext From 0c5e09d18f55d89aa2c279d2bd6c4ac04c480db9 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Sun, 9 Feb 2020 15:51:22 +0000 Subject: [PATCH 12/24] moved logic to request mapper and public setter gone --- src/Ocelot/Request/Mapper/IRequestMapper.cs | 3 +- src/Ocelot/Request/Mapper/RequestMapper.cs | 12 +++-- .../Request/Middleware/DownstreamRequest.cs | 2 +- .../DownstreamRequestInitialiserMiddleware.cs | 7 +-- ...streamRequestInitialiserMiddlewareTests.cs | 22 +++------ .../Request/Mapper/RequestMapperTests.cs | 47 ++++++++++++++----- 6 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/Ocelot/Request/Mapper/IRequestMapper.cs b/src/Ocelot/Request/Mapper/IRequestMapper.cs index d16a2d06..cd075c5d 100644 --- a/src/Ocelot/Request/Mapper/IRequestMapper.cs +++ b/src/Ocelot/Request/Mapper/IRequestMapper.cs @@ -1,12 +1,13 @@ namespace Ocelot.Request.Mapper { using Microsoft.AspNetCore.Http; + using Ocelot.Configuration; using Ocelot.Responses; using System.Net.Http; using System.Threading.Tasks; public interface IRequestMapper { - Task> Map(HttpRequest request); + Task> Map(HttpRequest request, DownstreamReRoute downstreamReRoute); } } diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index f669c286..e050f1a6 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.Extensions.Primitives; + using Ocelot.Configuration; using Ocelot.Responses; using System; using System.Collections.Generic; @@ -15,14 +16,14 @@ { private readonly string[] _unsupportedHeaders = { "host" }; - public async Task> Map(HttpRequest request) + public async Task> Map(HttpRequest request, DownstreamReRoute downstreamReRoute) { try { var requestMessage = new HttpRequestMessage() { Content = await MapContent(request), - Method = MapMethod(request), + Method = MapMethod(request, downstreamReRoute), RequestUri = MapUri(request) }; @@ -71,8 +72,13 @@ } } - private HttpMethod MapMethod(HttpRequest request) + private HttpMethod MapMethod(HttpRequest request, DownstreamReRoute downstreamReRoute) { + if (!string.IsNullOrEmpty(downstreamReRoute?.DownstreamHttpMethod)) + { + return new HttpMethod(downstreamReRoute.DownstreamHttpMethod); + } + return new HttpMethod(request.Method); } diff --git a/src/Ocelot/Request/Middleware/DownstreamRequest.cs b/src/Ocelot/Request/Middleware/DownstreamRequest.cs index 24d96cfe..cf973d94 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequest.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequest.cs @@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware public HttpRequestHeaders Headers { get; } - public string Method { get; set; } + public string Method { get; } public string OriginalString { get; } diff --git a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs index c9f6f859..83a2ecb9 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs @@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware public async Task Invoke(DownstreamContext context) { - var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request); + var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request, context.DownstreamReRoute); if (downstreamRequest.IsError) { @@ -34,11 +34,6 @@ namespace Ocelot.Request.Middleware context.DownstreamRequest = _creator.Create(downstreamRequest.Data); - if (!string.IsNullOrEmpty(context.DownstreamReRoute?.DownstreamHttpMethod)) - { - context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod; - } - await _next.Invoke(context); } } diff --git a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs index aadc551c..13bf024b 100644 --- a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs @@ -12,6 +12,7 @@ using Ocelot.Responses; using Shouldly; using System.Net.Http; + using Ocelot.Configuration; using TestStack.BDDfy; using Xunit; @@ -69,20 +70,16 @@ .BDDfy(); } - [Theory] - [InlineData("POST", "POST")] - [InlineData(null, "GET")] - [InlineData("", "GET")] - public void Should_map_downstream_reroute_method_to_downstream_request(string input, string expected) + [Fact] + public void Should_map_downstream_reroute_method_to_downstream_request() { this.Given(_ => GivenTheHttpContextContainsARequest()) - .And(_ => GivenTheDownstreamReRouteMethodIs(input)) .And(_ => GivenTheMapperWillReturnAMappedRequest()) .When(_ => WhenTheMiddlewareIsInvoked()) .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) .And(_ => ThenTheDownstreamRequestIsStored()) .And(_ => ThenTheNextMiddlewareIsInvoked()) - .And(_ => ThenTheDownstreamRequestMethodIs(expected)) + .And(_ => ThenTheDownstreamRequestMethodIs("GET")) .BDDfy(); } @@ -98,11 +95,6 @@ .BDDfy(); } - private void GivenTheDownstreamReRouteMethodIs(string input) - { - _downstreamContext.DownstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build(); - } - private void ThenTheDownstreamRequestMethodIs(string expected) { _downstreamContext.DownstreamRequest.Method.ShouldBe(expected); @@ -120,7 +112,7 @@ _mappedRequest = new OkResponse(new HttpRequestMessage(HttpMethod.Get, "http://www.bbc.co.uk")); _requestMapper - .Setup(rm => rm.Map(It.IsAny())) + .Setup(rm => rm.Map(It.IsAny(), It.IsAny())) .ReturnsAsync(_mappedRequest); } @@ -129,7 +121,7 @@ _mappedRequest = new ErrorResponse(new UnmappableRequestError(new System.Exception("boooom!"))); _requestMapper - .Setup(rm => rm.Map(It.IsAny())) + .Setup(rm => rm.Map(It.IsAny(), It.IsAny())) .ReturnsAsync(_mappedRequest); } @@ -140,7 +132,7 @@ private void ThenTheContexRequestIsMappedToADownstreamRequest() { - _requestMapper.Verify(rm => rm.Map(_httpRequest.Object), Times.Once); + _requestMapper.Verify(rm => rm.Map(_httpRequest.Object, _downstreamContext.DownstreamReRoute), Times.Once); } private void ThenTheDownstreamRequestIsStored() diff --git a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs index ac81a4d4..77091fc6 100644 --- a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs +++ b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs @@ -13,6 +13,8 @@ using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; + using Ocelot.Configuration; + using Ocelot.Configuration.Builder; using TestStack.BDDfy; using Xunit; @@ -27,6 +29,8 @@ private List> _inputHeaders = null; + private DownstreamReRoute _downstreamReRoute; + public RequestMapperTests() { _httpContext = new DefaultHttpContext(); @@ -82,6 +86,21 @@ .BDDfy(); } + [Theory] + [InlineData("", "GET")] + [InlineData(null, "GET")] + [InlineData("POST", "POST")] + public void Should_use_downstream_reroute_method_if_set(string input, string expected) + { + this.Given(_ => GivenTheInputRequestHasMethod("GET")) + .And(_ => GivenTheDownstreamReRouteMethodIs(input)) + .And(_ => GivenTheInputRequestHasAValidUri()) + .When(_ => WhenMapped()) + .Then(_ => ThenNoErrorIsReturned()) + .And(_ => ThenTheMappedRequestHasMethod(expected)) + .BDDfy(); + } + [Fact] public void Should_map_all_headers() { @@ -154,16 +173,6 @@ .BDDfy(); } - private void GivenTheInputRequestHasNoContentLength() - { - _inputRequest.ContentLength = null; - } - - private void GivenTheInputRequestHasNoContentType() - { - _inputRequest.ContentType = null; - } - [Fact] public void Should_map_content_headers() { @@ -212,6 +221,22 @@ .BDDfy(); } + private void GivenTheDownstreamReRouteMethodIs(string input) + { + _downstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build(); + } + + private void GivenTheInputRequestHasNoContentLength() + { + _inputRequest.ContentLength = null; + } + + private void GivenTheInputRequestHasNoContentType() + { + _inputRequest.ContentType = null; + } + + private void ThenTheContentHeadersAreNotAddedToNonContentHeaders() { _mappedRequest.Data.Headers.ShouldNotContain(x => x.Key == "Content-Disposition"); @@ -380,7 +405,7 @@ private async Task WhenMapped() { - _mappedRequest = await _requestMapper.Map(_inputRequest); + _mappedRequest = await _requestMapper.Map(_inputRequest, _downstreamReRoute); } private void ThenNoErrorIsReturned() From 7e807208812ffcc9cfb7929b2f2490b606adf39c Mon Sep 17 00:00:00 2001 From: TomPallister Date: Sun, 9 Feb 2020 16:15:39 +0000 Subject: [PATCH 13/24] acceptance tests --- test/Ocelot.AcceptanceTests/MethodTests.cs | 158 +++++++++++++++++++++ test/Ocelot.AcceptanceTests/Steps.cs | 12 ++ 2 files changed, 170 insertions(+) create mode 100644 test/Ocelot.AcceptanceTests/MethodTests.cs diff --git a/test/Ocelot.AcceptanceTests/MethodTests.cs b/test/Ocelot.AcceptanceTests/MethodTests.cs new file mode 100644 index 00000000..abfbfecf --- /dev/null +++ b/test/Ocelot.AcceptanceTests/MethodTests.cs @@ -0,0 +1,158 @@ +namespace Ocelot.AcceptanceTests +{ + using Microsoft.AspNetCore.Http; + using Ocelot.Configuration.File; + using System; + using System.Collections.Generic; + using System.IO; + using System.Net; + using System.Net.Http; + using TestStack.BDDfy; + using Xunit; + + public class MethodTests : IDisposable + { + private readonly Steps _steps; + private readonly ServiceHandler _serviceHandler; + + public MethodTests() + { + _serviceHandler = new ServiceHandler(); + _steps = new Steps(); + } + + [Fact] + public void should_return_response_200_when_get_converted_to_post() + { + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "http", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 53171, + }, + }, + DownstreamHttpMethod = "POST", + }, + }, + }; + + this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53171/", "/", "POST")) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_when_get_converted_to_post_with_content() + { + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "http", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 53271, + }, + }, + DownstreamHttpMethod = "POST", + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53271/", "/", "POST")) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_when_get_converted_to_get_with_content() + { + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "http", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Post" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 53272, + }, + }, + DownstreamHttpMethod = "GET", + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53272/", "/", "GET")) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIPostUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) + .BDDfy(); + } + + private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string expected) + { + _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context => + { + if (context.Request.Method == expected) + { + context.Response.StatusCode = 200; + var reader = new StreamReader(context.Request.Body); + var body = await reader.ReadToEndAsync(); + await context.Response.WriteAsync(body); + } + else + { + context.Response.StatusCode = 500; + } + }); + } + + public void Dispose() + { + _serviceHandler.Dispose(); + _steps.Dispose(); + } + } +} diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index 9bfb2091..60a770f7 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -901,6 +901,18 @@ _response = _ocelotClient.GetAsync(url).Result; } + public void WhenIGetUrlOnTheApiGateway(string url, HttpContent content) + { + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url) {Content = content}; + _response = _ocelotClient.SendAsync(httpRequestMessage).Result; + } + + public void WhenIPostUrlOnTheApiGateway(string url, HttpContent content) + { + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = content }; + _response = _ocelotClient.SendAsync(httpRequestMessage).Result; + } + public void WhenIGetUrlOnTheApiGateway(string url, string cookie, string value) { var request = _ocelotServer.CreateRequest(url); From 89b2decf5042afe0cc884e0a0f6caaae5e77ca9a Mon Sep 17 00:00:00 2001 From: TomPallister Date: Sun, 9 Feb 2020 16:52:59 +0000 Subject: [PATCH 14/24] added docs for http method transformation --- docs/features/configuration.rst | 1 + docs/features/methodtransformation.rst | 28 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 docs/features/methodtransformation.rst diff --git a/docs/features/configuration.rst b/docs/features/configuration.rst index 7983af39..fe399740 100644 --- a/docs/features/configuration.rst +++ b/docs/features/configuration.rst @@ -24,6 +24,7 @@ Here is an example ReRoute configuration, You don't need to set all of these thi "UpstreamHttpMethod": [ "Get" ], + "DownstreamHttpMethod": "". "AddHeadersToRequest": {}, "AddClaimsToRequest": {}, "RouteClaimsRequirement": {}, diff --git a/docs/features/methodtransformation.rst b/docs/features/methodtransformation.rst new file mode 100644 index 00000000..b94ebce4 --- /dev/null +++ b/docs/features/methodtransformation.rst @@ -0,0 +1,28 @@ +HTTP Method Transformation +========================== + +Ocelot allows the user to change the HTTP request method that will be used when making a request to a downstream service. + +This achieved by setting the following ReRoute configuration: + +.. code-block:: json + +{ + "DownstreamPathTemplate": "/{url}", + "UpstreamPathTemplate": "/{url}", + "UpstreamHttpMethod": [ + "Get" + ], + "DownstreamHttpMethod": "POST", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 53271 + } + ], +} + +The key property here is DownstreamHttpMethod which is set as POST and the ReRoute will only match on GET as set by UpstreamHttpMethod. + +This feature can be useful when interacting with downstream apis that only support POST and you want to present some kind of RESTful interface. \ No newline at end of file From ddf8b01d364dfd17f781c1ce74c586d4ddfa570c Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Sun, 9 Feb 2020 17:12:37 +0000 Subject: [PATCH 15/24] updated docs and readme with Method Transformation info (#1125) --- README.md | 2 +- docs/index.rst | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 57a93697..b59f84f3 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ A quick list of Ocelot's capabilities for more information see the [documentatio * Retry policies / QoS * Load Balancing * Logging / Tracing / Correlation -* Headers / Query String / Claims Transformation +* Headers / Method / Query String / Claims Transformation * Custom Middleware / Delegating Handlers * Configuration / Administration REST API * Platform / Cloud Agnostic diff --git a/docs/index.rst b/docs/index.rst index 4aba33fd..7952c91a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -23,7 +23,7 @@ Thanks for taking a look at the Ocelot documentation. Please use the left hand n features/requestaggregation features/graphql features/servicediscovery - features/servicefabric + features/servicefabric features/kubernetes features/authentication features/authorisation @@ -33,6 +33,7 @@ Thanks for taking a look at the Ocelot documentation. Please use the left hand n features/caching features/qualityofservice features/headerstransformation + features/methodtransformation features/claimstransformation features/logging features/tracing From fc3b6fdb8b7ee00e16e8f9e3d73992de98fab191 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Sun, 9 Feb 2020 18:15:24 +0000 Subject: [PATCH 16/24] fix docs formatting for http method transformation --- docs/features/methodtransformation.rst | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/features/methodtransformation.rst b/docs/features/methodtransformation.rst index b94ebce4..5b1c1518 100644 --- a/docs/features/methodtransformation.rst +++ b/docs/features/methodtransformation.rst @@ -7,21 +7,21 @@ This achieved by setting the following ReRoute configuration: .. code-block:: json -{ - "DownstreamPathTemplate": "/{url}", - "UpstreamPathTemplate": "/{url}", - "UpstreamHttpMethod": [ - "Get" - ], - "DownstreamHttpMethod": "POST", - "DownstreamScheme": "http", - "DownstreamHostAndPorts": [ - { - "Host": "localhost", - "Port": 53271 - } - ], -} + { + "DownstreamPathTemplate": "/{url}", + "UpstreamPathTemplate": "/{url}", + "UpstreamHttpMethod": [ + "Get" + ], + "DownstreamHttpMethod": "POST", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 53271 + } + ], + } The key property here is DownstreamHttpMethod which is set as POST and the ReRoute will only match on GET as set by UpstreamHttpMethod. From f2f583a88cdb281d4e14fcaf5247f857b4518b12 Mon Sep 17 00:00:00 2001 From: Andrea Tosato Date: Mon, 10 Feb 2020 18:01:54 +0100 Subject: [PATCH 17/24] Add DownstreamHttpVersion property for Http/2 WebServer --- .../Configuration/Builder/DownstreamReRouteBuilder.cs | 11 ++++++++++- src/Ocelot/Configuration/Creator/ReRoutesCreator.cs | 1 + src/Ocelot/Configuration/DownstreamReRoute.cs | 6 +++++- src/Ocelot/Configuration/File/FileReRoute.cs | 1 + .../Configuration/Validator/ReRouteFluentValidator.cs | 5 +++++ src/Ocelot/Request/Mapper/RequestMapper.cs | 8 +++++++- 6 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index ed978dde..c318b949 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -1,5 +1,6 @@ using Ocelot.Configuration.Creator; using Ocelot.Values; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -42,6 +43,7 @@ namespace Ocelot.Configuration.Builder private bool _dangerousAcceptAnyServerCertificateValidator; private SecurityOptions _securityOptions; private string _downstreamHttpMethod; + private string _downstreamHttpVersion; public DownstreamReRouteBuilder() { @@ -255,6 +257,12 @@ namespace Ocelot.Configuration.Builder return this; } + public DownstreamReRouteBuilder WithHttpVersion(string httpVersion) + { + _downstreamHttpVersion = httpVersion; + return this; + } + public DownstreamReRoute Build() { return new DownstreamReRoute( @@ -290,7 +298,8 @@ namespace Ocelot.Configuration.Builder _addHeadersToUpstream, _dangerousAcceptAnyServerCertificateValidator, _securityOptions, - _downstreamHttpMethod); + _downstreamHttpMethod, + _downstreamHttpVersion); } } } diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index 1cc463ef..63f42197 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -138,6 +138,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) + .WithHttpVersion(fileReRoute.DownstreamHttpVersion) .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); diff --git a/src/Ocelot/Configuration/DownstreamReRoute.cs b/src/Ocelot/Configuration/DownstreamReRoute.cs index 1c41f648..d8a3cb74 100644 --- a/src/Ocelot/Configuration/DownstreamReRoute.cs +++ b/src/Ocelot/Configuration/DownstreamReRoute.cs @@ -1,6 +1,7 @@ namespace Ocelot.Configuration { using Creator; + using System; using System.Collections.Generic; using Values; @@ -39,7 +40,8 @@ namespace Ocelot.Configuration List addHeadersToUpstream, bool dangerousAcceptAnyServerCertificateValidator, SecurityOptions securityOptions, - string downstreamHttpMethod) + string downstreamHttpMethod, + string downstreamHttpVersion) { DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator; AddHeadersToDownstream = addHeadersToDownstream; @@ -74,6 +76,7 @@ namespace Ocelot.Configuration AddHeadersToUpstream = addHeadersToUpstream; SecurityOptions = securityOptions; DownstreamHttpMethod = downstreamHttpMethod; + DownstreamHttpVersion = downstreamHttpVersion; } public string Key { get; } @@ -109,5 +112,6 @@ namespace Ocelot.Configuration public bool DangerousAcceptAnyServerCertificateValidator { get; } public SecurityOptions SecurityOptions { get; } public string DownstreamHttpMethod { get; } + public string DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/Configuration/File/FileReRoute.cs b/src/Ocelot/Configuration/File/FileReRoute.cs index 1bae31dd..b1a6cd4d 100644 --- a/src/Ocelot/Configuration/File/FileReRoute.cs +++ b/src/Ocelot/Configuration/File/FileReRoute.cs @@ -56,5 +56,6 @@ namespace Ocelot.Configuration.File public int Timeout { get; set; } public bool DangerousAcceptAnyServerCertificateValidator { get; set; } public FileSecurityOptions SecurityOptions { get; set; } + public string DownstreamHttpVersion { get; set; } } } diff --git a/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs b/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs index 746c430c..99a5a422 100644 --- a/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs +++ b/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs @@ -83,6 +83,11 @@ RuleForEach(reRoute => reRoute.DownstreamHostAndPorts) .SetValidator(hostAndPortValidator); }); + + When(reRoute => !string.IsNullOrEmpty(reRoute.DownstreamHttpVersion), () => + { + RuleFor(r => r.DownstreamHttpVersion).Matches("^[0-9]([.,][0-9]{1,1})?$"); + }); } private async Task IsSupportedAuthenticationProviders(FileAuthenticationOptions authenticationOptions, CancellationToken cancellationToken) diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index e050f1a6..f7ad7c38 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -20,11 +20,17 @@ { try { + if (!Version.TryParse(downstreamReRoute.DownstreamHttpVersion, out Version version)) + { + version = new Version(1, 1); + } + var requestMessage = new HttpRequestMessage() { Content = await MapContent(request), Method = MapMethod(request, downstreamReRoute), - RequestUri = MapUri(request) + RequestUri = MapUri(request), + Version = version, }; MapHeaders(request, requestMessage); From 42a1395a84eb5e029e7e9d7da3ccaae44ff6b528 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Mon, 17 Feb 2020 07:45:44 +0000 Subject: [PATCH 18/24] tests for downstream http version validation --- .../Validation/ReRouteFluentValidatorTests.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs b/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs index 9433bb89..82b3e646 100644 --- a/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs @@ -305,6 +305,71 @@ .BDDfy(); } + [Theory] + [InlineData("1.0")] + [InlineData("1.1")] + [InlineData("2.0")] + [InlineData("1,0")] + [InlineData("1,1")] + [InlineData("2,0")] + [InlineData("1")] + [InlineData("2")] + [InlineData("")] + [InlineData(null)] + public void should_be_valid_re_route_using_downstream_http_version(string version) + { + var fileReRoute = new FileReRoute + { + DownstreamPathTemplate = "/test", + UpstreamPathTemplate = "/test", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 5000, + }, + }, + DownstreamHttpVersion = version, + }; + + this.Given(_ => GivenThe(fileReRoute)) + .When(_ => WhenIValidate()) + .Then(_ => ThenTheResultIsValid()) + .BDDfy(); + } + + [Theory] + [InlineData("retg1.1")] + [InlineData("re2.0")] + [InlineData("1,0a")] + [InlineData("a1,1")] + [InlineData("12,0")] + [InlineData("asdf")] + public void should_be_invalid_re_route_using_downstream_http_version(string version) + { + var fileReRoute = new FileReRoute + { + DownstreamPathTemplate = "/test", + UpstreamPathTemplate = "/test", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 5000, + }, + }, + DownstreamHttpVersion = version, + }; + + this.Given(_ => GivenThe(fileReRoute)) + .When(_ => WhenIValidate()) + .Then(_ => ThenTheResultIsInvalid()) + .And(_ => ThenTheErrorsContains("'Downstream Http Version' is not in the correct format.")) + .BDDfy(); + } + private void GivenAnAuthProvider(string key) { var schemes = new List From b8922cef5f7fdffe5754e6aa40a2eba5f296a1ca Mon Sep 17 00:00:00 2001 From: TomPallister Date: Mon, 17 Feb 2020 08:03:11 +0000 Subject: [PATCH 19/24] tests for version creator --- .../Builder/DownstreamReRouteBuilder.cs | 6 ++-- .../Configuration/Creator/IVersionCreator.cs | 9 +++++ .../Configuration/Creator/ReRoutesCreator.cs | 9 +++-- .../Configuration/Creator/VersionCreator.cs | 17 ++++++++++ src/Ocelot/Configuration/DownstreamReRoute.cs | 4 +-- .../DependencyInjection/OcelotBuilder.cs | 1 + src/Ocelot/Request/Mapper/RequestMapper.cs | 7 +--- .../Configuration/ReRoutesCreatorTests.cs | 10 +++++- .../Configuration/VersionCreatorTests.cs | 34 +++++++++++++++++++ 9 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 src/Ocelot/Configuration/Creator/IVersionCreator.cs create mode 100644 src/Ocelot/Configuration/Creator/VersionCreator.cs create mode 100644 test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index c318b949..09f5c59a 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -43,7 +43,7 @@ namespace Ocelot.Configuration.Builder private bool _dangerousAcceptAnyServerCertificateValidator; private SecurityOptions _securityOptions; private string _downstreamHttpMethod; - private string _downstreamHttpVersion; + private Version _downstreamHttpVersion; public DownstreamReRouteBuilder() { @@ -257,9 +257,9 @@ namespace Ocelot.Configuration.Builder return this; } - public DownstreamReRouteBuilder WithHttpVersion(string httpVersion) + public DownstreamReRouteBuilder WithHttpVersion(Version downstreamHttpVersion) { - _downstreamHttpVersion = httpVersion; + _downstreamHttpVersion = downstreamHttpVersion; return this; } diff --git a/src/Ocelot/Configuration/Creator/IVersionCreator.cs b/src/Ocelot/Configuration/Creator/IVersionCreator.cs new file mode 100644 index 00000000..d810bde3 --- /dev/null +++ b/src/Ocelot/Configuration/Creator/IVersionCreator.cs @@ -0,0 +1,9 @@ +namespace Ocelot.Configuration.Creator +{ + using System; + + public interface IVersionCreator + { + Version Create(string downstreamHttpVersion); + } +} diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index 63f42197..ad896ac9 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -22,6 +22,7 @@ namespace Ocelot.Configuration.Creator private readonly IDownstreamAddressesCreator _downstreamAddressesCreator; private readonly IReRouteKeyCreator _reRouteKeyCreator; private readonly ISecurityOptionsCreator _securityOptionsCreator; + private readonly IVersionCreator _versionCreator; public ReRoutesCreator( IClaimsToThingCreator claimsToThingCreator, @@ -37,7 +38,8 @@ namespace Ocelot.Configuration.Creator IDownstreamAddressesCreator downstreamAddressesCreator, ILoadBalancerOptionsCreator loadBalancerOptionsCreator, IReRouteKeyCreator reRouteKeyCreator, - ISecurityOptionsCreator securityOptionsCreator + ISecurityOptionsCreator securityOptionsCreator, + IVersionCreator versionCreator ) { _reRouteKeyCreator = reRouteKeyCreator; @@ -55,6 +57,7 @@ namespace Ocelot.Configuration.Creator _httpHandlerOptionsCreator = httpHandlerOptionsCreator; _loadBalancerOptionsCreator = loadBalancerOptionsCreator; _securityOptionsCreator = securityOptionsCreator; + _versionCreator = versionCreator; } public List Create(FileConfiguration fileConfiguration) @@ -104,6 +107,8 @@ namespace Ocelot.Configuration.Creator var securityOptions = _securityOptionsCreator.Create(fileReRoute.SecurityOptions); + var downstreamHttpVersion = _versionCreator.Create(fileReRoute.DownstreamHttpVersion); + var reRoute = new DownstreamReRouteBuilder() .WithKey(fileReRoute.Key) .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate) @@ -138,7 +143,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) - .WithHttpVersion(fileReRoute.DownstreamHttpVersion) + .WithHttpVersion(downstreamHttpVersion) .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); diff --git a/src/Ocelot/Configuration/Creator/VersionCreator.cs b/src/Ocelot/Configuration/Creator/VersionCreator.cs new file mode 100644 index 00000000..b6fb3ad1 --- /dev/null +++ b/src/Ocelot/Configuration/Creator/VersionCreator.cs @@ -0,0 +1,17 @@ +namespace Ocelot.Configuration.Creator +{ + using System; + + public class VersionCreator : IVersionCreator + { + public Version Create(string downstreamHttpVersion) + { + if (!Version.TryParse(downstreamHttpVersion, out Version version)) + { + version = new Version(1, 1); + } + + return version; + } + } +} diff --git a/src/Ocelot/Configuration/DownstreamReRoute.cs b/src/Ocelot/Configuration/DownstreamReRoute.cs index d8a3cb74..be044539 100644 --- a/src/Ocelot/Configuration/DownstreamReRoute.cs +++ b/src/Ocelot/Configuration/DownstreamReRoute.cs @@ -41,7 +41,7 @@ namespace Ocelot.Configuration bool dangerousAcceptAnyServerCertificateValidator, SecurityOptions securityOptions, string downstreamHttpMethod, - string downstreamHttpVersion) + Version downstreamHttpVersion) { DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator; AddHeadersToDownstream = addHeadersToDownstream; @@ -112,6 +112,6 @@ namespace Ocelot.Configuration public bool DangerousAcceptAnyServerCertificateValidator { get; } public SecurityOptions SecurityOptions { get; } public string DownstreamHttpMethod { get; } - public string DownstreamHttpVersion { get; } + public Version DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 7f92d50d..2f1444f6 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -136,6 +136,7 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); + Services.TryAddSingleton(); //add security this.AddSecurity(); diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index f7ad7c38..7d383a70 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -20,17 +20,12 @@ { try { - if (!Version.TryParse(downstreamReRoute.DownstreamHttpVersion, out Version version)) - { - version = new Version(1, 1); - } - var requestMessage = new HttpRequestMessage() { Content = await MapContent(request), Method = MapMethod(request, downstreamReRoute), RequestUri = MapUri(request), - Version = version, + Version = downstreamReRoute.DownstreamHttpVersion, }; MapHeaders(request, requestMessage); diff --git a/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs index 32c235aa..47bc07c8 100644 --- a/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.Configuration { + using System; using Moq; using Ocelot.Cache; using Ocelot.Configuration; @@ -30,6 +31,7 @@ private Mock _lboCreator; private Mock _rrkCreator; private Mock _soCreator; + private Mock _versionCreator; private FileConfiguration _fileConfig; private ReRouteOptions _rro; private string _requestId; @@ -46,6 +48,7 @@ private LoadBalancerOptions _lbo; private List _result; private SecurityOptions _securityOptions; + private Version _expectedVersion; public ReRoutesCreatorTests() { @@ -63,6 +66,7 @@ _lboCreator = new Mock(); _rrkCreator = new Mock(); _soCreator = new Mock(); + _versionCreator = new Mock(); _creator = new ReRoutesCreator( _cthCreator.Object, @@ -78,7 +82,8 @@ _daCreator.Object, _lboCreator.Object, _rrkCreator.Object, - _soCreator.Object + _soCreator.Object, + _versionCreator.Object ); } @@ -155,6 +160,7 @@ private void GivenTheDependenciesAreSetUpCorrectly() { + _expectedVersion = new Version("1.1"); _rro = new ReRouteOptions(false, false, false, false, false); _requestId = "testy"; _rrk = "besty"; @@ -182,6 +188,7 @@ _hfarCreator.Setup(x => x.Create(It.IsAny())).Returns(_ht); _daCreator.Setup(x => x.Create(It.IsAny())).Returns(_dhp); _lboCreator.Setup(x => x.Create(It.IsAny())).Returns(_lbo); + _versionCreator.Setup(x => x.Create(It.IsAny())).Returns(_expectedVersion); } private void ThenTheReRoutesAreCreated() @@ -209,6 +216,7 @@ private void ThenTheReRouteIsSet(FileReRoute expected, int reRouteIndex) { + _result[reRouteIndex].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_expectedVersion); _result[reRouteIndex].DownstreamReRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated); _result[reRouteIndex].DownstreamReRoute[0].IsAuthorised.ShouldBe(_rro.IsAuthorised); _result[reRouteIndex].DownstreamReRoute[0].IsCached.ShouldBe(_rro.IsCached); diff --git a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs new file mode 100644 index 00000000..346f62e7 --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs @@ -0,0 +1,34 @@ +namespace Ocelot.UnitTests.Configuration +{ + using Ocelot.Configuration.Creator; + using Shouldly; + using Xunit; + + public class VersionCreatorTests + { + private readonly VersionCreator _creator; + + public VersionCreatorTests() + { + _creator = new VersionCreator(); + } + + [Fact] + public void should_create_version_based_on_input() + { + var input = "2.0"; + var result = _creator.Create(input); + result.Major.ShouldBe(2); + result.Minor.ShouldBe(0); + } + + [Fact] + public void should_default_to_version_one_point_one() + { + var input = ""; + var result = _creator.Create(input); + result.Major.ShouldBe(1); + result.Minor.ShouldBe(1); + } + } +} From e969e9ff0b999a700df9110b8a25d6b2f4ba1cf4 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Tue, 18 Feb 2020 20:45:28 +0000 Subject: [PATCH 20/24] unit tests passing --- .../Builder/DownstreamReRouteBuilder.cs | 2 +- .../Configuration/Creator/ReRoutesCreator.cs | 2 +- .../Configuration/VersionCreatorTests.cs | 36 ++++++++++++++----- .../Request/Mapper/RequestMapperTests.cs | 20 ++++++++++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index 09f5c59a..71049795 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -257,7 +257,7 @@ namespace Ocelot.Configuration.Builder return this; } - public DownstreamReRouteBuilder WithHttpVersion(Version downstreamHttpVersion) + public DownstreamReRouteBuilder WithDownstreamHttpVersion(Version downstreamHttpVersion) { _downstreamHttpVersion = downstreamHttpVersion; return this; diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index ad896ac9..e307b6d3 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -143,7 +143,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) - .WithHttpVersion(downstreamHttpVersion) + .WithDownstreamHttpVersion(downstreamHttpVersion) .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); diff --git a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs index 346f62e7..f5119ec8 100644 --- a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs @@ -1,12 +1,16 @@ namespace Ocelot.UnitTests.Configuration { + using System; using Ocelot.Configuration.Creator; using Shouldly; + using TestStack.BDDfy; using Xunit; public class VersionCreatorTests { private readonly VersionCreator _creator; + private string _input; + private Version _result; public VersionCreatorTests() { @@ -16,19 +20,35 @@ [Fact] public void should_create_version_based_on_input() { - var input = "2.0"; - var result = _creator.Create(input); - result.Major.ShouldBe(2); - result.Minor.ShouldBe(0); + this.Given(_ => GivenTheInput("2.0")) + .When(_ => WhenICreate()) + .Then(_ => ThenTheResultIs(2, 0)) + .BDDfy(); } [Fact] public void should_default_to_version_one_point_one() { - var input = ""; - var result = _creator.Create(input); - result.Major.ShouldBe(1); - result.Minor.ShouldBe(1); + this.Given(_ => GivenTheInput("")) + .When(_ => WhenICreate()) + .Then(_ => ThenTheResultIs(1, 1)) + .BDDfy(); + } + + private void GivenTheInput(string input) + { + _input = input; + } + + private void WhenICreate() + { + _result = _creator.Create(_input); + } + + private void ThenTheResultIs(int major, int minor) + { + _result.Major.ShouldBe(major); + _result.Minor.ShouldBe(minor); } } } diff --git a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs index 77091fc6..501e290c 100644 --- a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs +++ b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs @@ -51,6 +51,7 @@ .And(_ => GivenTheInputRequestHasHost(host)) .And(_ => GivenTheInputRequestHasPath(path)) .And(_ => GivenTheInputRequestHasQueryString(queryString)) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasUri(expectedUri)) @@ -80,6 +81,7 @@ { this.Given(_ => GivenTheInputRequestHasMethod(method)) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasMethod(method)) @@ -107,6 +109,7 @@ this.Given(_ => GivenTheInputRequestHasHeaders()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasEachHeader()) @@ -119,6 +122,7 @@ this.Given(_ => GivenTheInputRequestHasNoHeaders()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoHeaders()) @@ -131,6 +135,7 @@ this.Given(_ => GivenTheInputRequestHasContent("This is my content")) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasContent("This is my content")) @@ -143,6 +148,7 @@ this.Given(_ => GivenTheInputRequestHasNullContent()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoContent()) @@ -155,6 +161,7 @@ this.Given(_ => GivenTheInputRequestHasNoContentType()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoContent()) @@ -167,6 +174,7 @@ this.Given(_ => GivenTheInputRequestHasNoContentLength()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoContent()) @@ -192,6 +200,7 @@ .And(_ => GivenTheContentMD5Is(md5bytes)) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasContentTypeHeader("application/json")) @@ -213,6 +222,7 @@ .And(_ => GivenTheContentTypeIs("application/json")) .And(_ => GivenTheInputRequestHasMethod("POST")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasContentTypeHeader("application/json")) @@ -223,7 +233,15 @@ private void GivenTheDownstreamReRouteMethodIs(string input) { - _downstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build(); + _downstreamReRoute = new DownstreamReRouteBuilder() + .WithDownStreamHttpMethod(input) + .WithDownstreamHttpVersion(new Version("1.1")).Build(); + } + + private void GivenTheDownstreamReRoute() + { + _downstreamReRoute = new DownstreamReRouteBuilder() + .WithDownstreamHttpVersion(new Version("1.1")).Build(); } private void GivenTheInputRequestHasNoContentLength() From 2772ce406dcba54a16d2e1bfbe5a82663ae17096 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Wed, 19 Feb 2020 09:18:01 +0000 Subject: [PATCH 21/24] all acceptance tests passing --- .../Creator/ConfigurationCreator.cs | 10 +++++-- .../Configuration/Creator/DynamicsCreator.cs | 7 ++++- .../Configuration/File/FileDynamicReRoute.cs | 1 + .../File/FileGlobalConfiguration.cs | 2 ++ .../Configuration/IInternalConfiguration.cs | 4 +++ .../Configuration/InternalConfiguration.cs | 8 +++++- .../Finder/DownstreamRouteCreator.cs | 2 ++ .../LoadBalancerTests.cs | 2 +- test/Ocelot.AcceptanceTests/Steps.cs | 1 + ...wnstreamRouteFinderMiddlewareBenchmarks.cs | 2 +- .../ConfigurationCreatorTests.cs | 4 ++- .../Configuration/DynamicsCreatorTests.cs | 28 +++++++++++++++++-- .../FileConfigurationSetterTests.cs | 4 ++- .../FileInternalConfigurationCreatorTests.cs | 2 +- .../InMemoryConfigurationRepositoryTests.cs | 1 + .../DownstreamRouteCreatorTests.cs | 23 +++++++-------- .../DownstreamRouteFinderMiddlewareTests.cs | 3 +- .../DownstreamRouteFinderTests.cs | 4 ++- .../DownstreamRouteProviderFactoryTests.cs | 5 ++-- .../DownstreamUrlCreatorMiddlewareTests.cs | 2 +- .../Errors/ExceptionHandlerMiddlewareTests.cs | 8 +++--- ...ekaMiddlewareConfigurationProviderTests.cs | 4 +-- .../LoadBalancerMiddlewareTests.cs | 2 +- 23 files changed, 94 insertions(+), 35 deletions(-) diff --git a/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs index 1d5abc32..b3cced34 100644 --- a/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs @@ -13,13 +13,15 @@ namespace Ocelot.Configuration.Creator private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; private readonly IAdministrationPath _adminPath; private readonly ILoadBalancerOptionsCreator _loadBalancerOptionsCreator; + private readonly IVersionCreator _versionCreator; public ConfigurationCreator( IServiceProviderConfigurationCreator serviceProviderConfigCreator, IQoSOptionsCreator qosOptionsCreator, IHttpHandlerOptionsCreator httpHandlerOptionsCreator, IServiceProvider serviceProvider, - ILoadBalancerOptionsCreator loadBalancerOptionsCreator + ILoadBalancerOptionsCreator loadBalancerOptionsCreator, + IVersionCreator versionCreator ) { _adminPath = serviceProvider.GetService(); @@ -27,6 +29,7 @@ namespace Ocelot.Configuration.Creator _serviceProviderConfigCreator = serviceProviderConfigCreator; _qosOptionsCreator = qosOptionsCreator; _httpHandlerOptionsCreator = httpHandlerOptionsCreator; + _versionCreator = versionCreator; } public InternalConfiguration Create(FileConfiguration fileConfiguration, List reRoutes) @@ -41,6 +44,8 @@ namespace Ocelot.Configuration.Creator var adminPath = _adminPath != null ? _adminPath.Path : null; + var version = _versionCreator.Create(fileConfiguration.GlobalConfiguration.DownstreamHttpVersion); + return new InternalConfiguration(reRoutes, adminPath, serviceProviderConfiguration, @@ -48,7 +53,8 @@ namespace Ocelot.Configuration.Creator lbOptions, fileConfiguration.GlobalConfiguration.DownstreamScheme, qosOptions, - httpHandlerOptions + httpHandlerOptions, + version ); } } diff --git a/src/Ocelot/Configuration/Creator/DynamicsCreator.cs b/src/Ocelot/Configuration/Creator/DynamicsCreator.cs index 2b5193c0..57b86746 100644 --- a/src/Ocelot/Configuration/Creator/DynamicsCreator.cs +++ b/src/Ocelot/Configuration/Creator/DynamicsCreator.cs @@ -8,10 +8,12 @@ namespace Ocelot.Configuration.Creator public class DynamicsCreator : IDynamicsCreator { private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator; + private readonly IVersionCreator _versionCreator; - public DynamicsCreator(IRateLimitOptionsCreator rateLimitOptionsCreator) + public DynamicsCreator(IRateLimitOptionsCreator rateLimitOptionsCreator, IVersionCreator versionCreator) { _rateLimitOptionsCreator = rateLimitOptionsCreator; + _versionCreator = versionCreator; } public List Create(FileConfiguration fileConfiguration) @@ -26,10 +28,13 @@ namespace Ocelot.Configuration.Creator var rateLimitOption = _rateLimitOptionsCreator .Create(fileDynamicReRoute.RateLimitRule, globalConfiguration); + var version = _versionCreator.Create(fileDynamicReRoute.DownstreamHttpVersion); + var downstreamReRoute = new DownstreamReRouteBuilder() .WithEnableRateLimiting(rateLimitOption.EnableRateLimiting) .WithRateLimitOptions(rateLimitOption) .WithServiceName(fileDynamicReRoute.ServiceName) + .WithDownstreamHttpVersion(version) .Build(); var reRoute = new ReRouteBuilder() diff --git a/src/Ocelot/Configuration/File/FileDynamicReRoute.cs b/src/Ocelot/Configuration/File/FileDynamicReRoute.cs index 26d8b4d4..7657aa89 100644 --- a/src/Ocelot/Configuration/File/FileDynamicReRoute.cs +++ b/src/Ocelot/Configuration/File/FileDynamicReRoute.cs @@ -4,5 +4,6 @@ namespace Ocelot.Configuration.File { public string ServiceName { get; set; } public FileRateLimitRule RateLimitRule { get; set; } + public string DownstreamHttpVersion { get; set; } } } diff --git a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs index 9844c15d..6692ba04 100644 --- a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs +++ b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs @@ -26,5 +26,7 @@ public string DownstreamScheme { get; set; } public FileHttpHandlerOptions HttpHandlerOptions { get; set; } + + public string DownstreamHttpVersion { get; set; } } } diff --git a/src/Ocelot/Configuration/IInternalConfiguration.cs b/src/Ocelot/Configuration/IInternalConfiguration.cs index 1780da33..0925265f 100644 --- a/src/Ocelot/Configuration/IInternalConfiguration.cs +++ b/src/Ocelot/Configuration/IInternalConfiguration.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; namespace Ocelot.Configuration { + using System; + public interface IInternalConfiguration { List ReRoutes { get; } @@ -19,5 +21,7 @@ namespace Ocelot.Configuration QoSOptions QoSOptions { get; } HttpHandlerOptions HttpHandlerOptions { get; } + + Version DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/Configuration/InternalConfiguration.cs b/src/Ocelot/Configuration/InternalConfiguration.cs index 7b7649e4..7a398982 100644 --- a/src/Ocelot/Configuration/InternalConfiguration.cs +++ b/src/Ocelot/Configuration/InternalConfiguration.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; namespace Ocelot.Configuration { + using System; + public class InternalConfiguration : IInternalConfiguration { public InternalConfiguration( @@ -12,7 +14,8 @@ namespace Ocelot.Configuration LoadBalancerOptions loadBalancerOptions, string downstreamScheme, QoSOptions qoSOptions, - HttpHandlerOptions httpHandlerOptions) + HttpHandlerOptions httpHandlerOptions, + Version downstreamHttpVersion) { ReRoutes = reRoutes; AdministrationPath = administrationPath; @@ -22,6 +25,7 @@ namespace Ocelot.Configuration DownstreamScheme = downstreamScheme; QoSOptions = qoSOptions; HttpHandlerOptions = httpHandlerOptions; + DownstreamHttpVersion = downstreamHttpVersion; } public List ReRoutes { get; } @@ -32,5 +36,7 @@ namespace Ocelot.Configuration public string DownstreamScheme { get; } public QoSOptions QoSOptions { get; } public HttpHandlerOptions HttpHandlerOptions { get; } + + public Version DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs index ec6821ad..b2b1db95 100644 --- a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs +++ b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs @@ -1,5 +1,6 @@ namespace Ocelot.DownstreamRouteFinder.Finder { + using System; using Configuration; using Configuration.Builder; using Configuration.Creator; @@ -54,6 +55,7 @@ .WithQosOptions(qosOptions) .WithDownstreamScheme(configuration.DownstreamScheme) .WithLoadBalancerOptions(configuration.LoadBalancerOptions) + .WithDownstreamHttpVersion(configuration.DownstreamHttpVersion) .WithUpstreamPathTemplate(upstreamPathTemplate); var rateLimitOptions = configuration.ReRoutes != null diff --git a/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs b/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs index b3adf531..5ce9c1e2 100644 --- a/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs +++ b/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs @@ -27,7 +27,7 @@ namespace Ocelot.AcceptanceTests public void should_load_balance_request_with_least_connection() { int portOne = 50591; - int portTwo = 51482; + int portTwo = 54483; var downstreamServiceOneUrl = $"http://localhost:{portOne}"; var downstreamServiceTwoUrl = $"http://localhost:{portTwo}"; diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index e8aee690..976b7422 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -392,6 +392,7 @@ namespace Ocelot.AcceptanceTests _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); + Thread.Sleep(1000); } public void WhenIGetUrlOnTheApiGatewayWaitingForTheResponseToBeOk(string url) diff --git a/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs b/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs index 57f7386a..55869213 100644 --- a/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs +++ b/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs @@ -59,7 +59,7 @@ namespace Ocelot.Benchmarks _downstreamContext = new DownstreamContext(httpContext) { - Configuration = new InternalConfiguration(new List(), null, null, null, null, null, null, null) + Configuration = new InternalConfiguration(new List(), null, null, null, null, null, null, null, null) }; } diff --git a/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs index a51ec161..68516ad6 100644 --- a/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs @@ -19,6 +19,7 @@ namespace Ocelot.UnitTests.Configuration private readonly Mock _qosCreator; private readonly Mock _hhoCreator; private readonly Mock _lboCreator; + private readonly Mock _vCreator; private FileConfiguration _fileConfig; private List _reRoutes; private ServiceProviderConfiguration _spc; @@ -30,6 +31,7 @@ namespace Ocelot.UnitTests.Configuration public ConfigurationCreatorTests() { + _vCreator = new Mock(); _lboCreator = new Mock(); _hhoCreator = new Mock(); _qosCreator = new Mock(); @@ -117,7 +119,7 @@ namespace Ocelot.UnitTests.Configuration private void WhenICreate() { var serviceProvider = _serviceCollection.BuildServiceProvider(); - _creator = new ConfigurationCreator(_spcCreator.Object, _qosCreator.Object, _hhoCreator.Object, serviceProvider, _lboCreator.Object); + _creator = new ConfigurationCreator(_spcCreator.Object, _qosCreator.Object, _hhoCreator.Object, serviceProvider, _lboCreator.Object, _vCreator.Object); _result = _creator.Create(_fileConfig, _reRoutes); } } diff --git a/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs index 8a0601d3..b502aa73 100644 --- a/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.Configuration { + using System; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; @@ -14,15 +15,18 @@ { private readonly DynamicsCreator _creator; private readonly Mock _rloCreator; + private readonly Mock _versionCreator; private List _result; private FileConfiguration _fileConfig; private RateLimitOptions _rlo1; private RateLimitOptions _rlo2; + private Version _version; public DynamicsCreatorTests() { + _versionCreator = new Mock(); _rloCreator = new Mock(); - _creator = new DynamicsCreator(_rloCreator.Object); + _creator = new DynamicsCreator(_rloCreator.Object, _versionCreator.Object); } [Fact] @@ -50,7 +54,8 @@ RateLimitRule = new FileRateLimitRule { EnableRateLimiting = false - } + }, + DownstreamHttpVersion = "1.1" }, new FileDynamicReRoute { @@ -58,16 +63,19 @@ RateLimitRule = new FileRateLimitRule { EnableRateLimiting = true - } + }, + DownstreamHttpVersion = "2.0" } } }; this.Given(_ => GivenThe(fileConfig)) .And(_ => GivenTheRloCreatorReturns()) + .And(_ => GivenTheVersionCreatorReturns()) .When(_ => WhenICreate()) .Then(_ => ThenTheReRoutesAreReturned()) .And(_ => ThenTheRloCreatorIsCalledCorrectly()) + .And(_ => ThenTheVersionCreatorIsCalledCorrectly()) .BDDfy(); } @@ -80,18 +88,32 @@ _fileConfig.GlobalConfiguration), Times.Once); } + private void ThenTheVersionCreatorIsCalledCorrectly() + { + _versionCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[0].DownstreamHttpVersion), Times.Once); + _versionCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[1].DownstreamHttpVersion), Times.Once); + } + private void ThenTheReRoutesAreReturned() { _result.Count.ShouldBe(2); _result[0].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeFalse(); _result[0].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo1); + _result[0].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_version); _result[0].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[0].ServiceName); _result[1].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeTrue(); _result[1].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo2); + _result[1].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_version); _result[1].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[1].ServiceName); } + private void GivenTheVersionCreatorReturns() + { + _version = new Version("1.1"); + _versionCreator.Setup(x => x.Create(It.IsAny())).Returns(_version); + } + private void GivenTheRloCreatorReturns() { _rlo1 = new RateLimitOptionsBuilder().Build(); diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs index 85fdd53c..c647f410 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs @@ -15,6 +15,8 @@ using Xunit; namespace Ocelot.UnitTests.Configuration { + using System; + public class FileConfigurationSetterTests { private FileConfiguration _fileConfiguration; @@ -38,7 +40,7 @@ namespace Ocelot.UnitTests.Configuration { var fileConfig = new FileConfiguration(); var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build(); - var config = new InternalConfiguration(new List(), string.Empty, serviceProviderConfig, "asdf", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + var config = new InternalConfiguration(new List(), string.Empty, serviceProviderConfig, "asdf", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); this.Given(x => GivenTheFollowingConfiguration(fileConfig)) .And(x => GivenTheRepoReturns(new OkResponse())) diff --git a/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs index 6ccc4448..ccf3a677 100644 --- a/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs @@ -87,7 +87,7 @@ _reRoutes = new List { new ReRouteBuilder().Build() }; _aggregates = new List { new ReRouteBuilder().Build() }; _dynamics = new List { new ReRouteBuilder().Build() }; - _internalConfig = new InternalConfiguration(null, "", null, "", null, "", null, null); + _internalConfig = new InternalConfiguration(null, "", null, "", null, "", null, null, null); _reRoutesCreator.Setup(x => x.Create(It.IsAny())).Returns(_reRoutes); _aggregatesCreator.Setup(x => x.Create(It.IsAny(), It.IsAny>())).Returns(_aggregates); diff --git a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs index 508f25e0..4639837e 100644 --- a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs @@ -120,6 +120,7 @@ namespace Ocelot.UnitTests.Configuration public string DownstreamScheme { get; } public QoSOptions QoSOptions { get; } public HttpHandlerOptions HttpHandlerOptions { get; } + public Version DownstreamHttpVersion { get; } } } } diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs index a3348627..fd8190ea 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; @@ -44,7 +45,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_create_downstream_route() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) @@ -71,7 +72,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder var reRoutes = new List { reRoute }; - var configuration = new InternalConfiguration(reRoutes, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(reRoutes, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) @@ -83,7 +84,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_cache_downstream_route() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, "/geoffisthebest/")) .When(_ => WhenICreate()) @@ -96,7 +97,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_not_cache_downstream_route() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, "/geoffistheworst/")) .When(_ => WhenICreate()) @@ -110,7 +111,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_with_no_path() { var upstreamUrlPath = "/auth/"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -122,7 +123,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_with_only_first_segment_no_traling_slash() { var upstreamUrlPath = "/auth"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -134,7 +135,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_with_segments_no_traling_slash() { var upstreamUrlPath = "/auth/test"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -146,7 +147,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_and_remove_query_string() { var upstreamUrlPath = "/auth/test?test=1&best=2"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -158,7 +159,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_for_sticky_sessions() { var loadBalancerOptions = new LoadBalancerOptionsBuilder().WithType(nameof(CookieStickySessions)).WithKey("boom").WithExpiryInMs(1).Build(); - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) @@ -174,7 +175,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .WithTimeoutValue(1) .Build(); - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .And(_ => GivenTheQosCreatorReturns(qoSOptions)) @@ -186,7 +187,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_create_downstream_route_with_handler_options() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs index 3b333258..12e0a804 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; using Microsoft.AspNetCore.Http; using Moq; using Ocelot.Configuration; @@ -48,7 +49,7 @@ [Fact] public void should_call_scoped_data_repository_correctly() { - var config = new InternalConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + var config = new InternalConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); var downstreamReRoute = new DownstreamReRouteBuilder() .WithDownstreamPathTemplate("any old string") diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs index 7419fe55..5f9e1a50 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs @@ -13,6 +13,8 @@ using Xunit; namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; + public class DownstreamRouteFinderTests { private readonly IDownstreamRouteProvider _downstreamRouteFinder; @@ -739,7 +741,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private void GivenTheConfigurationIs(List reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig) { _reRoutesConfig = reRoutesConfig; - _config = new InternalConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + _config = new InternalConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); } private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath) diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs index 437033f7..ac8f5e0a 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Configuration; @@ -140,12 +141,12 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private void GivenTheReRoutes(List reRoutes) { - _config = new InternalConfiguration(reRoutes, "", null, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + _config = new InternalConfiguration(reRoutes, "", null, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); } private void GivenTheReRoutes(List reRoutes, ServiceProviderConfiguration config) { - _config = new InternalConfiguration(reRoutes, "", config, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + _config = new InternalConfiguration(reRoutes, "", config, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); } } } diff --git a/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs index 7ed6a02a..f6e0a2d5 100644 --- a/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs @@ -382,7 +382,7 @@ private void GivenTheServiceProviderConfigIs(ServiceProviderConfiguration config) { - var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null); + var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null, null); _downstreamContext.Configuration = configuration; } diff --git a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs index 3d86505e..932f40b2 100644 --- a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs @@ -52,7 +52,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void NoDownstreamException() { - var config = new InternalConfiguration(null, null, null, null, null, null, null, null); + var config = new InternalConfiguration(null, null, null, null, null, null, null, null, null); this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) @@ -65,7 +65,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void DownstreamException() { - var config = new InternalConfiguration(null, null, null, null, null, null, null, null); + var config = new InternalConfiguration(null, null, null, null, null, null, null, null, null); this.Given(_ => GivenAnExceptionWillBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) @@ -77,7 +77,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void ShouldSetRequestId() { - var config = new InternalConfiguration(null, null, null, "requestidkey", null, null, null, null); + var config = new InternalConfiguration(null, null, null, "requestidkey", null, null, null, null, null); this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) @@ -90,7 +90,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void ShouldSetAspDotNetRequestId() { - var config = new InternalConfiguration(null, null, null, null, null, null, null, null); + var config = new InternalConfiguration(null, null, null, null, null, null, null, null, null); this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) diff --git a/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs index fc7f3c82..5a6a5306 100644 --- a/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs +++ b/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs @@ -20,7 +20,7 @@ { var configRepo = new Mock(); configRepo.Setup(x => x.Get()) - .Returns(new OkResponse(new InternalConfiguration(null, null, null, null, null, null, null, null))); + .Returns(new OkResponse(new InternalConfiguration(null, null, null, null, null, null, null, null, null))); var services = new ServiceCollection(); services.AddSingleton(configRepo.Object); var sp = services.BuildServiceProvider(); @@ -35,7 +35,7 @@ var client = new Mock(); var configRepo = new Mock(); configRepo.Setup(x => x.Get()) - .Returns(new OkResponse(new InternalConfiguration(null, null, serviceProviderConfig, null, null, null, null, null))); + .Returns(new OkResponse(new InternalConfiguration(null, null, serviceProviderConfig, null, null, null, null, null, null))); var services = new ServiceCollection(); services.AddSingleton(configRepo.Object); services.AddSingleton(client.Object); diff --git a/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs b/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs index 2705b3b7..1a6202cf 100644 --- a/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs @@ -139,7 +139,7 @@ namespace Ocelot.UnitTests.LoadBalancer private void GivenTheConfigurationIs(ServiceProviderConfiguration config) { _config = config; - var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null); + var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null, null); _downstreamContext.Configuration = configuration; } From ec8a5b6ddf65572d93fdc0030c63fb51123e0732 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Fri, 21 Feb 2020 16:49:26 +0000 Subject: [PATCH 22/24] finished tests around http2 --- ...ersionCreator.cs => HttpVersionCreator.cs} | 2 +- .../DependencyInjection/OcelotBuilder.cs | 2 +- test/Ocelot.AcceptanceTests/HttpTests.cs | 205 ++++++++++++++++++ test/Ocelot.AcceptanceTests/ServiceHandler.cs | 27 +++ .../Configuration/VersionCreatorTests.cs | 4 +- 5 files changed, 236 insertions(+), 4 deletions(-) rename src/Ocelot/Configuration/Creator/{VersionCreator.cs => HttpVersionCreator.cs} (86%) create mode 100644 test/Ocelot.AcceptanceTests/HttpTests.cs diff --git a/src/Ocelot/Configuration/Creator/VersionCreator.cs b/src/Ocelot/Configuration/Creator/HttpVersionCreator.cs similarity index 86% rename from src/Ocelot/Configuration/Creator/VersionCreator.cs rename to src/Ocelot/Configuration/Creator/HttpVersionCreator.cs index b6fb3ad1..f80f98ff 100644 --- a/src/Ocelot/Configuration/Creator/VersionCreator.cs +++ b/src/Ocelot/Configuration/Creator/HttpVersionCreator.cs @@ -2,7 +2,7 @@ { using System; - public class VersionCreator : IVersionCreator + public class HttpVersionCreator : IVersionCreator { public Version Create(string downstreamHttpVersion) { diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 2f1444f6..0336f691 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -136,7 +136,7 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); - Services.TryAddSingleton(); + Services.TryAddSingleton(); //add security this.AddSecurity(); diff --git a/test/Ocelot.AcceptanceTests/HttpTests.cs b/test/Ocelot.AcceptanceTests/HttpTests.cs new file mode 100644 index 00000000..89ab12f4 --- /dev/null +++ b/test/Ocelot.AcceptanceTests/HttpTests.cs @@ -0,0 +1,205 @@ +namespace Ocelot.AcceptanceTests +{ + using Microsoft.AspNetCore.Http; + using Ocelot.Configuration.File; + using System; + using System.Collections.Generic; + using System.IO; + using System.Net; + using System.Net.Http; + using Microsoft.AspNetCore.Server.Kestrel.Core; + using TestStack.BDDfy; + using Xunit; + + public class HttpTests : IDisposable + { + private readonly Steps _steps; + private readonly ServiceHandler _serviceHandler; + + public HttpTests() + { + _serviceHandler = new ServiceHandler(); + _steps = new Steps(); + } + + [Fact] + public void should_return_response_200_when_using_http_one_point_one() + { + const int port = 53279; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "1.1", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_when_using_http_two_point_zero() + { + const int port = 53675; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "2.0", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http2)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) + .BDDfy(); + } + + [Fact] + public void should_return_response_500_when_using_http_one_to_talk_to_server_running_http_two() + { + const int port = 53677; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "1.1", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http2)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_when_using_http_two_to_talk_to_server_running_http_one_point_one() + { + const int port = 53679; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "2.0", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) + .BDDfy(); + } + + private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int port, HttpProtocols protocols) + { + _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context => + { + context.Response.StatusCode = 200; + var reader = new StreamReader(context.Request.Body); + var body = await reader.ReadToEndAsync(); + await context.Response.WriteAsync(body); + }, port, protocols); + } + + public void Dispose() + { + _serviceHandler.Dispose(); + _steps.Dispose(); + } + } +} diff --git a/test/Ocelot.AcceptanceTests/ServiceHandler.cs b/test/Ocelot.AcceptanceTests/ServiceHandler.cs index c10c122b..9fc8b0f5 100644 --- a/test/Ocelot.AcceptanceTests/ServiceHandler.cs +++ b/test/Ocelot.AcceptanceTests/ServiceHandler.cs @@ -6,9 +6,11 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; + using System.ComponentModel; using System.IO; using System.Net; using System.Threading.Tasks; + using Microsoft.AspNetCore.Server.Kestrel.Core; public class ServiceHandler : IDisposable { @@ -47,6 +49,31 @@ _builder.Start(); } + public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, RequestDelegate del, int port, HttpProtocols protocols) + { + _builder = new WebHostBuilder() + .UseUrls(baseUrl) + .UseKestrel() + .ConfigureKestrel(serverOptions => + { + serverOptions.Listen(IPAddress.Loopback, port, listenOptions => + { + listenOptions.UseHttps("idsrv3test.pfx", "idsrv3test"); + listenOptions.Protocols = protocols; + }); + }) + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .Configure(app => + { + app.UsePathBase(basePath); + app.Run(del); + }) + .Build(); + + _builder.Start(); + } + public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string fileName, string password, int port, RequestDelegate del) { _builder = new WebHostBuilder() diff --git a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs index f5119ec8..bb440c5c 100644 --- a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs @@ -8,13 +8,13 @@ public class VersionCreatorTests { - private readonly VersionCreator _creator; + private readonly HttpVersionCreator _creator; private string _input; private Version _result; public VersionCreatorTests() { - _creator = new VersionCreator(); + _creator = new HttpVersionCreator(); } [Fact] From ede5650a3cf01fc37cf39ea5bd66220011ef89af Mon Sep 17 00:00:00 2001 From: TomPallister Date: Fri, 21 Feb 2020 16:53:03 +0000 Subject: [PATCH 23/24] docs --- docs/features/configuration.rst | 8 ++++- test/Ocelot.AcceptanceTests/HttpTests.cs | 38 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/features/configuration.rst b/docs/features/configuration.rst index f9cb7841..ebd82203 100644 --- a/docs/features/configuration.rst +++ b/docs/features/configuration.rst @@ -25,6 +25,7 @@ Here is an example ReRoute configuration, You don't need to set all of these thi "Get" ], "DownstreamHttpMethod": "", + "DownstreamHttpVersion": "", "AddHeadersToRequest": {}, "AddClaimsToRequest": {}, "RouteClaimsRequirement": {}, @@ -278,4 +279,9 @@ Registering a callback { _callbackHolder.Dispose(); } - } \ No newline at end of file + } + +DownstreamHttpVersion +--------------------- + +Ocelot allows you to choose the HTTP version it will use to make the proxy request. It can be set as "1.0", "1.1" or "2.0". \ No newline at end of file diff --git a/test/Ocelot.AcceptanceTests/HttpTests.cs b/test/Ocelot.AcceptanceTests/HttpTests.cs index 89ab12f4..b63ad4bb 100644 --- a/test/Ocelot.AcceptanceTests/HttpTests.cs +++ b/test/Ocelot.AcceptanceTests/HttpTests.cs @@ -22,6 +22,44 @@ namespace Ocelot.AcceptanceTests _steps = new Steps(); } + [Fact] + public void should_return_response_200_when_using_http_one() + { + const int port = 53219; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "1.0", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + [Fact] public void should_return_response_200_when_using_http_one_point_one() { From 08512ec1c014c9bae9d54db2b7159173dbcb0b44 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Sat, 22 Feb 2020 12:30:38 +0000 Subject: [PATCH 24/24] check we are on master before pushing coverage report (#1143) This means that forked PRs can now build as they don't need the coveralls secret --- build.cake | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/build.cake b/build.cake index 3d4626bd..9b909f63 100644 --- a/build.cake +++ b/build.cake @@ -134,11 +134,12 @@ Task("RunUnitTests") var coverageSummaryFile = GetSubDirectories(artifactsForUnitTestsDir).First().CombineWithFilePath(File("coverage.opencover.xml")); Information(coverageSummaryFile); Information(artifactsForUnitTestsDir); + // todo bring back report generator to get a friendly report // ReportGenerator(coverageSummaryFile, artifactsForUnitTestsDir); // https://github.com/danielpalme/ReportGenerator - - if (IsRunningOnCircleCI()) + + if (IsRunningOnCircleCI() && IsMaster()) { var repoToken = EnvironmentVariable(coverallsRepoToken); if (string.IsNullOrEmpty(repoToken)) @@ -497,4 +498,9 @@ private string GetResource(string url) private bool IsRunningOnCircleCI() { return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("CIRCLECI")); +} + +private bool IsMaster() +{ + return Environment.GetEnvironmentVariable("CIRCLE_BRANCH").ToLower() == "master"; } \ No newline at end of file