mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
Merge pull request #129 from aringlot/feature/#128-cookie-proxying-and-auto-redirect-configuration
Cookie proxying and auto redirect configuration #128
This commit is contained in:
commit
4d0f389856
@ -29,6 +29,7 @@ namespace Ocelot.Configuration.Builder
|
|||||||
private ServiceProviderConfiguration _serviceProviderConfiguraion;
|
private ServiceProviderConfiguration _serviceProviderConfiguraion;
|
||||||
private bool _useQos;
|
private bool _useQos;
|
||||||
private QoSOptions _qosOptions;
|
private QoSOptions _qosOptions;
|
||||||
|
private HttpHandlerOptions _httpHandlerOptions;
|
||||||
public bool _enableRateLimiting;
|
public bool _enableRateLimiting;
|
||||||
public RateLimitOptions _rateLimitOptions;
|
public RateLimitOptions _rateLimitOptions;
|
||||||
|
|
||||||
@ -176,6 +177,11 @@ namespace Ocelot.Configuration.Builder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReRouteBuilder WithHttpHandlerOptions(HttpHandlerOptions input)
|
||||||
|
{
|
||||||
|
_httpHandlerOptions = input;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ReRoute Build()
|
public ReRoute Build()
|
||||||
{
|
{
|
||||||
@ -203,7 +209,8 @@ namespace Ocelot.Configuration.Builder
|
|||||||
_useQos,
|
_useQos,
|
||||||
_qosOptions,
|
_qosOptions,
|
||||||
_enableRateLimiting,
|
_enableRateLimiting,
|
||||||
_rateLimitOptions);
|
_rateLimitOptions,
|
||||||
|
_httpHandlerOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
private readonly IReRouteOptionsCreator _fileReRouteOptionsCreator;
|
private readonly IReRouteOptionsCreator _fileReRouteOptionsCreator;
|
||||||
private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator;
|
private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator;
|
||||||
private readonly IRegionCreator _regionCreator;
|
private readonly IRegionCreator _regionCreator;
|
||||||
|
private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator;
|
||||||
|
|
||||||
public FileOcelotConfigurationCreator(
|
public FileOcelotConfigurationCreator(
|
||||||
IOptions<FileConfiguration> options,
|
IOptions<FileConfiguration> options,
|
||||||
@ -55,7 +56,8 @@ namespace Ocelot.Configuration.Creator
|
|||||||
IQoSOptionsCreator qosOptionsCreator,
|
IQoSOptionsCreator qosOptionsCreator,
|
||||||
IReRouteOptionsCreator fileReRouteOptionsCreator,
|
IReRouteOptionsCreator fileReRouteOptionsCreator,
|
||||||
IRateLimitOptionsCreator rateLimitOptionsCreator,
|
IRateLimitOptionsCreator rateLimitOptionsCreator,
|
||||||
IRegionCreator regionCreator
|
IRegionCreator regionCreator,
|
||||||
|
IHttpHandlerOptionsCreator httpHandlerOptionsCreator
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_regionCreator = regionCreator;
|
_regionCreator = regionCreator;
|
||||||
@ -74,6 +76,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
_serviceProviderConfigCreator = serviceProviderConfigCreator;
|
_serviceProviderConfigCreator = serviceProviderConfigCreator;
|
||||||
_qosOptionsCreator = qosOptionsCreator;
|
_qosOptionsCreator = qosOptionsCreator;
|
||||||
_fileReRouteOptionsCreator = fileReRouteOptionsCreator;
|
_fileReRouteOptionsCreator = fileReRouteOptionsCreator;
|
||||||
|
_httpHandlerOptionsCreator = httpHandlerOptionsCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Response<IOcelotConfiguration>> Create()
|
public async Task<Response<IOcelotConfiguration>> Create()
|
||||||
@ -143,6 +146,8 @@ namespace Ocelot.Configuration.Creator
|
|||||||
|
|
||||||
var region = _regionCreator.Create(fileReRoute);
|
var region = _regionCreator.Create(fileReRoute);
|
||||||
|
|
||||||
|
var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileReRoute);
|
||||||
|
|
||||||
var reRoute = new ReRouteBuilder()
|
var reRoute = new ReRouteBuilder()
|
||||||
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
|
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
|
||||||
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
|
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
|
||||||
@ -168,6 +173,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
.WithQosOptions(qosOptions)
|
.WithQosOptions(qosOptions)
|
||||||
.WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting)
|
.WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting)
|
||||||
.WithRateLimitOptions(rateLimitOption)
|
.WithRateLimitOptions(rateLimitOption)
|
||||||
|
.WithHttpHandlerOptions(httpHandlerOptions)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await SetupLoadBalancer(reRoute);
|
await SetupLoadBalancer(reRoute);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using Ocelot.Configuration.File;
|
||||||
|
|
||||||
|
namespace Ocelot.Configuration.Creator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Describes creation of HttpHandlerOptions
|
||||||
|
/// </summary>
|
||||||
|
public interface IHttpHandlerOptionsCreator
|
||||||
|
{
|
||||||
|
HttpHandlerOptions Create(FileReRoute fileReRoute);
|
||||||
|
}
|
||||||
|
}
|
15
src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs
Normal file
15
src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ namespace Ocelot.Configuration.File
|
|||||||
FileCacheOptions = new FileCacheOptions();
|
FileCacheOptions = new FileCacheOptions();
|
||||||
QoSOptions = new FileQoSOptions();
|
QoSOptions = new FileQoSOptions();
|
||||||
RateLimitOptions = new FileRateLimitRule();
|
RateLimitOptions = new FileRateLimitRule();
|
||||||
|
HttpHandlerOptions = new FileHttpHandlerOptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string DownstreamPathTemplate { get; set; }
|
public string DownstreamPathTemplate { get; set; }
|
||||||
@ -35,5 +36,6 @@ namespace Ocelot.Configuration.File
|
|||||||
public FileQoSOptions QoSOptions { get; set; }
|
public FileQoSOptions QoSOptions { get; set; }
|
||||||
public string LoadBalancer {get;set;}
|
public string LoadBalancer {get;set;}
|
||||||
public FileRateLimitRule RateLimitOptions { get; set; }
|
public FileRateLimitRule RateLimitOptions { get; set; }
|
||||||
|
public FileHttpHandlerOptions HttpHandlerOptions { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
25
src/Ocelot/Configuration/HttpHandlerOptions.cs
Normal file
25
src/Ocelot/Configuration/HttpHandlerOptions.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
namespace Ocelot.Configuration
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Describes configuration parameters for http handler,
|
||||||
|
/// that is created to handle a request to service
|
||||||
|
/// </summary>
|
||||||
|
public class HttpHandlerOptions
|
||||||
|
{
|
||||||
|
public HttpHandlerOptions(bool allowAutoRedirect, bool useCookieContainer)
|
||||||
|
{
|
||||||
|
AllowAutoRedirect = allowAutoRedirect;
|
||||||
|
UseCookieContainer = useCookieContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specify if auto redirect is enabled
|
||||||
|
/// </summary>
|
||||||
|
public bool AllowAutoRedirect { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specify is handler has to use a cookie container
|
||||||
|
/// </summary>
|
||||||
|
public bool UseCookieContainer { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,8 @@ namespace Ocelot.Configuration
|
|||||||
bool isQos,
|
bool isQos,
|
||||||
QoSOptions qosOptions,
|
QoSOptions qosOptions,
|
||||||
bool enableEndpointRateLimiting,
|
bool enableEndpointRateLimiting,
|
||||||
RateLimitOptions ratelimitOptions)
|
RateLimitOptions ratelimitOptions,
|
||||||
|
HttpHandlerOptions httpHandlerOptions)
|
||||||
{
|
{
|
||||||
ReRouteKey = reRouteKey;
|
ReRouteKey = reRouteKey;
|
||||||
ServiceProviderConfiguraion = serviceProviderConfiguraion;
|
ServiceProviderConfiguraion = serviceProviderConfiguraion;
|
||||||
@ -58,6 +59,7 @@ namespace Ocelot.Configuration
|
|||||||
QosOptionsOptions = qosOptions;
|
QosOptionsOptions = qosOptions;
|
||||||
EnableEndpointEndpointRateLimiting = enableEndpointRateLimiting;
|
EnableEndpointEndpointRateLimiting = enableEndpointRateLimiting;
|
||||||
RateLimitOptions = ratelimitOptions;
|
RateLimitOptions = ratelimitOptions;
|
||||||
|
HttpHandlerOptions = httpHandlerOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ReRouteKey {get;private set;}
|
public string ReRouteKey {get;private set;}
|
||||||
@ -84,5 +86,6 @@ namespace Ocelot.Configuration
|
|||||||
public ServiceProviderConfiguration ServiceProviderConfiguraion { get; private set; }
|
public ServiceProviderConfiguration ServiceProviderConfiguraion { get; private set; }
|
||||||
public bool EnableEndpointEndpointRateLimiting { get; private set; }
|
public bool EnableEndpointEndpointRateLimiting { get; private set; }
|
||||||
public RateLimitOptions RateLimitOptions { get; private set; }
|
public RateLimitOptions RateLimitOptions { get; private set; }
|
||||||
|
public HttpHandlerOptions HttpHandlerOptions { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -84,6 +84,7 @@ namespace Ocelot.DependencyInjection
|
|||||||
services.TryAddSingleton<IQoSOptionsCreator, QoSOptionsCreator>();
|
services.TryAddSingleton<IQoSOptionsCreator, QoSOptionsCreator>();
|
||||||
services.TryAddSingleton<IReRouteOptionsCreator, ReRouteOptionsCreator>();
|
services.TryAddSingleton<IReRouteOptionsCreator, ReRouteOptionsCreator>();
|
||||||
services.TryAddSingleton<IRateLimitOptionsCreator, RateLimitOptionsCreator>();
|
services.TryAddSingleton<IRateLimitOptionsCreator, RateLimitOptionsCreator>();
|
||||||
|
services.TryAddSingleton<IHttpHandlerOptionsCreator, HttpHandlerOptionsCreator>();
|
||||||
|
|
||||||
var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
|
var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
|
||||||
|
|
||||||
|
@ -10,9 +10,11 @@ namespace Ocelot.Request.Builder
|
|||||||
public async Task<Response<Request>> Build(
|
public async Task<Response<Request>> Build(
|
||||||
HttpRequestMessage httpRequestMessage,
|
HttpRequestMessage httpRequestMessage,
|
||||||
bool isQos,
|
bool isQos,
|
||||||
IQoSProvider qosProvider)
|
IQoSProvider qosProvider,
|
||||||
|
bool useCookieContainer,
|
||||||
|
bool allowAutoRedirect)
|
||||||
{
|
{
|
||||||
return new OkResponse<Request>(new Request(httpRequestMessage, isQos, qosProvider));
|
return new OkResponse<Request>(new Request(httpRequestMessage, isQos, qosProvider, useCookieContainer, allowAutoRedirect));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,8 @@
|
|||||||
Task<Response<Request>> Build(
|
Task<Response<Request>> Build(
|
||||||
HttpRequestMessage httpRequestMessage,
|
HttpRequestMessage httpRequestMessage,
|
||||||
bool isQos,
|
bool isQos,
|
||||||
IQoSProvider qosProvider);
|
IQoSProvider qosProvider,
|
||||||
|
bool useCookieContainer,
|
||||||
|
bool allowAutoRedirect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,9 @@ namespace Ocelot.Request.Middleware
|
|||||||
var buildResult = await _requestCreator.Build(
|
var buildResult = await _requestCreator.Build(
|
||||||
DownstreamRequest,
|
DownstreamRequest,
|
||||||
DownstreamRoute.ReRoute.IsQos,
|
DownstreamRoute.ReRoute.IsQos,
|
||||||
qosProvider.Data);
|
qosProvider.Data,
|
||||||
|
DownstreamRoute.ReRoute.HttpHandlerOptions.UseCookieContainer,
|
||||||
|
DownstreamRoute.ReRoute.HttpHandlerOptions.AllowAutoRedirect);
|
||||||
|
|
||||||
if (buildResult.IsError)
|
if (buildResult.IsError)
|
||||||
{
|
{
|
||||||
|
@ -8,15 +8,21 @@ namespace Ocelot.Request
|
|||||||
public Request(
|
public Request(
|
||||||
HttpRequestMessage httpRequestMessage,
|
HttpRequestMessage httpRequestMessage,
|
||||||
bool isQos,
|
bool isQos,
|
||||||
IQoSProvider qosProvider)
|
IQoSProvider qosProvider,
|
||||||
|
bool allowAutoRedirect,
|
||||||
|
bool useCookieContainer)
|
||||||
{
|
{
|
||||||
HttpRequestMessage = httpRequestMessage;
|
HttpRequestMessage = httpRequestMessage;
|
||||||
IsQos = isQos;
|
IsQos = isQos;
|
||||||
QosProvider = qosProvider;
|
QosProvider = qosProvider;
|
||||||
|
AllowAutoRedirect = allowAutoRedirect;
|
||||||
|
UseCookieContainer = useCookieContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpRequestMessage HttpRequestMessage { get; private set; }
|
public HttpRequestMessage HttpRequestMessage { get; private set; }
|
||||||
public bool IsQos { get; private set; }
|
public bool IsQos { get; private set; }
|
||||||
public IQoSProvider QosProvider { get; private set; }
|
public IQoSProvider QosProvider { get; private set; }
|
||||||
|
public bool AllowAutoRedirect { get; private set; }
|
||||||
|
public bool UseCookieContainer { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,9 @@ namespace Ocelot.Requester
|
|||||||
return this;
|
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));
|
var client = new HttpClient(CreateHttpMessageHandler(httpclientHandler));
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Ocelot.Configuration;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using Polly.CircuitBreaker;
|
using Polly.CircuitBreaker;
|
||||||
@ -14,7 +15,8 @@ namespace Ocelot.Requester
|
|||||||
private readonly IHttpClientCache _cacheHandlers;
|
private readonly IHttpClientCache _cacheHandlers;
|
||||||
private readonly IOcelotLogger _logger;
|
private readonly IOcelotLogger _logger;
|
||||||
|
|
||||||
public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory, IHttpClientCache cacheHandlers)
|
public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory,
|
||||||
|
IHttpClientCache cacheHandlers)
|
||||||
{
|
{
|
||||||
_logger = loggerFactory.CreateLogger<HttpClientHttpRequester>();
|
_logger = loggerFactory.CreateLogger<HttpClientHttpRequester>();
|
||||||
_cacheHandlers = cacheHandlers;
|
_cacheHandlers = cacheHandlers;
|
||||||
@ -26,7 +28,7 @@ namespace Ocelot.Requester
|
|||||||
|
|
||||||
var cacheKey = GetCacheKey(request, builder);
|
var cacheKey = GetCacheKey(request, builder);
|
||||||
|
|
||||||
var httpClient = GetHttpClient(cacheKey, builder);
|
var httpClient = GetHttpClient(cacheKey, builder, request.UseCookieContainer, request.AllowAutoRedirect);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -54,13 +56,13 @@ namespace Ocelot.Requester
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IHttpClient GetHttpClient(string cacheKey, IHttpClientBuilder builder)
|
private IHttpClient GetHttpClient(string cacheKey, IHttpClientBuilder builder, bool useCookieContainer, bool allowAutoRedirect)
|
||||||
{
|
{
|
||||||
var httpClient = _cacheHandlers.Get(cacheKey);
|
var httpClient = _cacheHandlers.Get(cacheKey);
|
||||||
|
|
||||||
if (httpClient == null)
|
if (httpClient == null)
|
||||||
{
|
{
|
||||||
httpClient = builder.Create();
|
httpClient = builder.Create(useCookieContainer, allowAutoRedirect);
|
||||||
}
|
}
|
||||||
return httpClient;
|
return httpClient;
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ namespace Ocelot.Requester
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the <see cref="HttpClient"/>
|
/// Creates the <see cref="HttpClient"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IHttpClient Create();
|
/// <param name="useCookies">Defines if http client should use cookie container</param>
|
||||||
|
/// <param name="allowAutoRedirect">Defines if http client should allow auto redirect</param>
|
||||||
|
IHttpClient Create(bool useCookies, bool allowAutoRedirect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
private Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
|
private Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
|
||||||
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
||||||
private Mock<IRegionCreator> _regionCreator;
|
private Mock<IRegionCreator> _regionCreator;
|
||||||
|
private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
|
||||||
|
|
||||||
public FileConfigurationCreatorTests()
|
public FileConfigurationCreatorTests()
|
||||||
{
|
{
|
||||||
@ -66,6 +67,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_fileReRouteOptionsCreator = new Mock<IReRouteOptionsCreator>();
|
_fileReRouteOptionsCreator = new Mock<IReRouteOptionsCreator>();
|
||||||
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
|
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
|
||||||
_regionCreator = new Mock<IRegionCreator>();
|
_regionCreator = new Mock<IRegionCreator>();
|
||||||
|
_httpHandlerOptionsCreator = new Mock<IHttpHandlerOptionsCreator>();
|
||||||
|
|
||||||
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
||||||
_fileConfig.Object, _validator.Object, _logger.Object,
|
_fileConfig.Object, _validator.Object, _logger.Object,
|
||||||
@ -73,7 +75,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
|
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
|
||||||
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
|
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
|
||||||
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
|
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
|
||||||
_rateLimitOptions.Object, _regionCreator.Object);
|
_rateLimitOptions.Object, _regionCreator.Object, _httpHandlerOptionsCreator.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -444,6 +446,45 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.BDDfy();
|
.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<FileReRoute>
|
||||||
|
{
|
||||||
|
new FileReRoute
|
||||||
|
{
|
||||||
|
DownstreamHost = "127.0.0.1",
|
||||||
|
UpstreamPathTemplate = "/api/products/{productId}",
|
||||||
|
DownstreamPathTemplate = "/products/{productId}",
|
||||||
|
UpstreamHttpMethod = new List<string> { "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<FileReRoute>()))
|
||||||
|
.Returns(httpHandlerOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheHttpHandlerOptionsCreatorIsCalledCorrectly()
|
||||||
|
{
|
||||||
|
_httpHandlerOptionsCreator.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[MemberData(nameof(AuthenticationConfigTestData.GetAuthenticationData), MemberType = typeof(AuthenticationConfigTestData))]
|
[MemberData(nameof(AuthenticationConfigTestData.GetAuthenticationData), MemberType = typeof(AuthenticationConfigTestData))]
|
||||||
public void should_create_with_headers_to_extract(string provider, IAuthenticationConfig config, FileConfiguration fileConfig)
|
public void should_create_with_headers_to_extract(string provider, IAuthenticationConfig config, FileConfiguration fileConfig)
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@
|
|||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Ocelot.Requester.QoS;
|
using Ocelot.Requester.QoS;
|
||||||
|
using Ocelot.Configuration;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
|
||||||
public class HttpRequestBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
public class HttpRequestBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
@ -50,12 +51,13 @@
|
|||||||
new ReRouteBuilder()
|
new ReRouteBuilder()
|
||||||
.WithRequestIdKey("LSRequestId")
|
.WithRequestIdKey("LSRequestId")
|
||||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||||
|
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true))
|
||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
||||||
.And(x => x.GivenTheQosProviderHouseReturns(new OkResponse<IQoSProvider>(new NoQoSProvider())))
|
.And(x => x.GivenTheQosProviderHouseReturns(new OkResponse<IQoSProvider>(new NoQoSProvider())))
|
||||||
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
.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())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -103,7 +105,11 @@
|
|||||||
_request = new OkResponse<Ocelot.Request.Request>(request);
|
_request = new OkResponse<Ocelot.Request.Request>(request);
|
||||||
|
|
||||||
_requestBuilder
|
_requestBuilder
|
||||||
.Setup(x => x.Build(It.IsAny<HttpRequestMessage>(), It.IsAny<bool>(), It.IsAny<IQoSProvider>()))
|
.Setup(x => x.Build(It.IsAny<HttpRequestMessage>(),
|
||||||
|
It.IsAny<bool>(),
|
||||||
|
It.IsAny<IQoSProvider>(),
|
||||||
|
It.IsAny<bool>(),
|
||||||
|
It.IsAny<bool>()))
|
||||||
.ReturnsAsync(_request);
|
.ReturnsAsync(_request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,9 @@
|
|||||||
private readonly bool _isQos;
|
private readonly bool _isQos;
|
||||||
private readonly IQoSProvider _qoSProvider;
|
private readonly IQoSProvider _qoSProvider;
|
||||||
private readonly HttpRequestMessage _requestMessage;
|
private readonly HttpRequestMessage _requestMessage;
|
||||||
|
private readonly bool _useCookieContainer;
|
||||||
|
private readonly bool _allowAutoRedirect;
|
||||||
|
|
||||||
private Response<Ocelot.Request.Request> _response;
|
private Response<Ocelot.Request.Request> _response;
|
||||||
|
|
||||||
public HttpRequestCreatorTests()
|
public HttpRequestCreatorTests()
|
||||||
@ -22,6 +25,9 @@
|
|||||||
_requestCreator = new HttpRequestCreator();
|
_requestCreator = new HttpRequestCreator();
|
||||||
_isQos = true;
|
_isQos = true;
|
||||||
_qoSProvider = new NoQoSProvider();
|
_qoSProvider = new NoQoSProvider();
|
||||||
|
_useCookieContainer = false;
|
||||||
|
_allowAutoRedirect = false;
|
||||||
|
|
||||||
_requestMessage = new HttpRequestMessage();
|
_requestMessage = new HttpRequestMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,12 +36,19 @@
|
|||||||
{
|
{
|
||||||
this.When(x => x.WhenIBuildARequest())
|
this.When(x => x.WhenIBuildARequest())
|
||||||
.Then(x => x.ThenTheRequestContainsTheRequestMessage())
|
.Then(x => x.ThenTheRequestContainsTheRequestMessage())
|
||||||
|
.Then(x => x.ThenTheRequestContainsTheIsQos())
|
||||||
|
.Then(x => x.ThenTheRequestContainsTheQosProvider())
|
||||||
|
.Then(x => x.ThenTheRequestContainsUseCookieContainer())
|
||||||
|
.Then(x => x.ThenTheRequestContainsAllowAutoRedirect())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIBuildARequest()
|
private void WhenIBuildARequest()
|
||||||
{
|
{
|
||||||
_response = _requestCreator.Build(_requestMessage, _isQos, _qoSProvider).GetAwaiter().GetResult();
|
_response = _requestCreator.Build(_requestMessage,
|
||||||
|
_isQos, _qoSProvider, _useCookieContainer, _allowAutoRedirect)
|
||||||
|
.GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheRequestContainsTheRequestMessage()
|
private void ThenTheRequestContainsTheRequestMessage()
|
||||||
@ -52,5 +65,15 @@
|
|||||||
{
|
{
|
||||||
_response.Data.QosProvider.ShouldBe(_qoSProvider);
|
_response.Data.QosProvider.ShouldBe(_qoSProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ThenTheRequestContainsUseCookieContainer()
|
||||||
|
{
|
||||||
|
_response.Data.UseCookieContainer.ShouldBe(_useCookieContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheRequestContainsAllowAutoRedirect()
|
||||||
|
{
|
||||||
|
_response.Data.AllowAutoRedirect.ShouldBe(_allowAutoRedirect);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_scoped_data_repository_correctly()
|
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.GivenTheRequesterReturns(new HttpResponseMessage()))
|
||||||
.And(x => x.GivenTheScopedRepoReturns())
|
.And(x => x.GivenTheScopedRepoReturns())
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user