From a8b56a63c24ea8b6dc5db522e40aaf6a8abf2312 Mon Sep 17 00:00:00 2001 From: Oleksandr Yershov Date: Thu, 28 Sep 2017 09:41:13 +0300 Subject: [PATCH] Cookie proxying and auto redirect configuration #128 HttpHandlerOptions are added to ReRoute configuration and passed down to HttpClientHttpRequester as Request properties. --- .../Configuration/Builder/ReRouteBuilder.cs | 11 ++- .../Creator/FileOcelotConfigurationCreator.cs | 8 ++- .../Creator/HttpHandlerOptionsCreator.cs | 13 ++++ .../Creator/IHttpHandlerOptionsCreator.cs | 12 ++++ .../File/FileHttpHandlerOptions.cs | 15 ++++ src/Ocelot/Configuration/File/FileReRoute.cs | 2 + .../Configuration/HttpHandlerOptions.cs | 25 +++++++ src/Ocelot/Configuration/ReRoute.cs | 5 +- .../ServiceCollectionExtensions.cs | 1 + .../Request/Builder/HttpRequestCreator.cs | 6 +- src/Ocelot/Request/Builder/IRequestCreator.cs | 4 +- .../HttpRequestBuilderMiddleware.cs | 4 +- src/Ocelot/Request/Request.cs | 8 ++- src/Ocelot/Requester/HttpClientBuilder.cs | 4 +- .../Requester/HttpClientHttpRequester.cs | 12 ++-- src/Ocelot/Requester/IHttpClientBuilder.cs | 12 ++-- .../FileConfigurationCreatorTests.cs | 43 ++++++++++- .../HttpHandlerOptionsCreatorTests.cs | 71 +++++++++++++++++++ .../HttpRequestBuilderMiddlewareTests.cs | 14 ++-- .../Request/HttpRequestCreatorTests.cs | 25 ++++++- .../Requester/HttpRequesterMiddlewareTests.cs | 2 +- 21 files changed, 269 insertions(+), 28 deletions(-) create mode 100644 src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs create mode 100644 src/Ocelot/Configuration/Creator/IHttpHandlerOptionsCreator.cs create mode 100644 src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs create mode 100644 src/Ocelot/Configuration/HttpHandlerOptions.cs create mode 100644 test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs diff --git a/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs index 852c4870c..ad84a3e63 100644 --- a/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/ReRouteBuilder.cs @@ -28,7 +28,8 @@ public class ReRouteBuilder private string _loadBalancer; private ServiceProviderConfiguration _serviceProviderConfiguraion; private bool _useQos; - private QoSOptions _qosOptions; + private QoSOptions _qosOptions; + private HttpHandlerOptions _httpHandlerOptions; public bool _enableRateLimiting; public RateLimitOptions _rateLimitOptions; @@ -176,6 +177,11 @@ public ReRouteBuilder WithRateLimitOptions(RateLimitOptions input) return this; } + public ReRouteBuilder WithHttpHandlerOptions(HttpHandlerOptions input) + { + _httpHandlerOptions = input; + return this; + } public ReRoute Build() { @@ -203,7 +209,8 @@ public ReRoute Build() _useQos, _qosOptions, _enableRateLimiting, - _rateLimitOptions); + _rateLimitOptions, + _httpHandlerOptions); } } } diff --git a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs index c6f5f4fe9..b8a2df079 100644 --- a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs @@ -38,6 +38,7 @@ public class FileOcelotConfigurationCreator : IOcelotConfigurationCreator private readonly IReRouteOptionsCreator _fileReRouteOptionsCreator; private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator; private readonly IRegionCreator _regionCreator; + private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; public FileOcelotConfigurationCreator( IOptions options, @@ -55,7 +56,8 @@ public FileOcelotConfigurationCreator( IQoSOptionsCreator qosOptionsCreator, IReRouteOptionsCreator fileReRouteOptionsCreator, IRateLimitOptionsCreator rateLimitOptionsCreator, - IRegionCreator regionCreator + IRegionCreator regionCreator, + IHttpHandlerOptionsCreator httpHandlerOptionsCreator ) { _regionCreator = regionCreator; @@ -74,6 +76,7 @@ IRegionCreator regionCreator _serviceProviderConfigCreator = serviceProviderConfigCreator; _qosOptionsCreator = qosOptionsCreator; _fileReRouteOptionsCreator = fileReRouteOptionsCreator; + _httpHandlerOptionsCreator = httpHandlerOptionsCreator; } public async Task> Create() @@ -143,6 +146,8 @@ private async Task SetUpReRoute(FileReRoute fileReRoute, FileGlobalConf var region = _regionCreator.Create(fileReRoute); + var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileReRoute); + var reRoute = new ReRouteBuilder() .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate) .WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate) @@ -168,6 +173,7 @@ private async Task SetUpReRoute(FileReRoute fileReRoute, FileGlobalConf .WithQosOptions(qosOptions) .WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting) .WithRateLimitOptions(rateLimitOption) + .WithHttpHandlerOptions(httpHandlerOptions) .Build(); await SetupLoadBalancer(reRoute); diff --git a/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs b/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs new file mode 100644 index 000000000..52aa76948 --- /dev/null +++ b/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs @@ -0,0 +1,13 @@ +using Ocelot.Configuration.File; + +namespace Ocelot.Configuration.Creator +{ + public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator + { + public HttpHandlerOptions Create(FileReRoute fileReRoute) + { + return new HttpHandlerOptions(fileReRoute.HttpHandlerOptions.AllowAutoRedirect, + fileReRoute.HttpHandlerOptions.UseCookieContainer); + } + } +} diff --git a/src/Ocelot/Configuration/Creator/IHttpHandlerOptionsCreator.cs b/src/Ocelot/Configuration/Creator/IHttpHandlerOptionsCreator.cs new file mode 100644 index 000000000..34e5ffd1a --- /dev/null +++ b/src/Ocelot/Configuration/Creator/IHttpHandlerOptionsCreator.cs @@ -0,0 +1,12 @@ +using Ocelot.Configuration.File; + +namespace Ocelot.Configuration.Creator +{ + /// + /// Describes creation of HttpHandlerOptions + /// + public interface IHttpHandlerOptionsCreator + { + HttpHandlerOptions Create(FileReRoute fileReRoute); + } +} diff --git a/src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs b/src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs new file mode 100644 index 000000000..ea3527672 --- /dev/null +++ b/src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs @@ -0,0 +1,15 @@ +namespace Ocelot.Configuration.File +{ + public class FileHttpHandlerOptions + { + public FileHttpHandlerOptions() + { + AllowAutoRedirect = true; + UseCookieContainer = true; + } + + public bool AllowAutoRedirect { get; set; } + + public bool UseCookieContainer { get; set; } + } +} diff --git a/src/Ocelot/Configuration/File/FileReRoute.cs b/src/Ocelot/Configuration/File/FileReRoute.cs index 59483aee2..1505e50a8 100644 --- a/src/Ocelot/Configuration/File/FileReRoute.cs +++ b/src/Ocelot/Configuration/File/FileReRoute.cs @@ -15,6 +15,7 @@ public FileReRoute() FileCacheOptions = new FileCacheOptions(); QoSOptions = new FileQoSOptions(); RateLimitOptions = new FileRateLimitRule(); + HttpHandlerOptions = new FileHttpHandlerOptions(); } public string DownstreamPathTemplate { get; set; } @@ -35,5 +36,6 @@ public FileReRoute() public FileQoSOptions QoSOptions { get; set; } public string LoadBalancer {get;set;} public FileRateLimitRule RateLimitOptions { get; set; } + public FileHttpHandlerOptions HttpHandlerOptions { get; set; } } } \ No newline at end of file diff --git a/src/Ocelot/Configuration/HttpHandlerOptions.cs b/src/Ocelot/Configuration/HttpHandlerOptions.cs new file mode 100644 index 000000000..0ec72d1dd --- /dev/null +++ b/src/Ocelot/Configuration/HttpHandlerOptions.cs @@ -0,0 +1,25 @@ +namespace Ocelot.Configuration +{ + /// + /// Describes configuration parameters for http handler, + /// that is created to handle a request to service + /// + public class HttpHandlerOptions + { + public HttpHandlerOptions(bool allowAutoRedirect, bool useCookieContainer) + { + AllowAutoRedirect = allowAutoRedirect; + UseCookieContainer = useCookieContainer; + } + + /// + /// Specify if auto redirect is enabled + /// + public bool AllowAutoRedirect { get; private set; } + + /// + /// Specify is handler has to use a cookie container + /// + public bool UseCookieContainer { get; private set; } + } +} diff --git a/src/Ocelot/Configuration/ReRoute.cs b/src/Ocelot/Configuration/ReRoute.cs index 2b8734c42..cfc8b9bad 100644 --- a/src/Ocelot/Configuration/ReRoute.cs +++ b/src/Ocelot/Configuration/ReRoute.cs @@ -29,7 +29,8 @@ public ReRoute(PathTemplate downstreamPathTemplate, bool isQos, QoSOptions qosOptions, bool enableEndpointRateLimiting, - RateLimitOptions ratelimitOptions) + RateLimitOptions ratelimitOptions, + HttpHandlerOptions httpHandlerOptions) { ReRouteKey = reRouteKey; ServiceProviderConfiguraion = serviceProviderConfiguraion; @@ -58,6 +59,7 @@ public ReRoute(PathTemplate downstreamPathTemplate, QosOptionsOptions = qosOptions; EnableEndpointEndpointRateLimiting = enableEndpointRateLimiting; RateLimitOptions = ratelimitOptions; + HttpHandlerOptions = httpHandlerOptions; } public string ReRouteKey {get;private set;} @@ -84,5 +86,6 @@ public ReRoute(PathTemplate downstreamPathTemplate, public ServiceProviderConfiguration ServiceProviderConfiguraion { get; private set; } public bool EnableEndpointEndpointRateLimiting { get; private set; } public RateLimitOptions RateLimitOptions { get; private set; } + public HttpHandlerOptions HttpHandlerOptions { get; private set; } } } \ No newline at end of file diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index d55a8573c..08314b706 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -84,6 +84,7 @@ public static IServiceCollection AddOcelot(this IServiceCollection services, ICo services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(); var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(); diff --git a/src/Ocelot/Request/Builder/HttpRequestCreator.cs b/src/Ocelot/Request/Builder/HttpRequestCreator.cs index f95db4b9f..8c3c9218b 100644 --- a/src/Ocelot/Request/Builder/HttpRequestCreator.cs +++ b/src/Ocelot/Request/Builder/HttpRequestCreator.cs @@ -10,9 +10,11 @@ public sealed class HttpRequestCreator : IRequestCreator public async Task> Build( HttpRequestMessage httpRequestMessage, bool isQos, - IQoSProvider qosProvider) + IQoSProvider qosProvider, + bool useCookieContainer, + bool allowAutoRedirect) { - return new OkResponse(new Request(httpRequestMessage, isQos, qosProvider)); + return new OkResponse(new Request(httpRequestMessage, isQos, qosProvider, useCookieContainer, allowAutoRedirect)); } } } \ No newline at end of file diff --git a/src/Ocelot/Request/Builder/IRequestCreator.cs b/src/Ocelot/Request/Builder/IRequestCreator.cs index 85fbfa8dc..d290db4fe 100644 --- a/src/Ocelot/Request/Builder/IRequestCreator.cs +++ b/src/Ocelot/Request/Builder/IRequestCreator.cs @@ -11,6 +11,8 @@ public interface IRequestCreator Task> Build( HttpRequestMessage httpRequestMessage, bool isQos, - IQoSProvider qosProvider); + IQoSProvider qosProvider, + bool useCookieContainer, + bool allowAutoRedirect); } } diff --git a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddleware.cs b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddleware.cs index c59af7a60..8e05f2ea2 100644 --- a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddleware.cs +++ b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddleware.cs @@ -46,7 +46,9 @@ public async Task Invoke(HttpContext context) var buildResult = await _requestCreator.Build( DownstreamRequest, DownstreamRoute.ReRoute.IsQos, - qosProvider.Data); + qosProvider.Data, + DownstreamRoute.ReRoute.HttpHandlerOptions.UseCookieContainer, + DownstreamRoute.ReRoute.HttpHandlerOptions.AllowAutoRedirect); if (buildResult.IsError) { diff --git a/src/Ocelot/Request/Request.cs b/src/Ocelot/Request/Request.cs index 3f1c654c1..9f0b66e21 100644 --- a/src/Ocelot/Request/Request.cs +++ b/src/Ocelot/Request/Request.cs @@ -8,15 +8,21 @@ public class Request public Request( HttpRequestMessage httpRequestMessage, bool isQos, - IQoSProvider qosProvider) + IQoSProvider qosProvider, + bool allowAutoRedirect, + bool useCookieContainer) { HttpRequestMessage = httpRequestMessage; IsQos = isQos; QosProvider = qosProvider; + AllowAutoRedirect = allowAutoRedirect; + UseCookieContainer = useCookieContainer; } public HttpRequestMessage HttpRequestMessage { get; private set; } public bool IsQos { get; private set; } public IQoSProvider QosProvider { get; private set; } + public bool AllowAutoRedirect { get; private set; } + public bool UseCookieContainer { get; private set; } } } diff --git a/src/Ocelot/Requester/HttpClientBuilder.cs b/src/Ocelot/Requester/HttpClientBuilder.cs index 141eab8b3..83f475c2b 100644 --- a/src/Ocelot/Requester/HttpClientBuilder.cs +++ b/src/Ocelot/Requester/HttpClientBuilder.cs @@ -20,9 +20,9 @@ public IHttpClientBuilder WithQos(IQoSProvider qosProvider, IOcelotLogger logge return this; } - public IHttpClient Create() + public IHttpClient Create(bool useCookies, bool allowAutoRedirect) { - var httpclientHandler = new HttpClientHandler(); + var httpclientHandler = new HttpClientHandler { AllowAutoRedirect = allowAutoRedirect, UseCookies = useCookies}; var client = new HttpClient(CreateHttpMessageHandler(httpclientHandler)); diff --git a/src/Ocelot/Requester/HttpClientHttpRequester.cs b/src/Ocelot/Requester/HttpClientHttpRequester.cs index 3f85af0c8..a6a289151 100644 --- a/src/Ocelot/Requester/HttpClientHttpRequester.cs +++ b/src/Ocelot/Requester/HttpClientHttpRequester.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Net.Http; using System.Threading.Tasks; +using Ocelot.Configuration; using Ocelot.Logging; using Ocelot.Responses; using Polly.CircuitBreaker; @@ -14,7 +15,8 @@ public class HttpClientHttpRequester : IHttpRequester private readonly IHttpClientCache _cacheHandlers; private readonly IOcelotLogger _logger; - public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory, IHttpClientCache cacheHandlers) + public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory, + IHttpClientCache cacheHandlers) { _logger = loggerFactory.CreateLogger(); _cacheHandlers = cacheHandlers; @@ -25,8 +27,8 @@ public async Task> GetResponse(Request.Request req var builder = new HttpClientBuilder(); var cacheKey = GetCacheKey(request, builder); - - var httpClient = GetHttpClient(cacheKey, builder); + + var httpClient = GetHttpClient(cacheKey, builder, request.UseCookieContainer, request.AllowAutoRedirect); try { @@ -54,13 +56,13 @@ public async Task> GetResponse(Request.Request req } - private IHttpClient GetHttpClient(string cacheKey, IHttpClientBuilder builder) + private IHttpClient GetHttpClient(string cacheKey, IHttpClientBuilder builder, bool useCookieContainer, bool allowAutoRedirect) { var httpClient = _cacheHandlers.Get(cacheKey); if (httpClient == null) { - httpClient = builder.Create(); + httpClient = builder.Create(useCookieContainer, allowAutoRedirect); } return httpClient; } diff --git a/src/Ocelot/Requester/IHttpClientBuilder.cs b/src/Ocelot/Requester/IHttpClientBuilder.cs index 832fd8d1e..6de5f87a8 100644 --- a/src/Ocelot/Requester/IHttpClientBuilder.cs +++ b/src/Ocelot/Requester/IHttpClientBuilder.cs @@ -15,11 +15,13 @@ public interface IHttpClientBuilder /// /// Sets a PollyCircuitBreakingDelegatingHandler . /// - IHttpClientBuilder WithQos(IQoSProvider qosProvider, IOcelotLogger logger); - - /// + IHttpClientBuilder WithQos(IQoSProvider qosProvider, IOcelotLogger logger); + + /// /// Creates the - /// - IHttpClient Create(); + /// + /// Defines if http client should use cookie container + /// Defines if http client should allow auto redirect + IHttpClient Create(bool useCookies, bool allowAutoRedirect); } } diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs index 4cb53118b..252e6f11a 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs @@ -45,6 +45,7 @@ public class FileConfigurationCreatorTests private Mock _fileReRouteOptionsCreator; private Mock _rateLimitOptions; private Mock _regionCreator; + private Mock _httpHandlerOptionsCreator; public FileConfigurationCreatorTests() { @@ -66,6 +67,7 @@ public FileConfigurationCreatorTests() _fileReRouteOptionsCreator = new Mock(); _rateLimitOptions = new Mock(); _regionCreator = new Mock(); + _httpHandlerOptionsCreator = new Mock(); _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( _fileConfig.Object, _validator.Object, _logger.Object, @@ -73,7 +75,7 @@ public FileConfigurationCreatorTests() _qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object, _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object, _serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object, - _rateLimitOptions.Object, _regionCreator.Object); + _rateLimitOptions.Object, _regionCreator.Object, _httpHandlerOptionsCreator.Object); } [Fact] @@ -444,6 +446,45 @@ public void should_call_request_id_creator() .BDDfy(); } + [Fact] + public void should_call_httpHandler_creator() + { + var reRouteOptions = new ReRouteOptionsBuilder() + .Build(); + var httpHandlerOptions = new HttpHandlerOptions(true, true); + + this.Given(x => x.GivenTheConfigIs(new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamHost = "127.0.0.1", + UpstreamPathTemplate = "/api/products/{productId}", + DownstreamPathTemplate = "/products/{productId}", + UpstreamHttpMethod = new List { "Get" } + } + }, + })) + .And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions)) + .And(x => x.GivenTheConfigIsValid()) + .And(x => x.GivenTheFollowingHttpHandlerOptionsAreReturned(httpHandlerOptions)) + .When(x => x.WhenICreateTheConfig()) + .Then(x => x.ThenTheHttpHandlerOptionsCreatorIsCalledCorrectly()) + .BDDfy(); + } + + private void GivenTheFollowingHttpHandlerOptionsAreReturned(HttpHandlerOptions httpHandlerOptions) + { + _httpHandlerOptionsCreator.Setup(x => x.Create(It.IsAny())) + .Returns(httpHandlerOptions); + } + + private void ThenTheHttpHandlerOptionsCreatorIsCalledCorrectly() + { + _httpHandlerOptionsCreator.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once()); + } + [Theory] [MemberData(nameof(AuthenticationConfigTestData.GetAuthenticationData), MemberType = typeof(AuthenticationConfigTestData))] public void should_create_with_headers_to_extract(string provider, IAuthenticationConfig config, FileConfiguration fileConfig) diff --git a/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs new file mode 100644 index 000000000..69f414e52 --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs @@ -0,0 +1,71 @@ +using Ocelot.Configuration; +using Ocelot.Configuration.Creator; +using Ocelot.Configuration.File; +using Shouldly; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Configuration +{ + + public class HttpHandlerOptionsCreatorTests + { + private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; + private FileReRoute _fileReRoute; + private HttpHandlerOptions _httpHandlerOptions; + + public HttpHandlerOptionsCreatorTests() + { + _httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(); + } + + [Fact] + public void should_create_options_with_useCookie_and_allowAutoRedirect_true_as_default() + { + var fileReRoute = new FileReRoute(); + var expectedOptions = new HttpHandlerOptions(true, true); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void should_create_options_with_specified_useCookie_and_allowAutoRedirect() + { + var fileReRoute = new FileReRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions + { + AllowAutoRedirect = false, + UseCookieContainer = false + } + }; + + var expectedOptions = new HttpHandlerOptions(false, false); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + private void GivenTheFollowing(FileReRoute fileReRoute) + { + _fileReRoute = fileReRoute; + } + + private void WhenICreateHttpHandlerOptions() + { + _httpHandlerOptions = _httpHandlerOptionsCreator.Create(_fileReRoute); + } + + private void ThenTheFollowingOptionsReturned(HttpHandlerOptions options) + { + _httpHandlerOptions.ShouldNotBeNull(); + _httpHandlerOptions.AllowAutoRedirect.ShouldBe(options.AllowAutoRedirect); + _httpHandlerOptions.UseCookieContainer.ShouldBe(options.UseCookieContainer); + } + } +} diff --git a/test/Ocelot.UnitTests/Request/HttpRequestBuilderMiddlewareTests.cs b/test/Ocelot.UnitTests/Request/HttpRequestBuilderMiddlewareTests.cs index 02f2b2084..807c35a56 100644 --- a/test/Ocelot.UnitTests/Request/HttpRequestBuilderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Request/HttpRequestBuilderMiddlewareTests.cs @@ -15,8 +15,9 @@ using TestStack.BDDfy; using Xunit; using Ocelot.Requester.QoS; - using Microsoft.AspNetCore.Builder; - + using Ocelot.Configuration; + using Microsoft.AspNetCore.Builder; + public class HttpRequestBuilderMiddlewareTests : ServerHostedMiddlewareTest { private readonly Mock _requestBuilder; @@ -50,12 +51,13 @@ public void should_call_scoped_data_repository_correctly() new ReRouteBuilder() .WithRequestIdKey("LSRequestId") .WithUpstreamHttpMethod(new List { "Get" }) + .WithHttpHandlerOptions(new HttpHandlerOptions(true, true)) .Build()); this.Given(x => x.GivenTheDownStreamUrlIs("any old string")) .And(x => x.GivenTheQosProviderHouseReturns(new OkResponse(new NoQoSProvider()))) .And(x => x.GivenTheDownStreamRouteIs(downstreamRoute)) - .And(x => x.GivenTheRequestBuilderReturns(new Ocelot.Request.Request(new HttpRequestMessage(), true, new NoQoSProvider()))) + .And(x => x.GivenTheRequestBuilderReturns(new Ocelot.Request.Request(new HttpRequestMessage(), true, new NoQoSProvider(), false, false))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) .BDDfy(); @@ -103,7 +105,11 @@ private void GivenTheRequestBuilderReturns(Ocelot.Request.Request request) _request = new OkResponse(request); _requestBuilder - .Setup(x => x.Build(It.IsAny(), It.IsAny(), It.IsAny())) + .Setup(x => x.Build(It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) .ReturnsAsync(_request); } diff --git a/test/Ocelot.UnitTests/Request/HttpRequestCreatorTests.cs b/test/Ocelot.UnitTests/Request/HttpRequestCreatorTests.cs index d831aed97..f4f67ca02 100644 --- a/test/Ocelot.UnitTests/Request/HttpRequestCreatorTests.cs +++ b/test/Ocelot.UnitTests/Request/HttpRequestCreatorTests.cs @@ -15,6 +15,9 @@ public class HttpRequestCreatorTests private readonly bool _isQos; private readonly IQoSProvider _qoSProvider; private readonly HttpRequestMessage _requestMessage; + private readonly bool _useCookieContainer; + private readonly bool _allowAutoRedirect; + private Response _response; public HttpRequestCreatorTests() @@ -22,6 +25,9 @@ public HttpRequestCreatorTests() _requestCreator = new HttpRequestCreator(); _isQos = true; _qoSProvider = new NoQoSProvider(); + _useCookieContainer = false; + _allowAutoRedirect = false; + _requestMessage = new HttpRequestMessage(); } @@ -30,12 +36,19 @@ public void ShouldBuildRequest() { this.When(x => x.WhenIBuildARequest()) .Then(x => x.ThenTheRequestContainsTheRequestMessage()) + .Then(x => x.ThenTheRequestContainsTheIsQos()) + .Then(x => x.ThenTheRequestContainsTheQosProvider()) + .Then(x => x.ThenTheRequestContainsUseCookieContainer()) + .Then(x => x.ThenTheRequestContainsAllowAutoRedirect()) .BDDfy(); } private void WhenIBuildARequest() { - _response = _requestCreator.Build(_requestMessage, _isQos, _qoSProvider).GetAwaiter().GetResult(); + _response = _requestCreator.Build(_requestMessage, + _isQos, _qoSProvider, _useCookieContainer, _allowAutoRedirect) + .GetAwaiter() + .GetResult(); } private void ThenTheRequestContainsTheRequestMessage() @@ -52,5 +65,15 @@ private void ThenTheRequestContainsTheQosProvider() { _response.Data.QosProvider.ShouldBe(_qoSProvider); } + + private void ThenTheRequestContainsUseCookieContainer() + { + _response.Data.UseCookieContainer.ShouldBe(_useCookieContainer); + } + + private void ThenTheRequestContainsAllowAutoRedirect() + { + _response.Data.AllowAutoRedirect.ShouldBe(_allowAutoRedirect); + } } } diff --git a/test/Ocelot.UnitTests/Requester/HttpRequesterMiddlewareTests.cs b/test/Ocelot.UnitTests/Requester/HttpRequesterMiddlewareTests.cs index f5570f15a..45aafecd6 100644 --- a/test/Ocelot.UnitTests/Requester/HttpRequesterMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Requester/HttpRequesterMiddlewareTests.cs @@ -28,7 +28,7 @@ public HttpRequesterMiddlewareTests() [Fact] public void should_call_scoped_data_repository_correctly() { - this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),true, new NoQoSProvider()))) + this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),true, new NoQoSProvider(), false, false))) .And(x => x.GivenTheRequesterReturns(new HttpResponseMessage())) .And(x => x.GivenTheScopedRepoReturns()) .When(x => x.WhenICallTheMiddleware())