mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +08:00
* #568 Ocelot wont start if QoSOptions specified and no QoS DelegatingHandler registered e.g. no use of Ocelot.Provider.Polly. Also adds a NoQosDelegatingHandler and logs an error if ive missed something and the user can get to making the request * #568 sadly something wierd with IServiceProvider and FluentValidation so I'm just defaulting to warning and noqosdelegatinghandler if someone doesnt register but provides options, also added basic in memory cache in case people dont use a specific package
This commit is contained in:
parent
e257d82abf
commit
8db5570840
16
src/Ocelot/Cache/CacheObject.cs
Normal file
16
src/Ocelot/Cache/CacheObject.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace Ocelot.Cache
|
||||
{
|
||||
using System;
|
||||
|
||||
class CacheObject<T>
|
||||
{
|
||||
public CacheObject(T value, DateTime expires)
|
||||
{
|
||||
Value = value;
|
||||
Expires = expires;
|
||||
}
|
||||
|
||||
public T Value { get; }
|
||||
public DateTime Expires { get; }
|
||||
}
|
||||
}
|
@ -5,28 +5,7 @@ namespace Ocelot.Cache
|
||||
public interface IOcelotCache<T>
|
||||
{
|
||||
void Add(string key, T value, TimeSpan ttl, string region);
|
||||
void AddAndDelete(string key, T value, TimeSpan ttl, string region);
|
||||
T Get(string key, string region);
|
||||
void ClearRegion(string region);
|
||||
}
|
||||
|
||||
public class NoCache<T> : IOcelotCache<T>
|
||||
{
|
||||
public void Add(string key, T value, TimeSpan ttl, string region)
|
||||
{
|
||||
}
|
||||
|
||||
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
|
||||
{
|
||||
}
|
||||
|
||||
public void ClearRegion(string region)
|
||||
{
|
||||
}
|
||||
|
||||
public T Get(string key, string region)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
71
src/Ocelot/Cache/InMemoryCache.cs
Normal file
71
src/Ocelot/Cache/InMemoryCache.cs
Normal file
@ -0,0 +1,71 @@
|
||||
namespace Ocelot.Cache
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class InMemoryCache<T> : IOcelotCache<T>
|
||||
{
|
||||
private readonly Dictionary<string, CacheObject<T>> _cache;
|
||||
private readonly Dictionary<string, List<string>> _regions;
|
||||
|
||||
public InMemoryCache()
|
||||
{
|
||||
_cache = new Dictionary<string, CacheObject<T>>();
|
||||
_regions = new Dictionary<string, List<string>>();
|
||||
}
|
||||
|
||||
public void Add(string key, T value, TimeSpan ttl, string region)
|
||||
{
|
||||
if (ttl.TotalMilliseconds <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var expires = DateTime.UtcNow.Add(ttl);
|
||||
|
||||
_cache.Add(key, new CacheObject<T>(value, expires));
|
||||
|
||||
if (_regions.ContainsKey(region))
|
||||
{
|
||||
var current = _regions[region];
|
||||
if (!current.Contains(key))
|
||||
{
|
||||
current.Add(key);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_regions.Add(region, new List<string>{ key });
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearRegion(string region)
|
||||
{
|
||||
if (_regions.ContainsKey(region))
|
||||
{
|
||||
var keys = _regions[region];
|
||||
foreach (var key in keys)
|
||||
{
|
||||
_cache.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public T Get(string key, string region)
|
||||
{
|
||||
if (_cache.ContainsKey(key))
|
||||
{
|
||||
var cached = _cache[key];
|
||||
|
||||
if (cached.Expires > DateTime.UtcNow)
|
||||
{
|
||||
return cached.Value;
|
||||
}
|
||||
|
||||
_cache.Remove(key);
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
}
|
@ -9,12 +9,19 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Ocelot.Configuration.Validator
|
||||
{
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Requester;
|
||||
|
||||
public class FileConfigurationFluentValidator : AbstractValidator<FileConfiguration>, IConfigurationValidator
|
||||
{
|
||||
public FileConfigurationFluentValidator(IAuthenticationSchemeProvider authenticationSchemeProvider)
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public FileConfigurationFluentValidator(IAuthenticationSchemeProvider authenticationSchemeProvider, IServiceProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
RuleFor(configuration => configuration.ReRoutes)
|
||||
.SetCollectionValidator(new ReRouteFluentValidator(authenticationSchemeProvider));
|
||||
.SetCollectionValidator(new ReRouteFluentValidator(authenticationSchemeProvider, provider));
|
||||
|
||||
RuleForEach(configuration => configuration.ReRoutes)
|
||||
.Must((config, reRoute) => IsNotDuplicateIn(reRoute, config.ReRoutes))
|
||||
|
@ -1,19 +1,24 @@
|
||||
using FluentValidation;
|
||||
namespace Ocelot.Configuration.Validator
|
||||
{
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Ocelot.Configuration.File;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Requester;
|
||||
|
||||
namespace Ocelot.Configuration.Validator
|
||||
{
|
||||
public class ReRouteFluentValidator : AbstractValidator<FileReRoute>
|
||||
{
|
||||
private readonly IAuthenticationSchemeProvider _authenticationSchemeProvider;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public ReRouteFluentValidator(IAuthenticationSchemeProvider authenticationSchemeProvider)
|
||||
public ReRouteFluentValidator(IAuthenticationSchemeProvider authenticationSchemeProvider, IServiceProvider serviceProvider)
|
||||
{
|
||||
_authenticationSchemeProvider = authenticationSchemeProvider;
|
||||
_serviceProvider = serviceProvider;
|
||||
|
||||
RuleFor(reRoute => reRoute.DownstreamPathTemplate)
|
||||
.Must(path => path.StartsWith("/"))
|
||||
@ -48,11 +53,13 @@ namespace Ocelot.Configuration.Validator
|
||||
.WithMessage("{PropertyValue} is unsupported authentication provider");
|
||||
|
||||
When(reRoute => reRoute.UseServiceDiscovery, () => {
|
||||
RuleFor(r => r.ServiceName).NotEmpty().WithMessage("ServiceName cannot be empty or null when using service discovery or Ocelot cannot look up your service!");
|
||||
RuleFor(r => r.ServiceName).NotEmpty()
|
||||
.WithMessage("ServiceName cannot be empty or null when using service discovery or Ocelot cannot look up your service!");
|
||||
});
|
||||
|
||||
When(reRoute => !reRoute.UseServiceDiscovery, () => {
|
||||
RuleFor(r => r.DownstreamHostAndPorts).NotEmpty().WithMessage("When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!");
|
||||
RuleFor(r => r.DownstreamHostAndPorts).NotEmpty()
|
||||
.WithMessage("When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!");
|
||||
});
|
||||
|
||||
When(reRoute => !reRoute.UseServiceDiscovery, () => {
|
||||
|
@ -45,13 +45,10 @@ namespace Ocelot.DependencyInjection
|
||||
{
|
||||
Configuration = configurationRoot;
|
||||
Services = services;
|
||||
|
||||
Services.Configure<FileConfiguration>(configurationRoot);
|
||||
|
||||
//default no caches...
|
||||
Services.TryAddSingleton<IOcelotCache<FileConfiguration>, NoCache<FileConfiguration>>();
|
||||
Services.TryAddSingleton<IOcelotCache<CachedResponse>, NoCache<CachedResponse>>();
|
||||
|
||||
Services.TryAddSingleton<IOcelotCache<FileConfiguration>, InMemoryCache<FileConfiguration>>();
|
||||
Services.TryAddSingleton<IOcelotCache<CachedResponse>, InMemoryCache<CachedResponse>>();
|
||||
Services.TryAddSingleton<IHttpResponseHeaderReplacer, HttpResponseHeaderReplacer>();
|
||||
Services.TryAddSingleton<IHttpContextRequestHeaderReplacer, HttpContextRequestHeaderReplacer>();
|
||||
Services.TryAddSingleton<IHeaderFindAndReplaceCreator, HeaderFindAndReplaceCreator>();
|
||||
@ -105,6 +102,18 @@ namespace Ocelot.DependencyInjection
|
||||
Services.TryAddSingleton<IRequestScopedDataRepository, HttpDataRepository>();
|
||||
Services.AddMemoryCache();
|
||||
Services.TryAddSingleton<OcelotDiagnosticListener>();
|
||||
Services.TryAddSingleton<IMultiplexer, Multiplexer>();
|
||||
Services.TryAddSingleton<IResponseAggregator, SimpleJsonResponseAggregator>();
|
||||
Services.AddSingleton<ITracingHandlerFactory, TracingHandlerFactory>();
|
||||
Services.TryAddSingleton<IFileConfigurationPollerOptions, InMemoryFileConfigurationPollerOptions>();
|
||||
Services.TryAddSingleton<IAddHeadersToResponse, AddHeadersToResponse>();
|
||||
Services.TryAddSingleton<IPlaceholders, Placeholders>();
|
||||
Services.TryAddSingleton<IResponseAggregatorFactory, InMemoryResponseAggregatorFactory>();
|
||||
Services.TryAddSingleton<IDefinedAggregatorProvider, ServiceLocatorDefinedAggregatorProvider>();
|
||||
Services.TryAddSingleton<IDownstreamRequestCreator, DownstreamRequestCreator>();
|
||||
Services.TryAddSingleton<IFrameworkDescription, FrameworkDescription>();
|
||||
Services.TryAddSingleton<IQoSFactory, QoSFactory>();
|
||||
Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
|
||||
|
||||
//add asp.net services..
|
||||
var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly;
|
||||
@ -118,19 +127,6 @@ namespace Ocelot.DependencyInjection
|
||||
Services.AddLogging();
|
||||
Services.AddMiddlewareAnalysis();
|
||||
Services.AddWebEncoders();
|
||||
|
||||
Services.TryAddSingleton<IMultiplexer, Multiplexer>();
|
||||
Services.TryAddSingleton<IResponseAggregator, SimpleJsonResponseAggregator>();
|
||||
Services.AddSingleton<ITracingHandlerFactory, TracingHandlerFactory>();
|
||||
Services.TryAddSingleton<IFileConfigurationPollerOptions, InMemoryFileConfigurationPollerOptions>();
|
||||
Services.TryAddSingleton<IAddHeadersToResponse, AddHeadersToResponse>();
|
||||
Services.TryAddSingleton<IPlaceholders, Placeholders>();
|
||||
Services.TryAddSingleton<IResponseAggregatorFactory, InMemoryResponseAggregatorFactory>();
|
||||
Services.TryAddSingleton<IDefinedAggregatorProvider, ServiceLocatorDefinedAggregatorProvider>();
|
||||
Services.TryAddSingleton<IDownstreamRequestCreator, DownstreamRequestCreator>();
|
||||
Services.TryAddSingleton<IFrameworkDescription, FrameworkDescription>();
|
||||
Services.TryAddSingleton<IQoSFactory, QoSFactory>();
|
||||
Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
|
||||
}
|
||||
|
||||
public IOcelotBuilder AddSingletonDefinedAggregator<T>()
|
||||
|
@ -4,6 +4,7 @@ namespace Ocelot.Requester
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Responses;
|
||||
@ -14,18 +15,21 @@ namespace Ocelot.Requester
|
||||
private readonly ITracingHandlerFactory _tracingFactory;
|
||||
private readonly IQoSFactory _qoSFactory;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IOcelotLogger _logger;
|
||||
|
||||
public DelegatingHandlerHandlerFactory(
|
||||
ITracingHandlerFactory tracingFactory,
|
||||
IQoSFactory qoSFactory,
|
||||
IServiceProvider serviceProvider)
|
||||
IServiceProvider serviceProvider,
|
||||
IOcelotLoggerFactory loggerFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger<DelegatingHandlerHandlerFactory>();
|
||||
_serviceProvider = serviceProvider;
|
||||
_tracingFactory = tracingFactory;
|
||||
_qoSFactory = qoSFactory;
|
||||
}
|
||||
|
||||
public Response<List<Func<DelegatingHandler>>> Get(DownstreamReRoute request)
|
||||
public Response<List<Func<DelegatingHandler>>> Get(DownstreamReRoute downstreamReRoute)
|
||||
{
|
||||
var globalDelegatingHandlers = _serviceProvider
|
||||
.GetServices<GlobalDelegatingHandler>()
|
||||
@ -39,7 +43,7 @@ namespace Ocelot.Requester
|
||||
|
||||
foreach (var handler in globalDelegatingHandlers)
|
||||
{
|
||||
if (GlobalIsInHandlersConfig(request, handler))
|
||||
if (GlobalIsInHandlersConfig(downstreamReRoute, handler))
|
||||
{
|
||||
reRouteSpecificHandlers.Add(handler.DelegatingHandler);
|
||||
}
|
||||
@ -49,9 +53,9 @@ namespace Ocelot.Requester
|
||||
}
|
||||
}
|
||||
|
||||
if (request.DelegatingHandlers.Any())
|
||||
if (downstreamReRoute.DelegatingHandlers.Any())
|
||||
{
|
||||
var sorted = SortByConfigOrder(request, reRouteSpecificHandlers);
|
||||
var sorted = SortByConfigOrder(downstreamReRoute, reRouteSpecificHandlers);
|
||||
|
||||
foreach (var handler in sorted)
|
||||
{
|
||||
@ -59,14 +63,14 @@ namespace Ocelot.Requester
|
||||
}
|
||||
}
|
||||
|
||||
if (request.HttpHandlerOptions.UseTracing)
|
||||
if (downstreamReRoute.HttpHandlerOptions.UseTracing)
|
||||
{
|
||||
handlers.Add(() => (DelegatingHandler)_tracingFactory.Get());
|
||||
}
|
||||
|
||||
if (request.QosOptions.UseQos)
|
||||
if (downstreamReRoute.QosOptions.UseQos)
|
||||
{
|
||||
var handler = _qoSFactory.Get(request);
|
||||
var handler = _qoSFactory.Get(downstreamReRoute);
|
||||
|
||||
if (handler != null && !handler.IsError)
|
||||
{
|
||||
@ -74,7 +78,8 @@ namespace Ocelot.Requester
|
||||
}
|
||||
else
|
||||
{
|
||||
return new ErrorResponse<List<Func<DelegatingHandler>>>(handler?.Errors);
|
||||
_logger.LogWarning($"ReRoute {downstreamReRoute.UpstreamPathTemplate} specifies use QoS but no QosHandler found in DI container. Will use not use a QosHandler, please check your setup!");
|
||||
handlers.Add(() => new NoQosDelegatingHandler());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
namespace Ocelot.Requester
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Requester
|
||||
{
|
||||
public interface IDelegatingHandlerHandlerFactory
|
||||
{
|
||||
Response<List<Func<DelegatingHandler>>> Get(DownstreamReRoute request);
|
||||
Response<List<Func<DelegatingHandler>>> Get(DownstreamReRoute downstreamReRoute);
|
||||
}
|
||||
}
|
||||
|
8
src/Ocelot/Requester/NoQosDelegatingHandler.cs
Normal file
8
src/Ocelot/Requester/NoQosDelegatingHandler.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Ocelot.Requester
|
||||
{
|
||||
using System.Net.Http;
|
||||
|
||||
public class NoQosDelegatingHandler : DelegatingHandler
|
||||
{
|
||||
}
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CannotStartOcelotTests : IDisposable
|
||||
{
|
||||
private readonly Steps _steps;
|
||||
@ -42,6 +42,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
|
||||
exception.ShouldNotBeNull();
|
||||
exception.Message.ShouldBe("One or more errors occurred. (Unable to start Ocelot, errors are: Downstream Path Template test doesnt start with forward slash,Upstream Path Template api doesnt start with forward slash,When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!)");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -3,6 +3,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
@ -523,13 +523,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
QoSOptions = new FileQoSOptions()
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 3,
|
||||
DurationOfBreak = 5,
|
||||
TimeoutValue = 5000
|
||||
}
|
||||
UpstreamHttpMethod = new List<string> { "Get" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
69
test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs
Normal file
69
test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs
Normal file
@ -0,0 +1,69 @@
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Ocelot.Cache;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
public class InMemoryCacheTests
|
||||
{
|
||||
private readonly InMemoryCache<Fake> _cache;
|
||||
|
||||
public InMemoryCacheTests()
|
||||
{
|
||||
_cache = new InMemoryCache<Fake>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_cache()
|
||||
{
|
||||
var fake = new Fake(1);
|
||||
_cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
|
||||
var result = _cache.Get("1", "region");
|
||||
result.ShouldBe(fake);
|
||||
fake.Value.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_clear_region()
|
||||
{
|
||||
var fake = new Fake(1);
|
||||
_cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
|
||||
_cache.ClearRegion("region");
|
||||
var result = _cache.Get("1", "region");
|
||||
result.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_clear_key_if_ttl_expired()
|
||||
{
|
||||
var fake = new Fake(1);
|
||||
_cache.Add("1", fake, TimeSpan.FromMilliseconds(50), "region");
|
||||
Thread.Sleep(200);
|
||||
var result = _cache.Get("1", "region");
|
||||
result.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public void should_not_add_to_cache_if_timespan_empty(int ttl)
|
||||
{
|
||||
var fake = new Fake(1);
|
||||
_cache.Add("1", fake, TimeSpan.FromSeconds(ttl), "region");
|
||||
var result = _cache.Get("1", "region");
|
||||
result.ShouldBeNull();
|
||||
}
|
||||
|
||||
class Fake
|
||||
{
|
||||
public Fake(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int Value { get; }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Threading.Tasks;
|
||||
@ -12,12 +14,13 @@ using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Requester;
|
||||
using Requester;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ConfigurationFluentValidationTests
|
||||
{
|
||||
private readonly IConfigurationValidator _configurationValidator;
|
||||
private IConfigurationValidator _configurationValidator;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private Response<ConfigurationValidationResult> _result;
|
||||
private readonly Mock<IAuthenticationSchemeProvider> _provider;
|
||||
@ -25,7 +28,9 @@ namespace Ocelot.UnitTests.Configuration
|
||||
public ConfigurationFluentValidationTests()
|
||||
{
|
||||
_provider = new Mock<IAuthenticationSchemeProvider>();
|
||||
_configurationValidator = new FileConfigurationFluentValidator(_provider.Object);
|
||||
var provider = new ServiceCollection()
|
||||
.BuildServiceProvider();
|
||||
_configurationValidator = new FileConfigurationFluentValidator(_provider.Object, provider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -1046,6 +1051,15 @@ namespace Ocelot.UnitTests.Configuration
|
||||
});
|
||||
}
|
||||
|
||||
private void GivenAQosDelegate()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
QosDelegatingHandlerDelegate del = (a, b) => new FakeDelegatingHandler();
|
||||
services.AddSingleton<QosDelegatingHandlerDelegate>(del);
|
||||
var provider = services.BuildServiceProvider();
|
||||
_configurationValidator = new FileConfigurationFluentValidator(_provider.Object, provider);
|
||||
}
|
||||
|
||||
private class TestOptions : AuthenticationSchemeOptions
|
||||
{
|
||||
}
|
||||
|
@ -22,7 +22,8 @@ namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
private DelegatingHandlerHandlerFactory _factory;
|
||||
private readonly Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private DownstreamReRoute _request;
|
||||
private readonly Mock<IOcelotLogger> _logger;
|
||||
private DownstreamReRoute _downstreamReRoute;
|
||||
private Response<List<Func<DelegatingHandler>>> _result;
|
||||
private readonly Mock<IQoSFactory> _qosFactory;
|
||||
private readonly Mock<ITracingHandlerFactory> _tracingFactory;
|
||||
@ -36,6 +37,8 @@ namespace Ocelot.UnitTests.Requester
|
||||
_tracingFactory = new Mock<ITracingHandlerFactory>();
|
||||
_qosFactory = new Mock<IQoSFactory>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<DelegatingHandlerHandlerFactory>()).Returns(_logger.Object);
|
||||
_services = new ServiceCollection();
|
||||
_services.AddSingleton(_qosDelegate);
|
||||
}
|
||||
@ -300,7 +303,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
public void should_log_error_and_return_no_qos_provider_delegate_when_qos_factory_returns_error()
|
||||
{
|
||||
var qosOptions = new QoSOptionsBuilder()
|
||||
.WithTimeoutValue(1)
|
||||
@ -310,16 +313,60 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
var reRoute = new DownstreamReRouteBuilder()
|
||||
.WithQosOptions(qosOptions)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false, true)).WithLoadBalancerKey("").Build();
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, true, true))
|
||||
.WithLoadBalancerKey("")
|
||||
.Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosFactoryReturnsError())
|
||||
.And(x => GivenTheServiceProviderReturnsNothing())
|
||||
.And(x => GivenTheTracingFactoryReturns())
|
||||
.And(x => GivenTheServiceProviderReturnsGlobalDelegatingHandlers<FakeDelegatingHandler, FakeDelegatingHandlerTwo>())
|
||||
.And(x => GivenTheServiceProviderReturnsSpecificDelegatingHandlers<FakeDelegatingHandler, FakeDelegatingHandlerTwo>())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenAnErrorIsReturned())
|
||||
.Then(x => ThenThereIsDelegatesInProvider(4))
|
||||
.And(x => ThenHandlerAtPositionIs<FakeDelegatingHandler>(0))
|
||||
.And(x => ThenHandlerAtPositionIs<FakeDelegatingHandlerTwo>(1))
|
||||
.And(x => ThenHandlerAtPositionIs<FakeTracingHandler>(2))
|
||||
.And(x => ThenHandlerAtPositionIs<NoQosDelegatingHandler>(3))
|
||||
.And(_ => ThenTheWarningIsLogged())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_log_error_and_return_no_qos_provider_delegate_when_qos_factory_returns_null()
|
||||
{
|
||||
var qosOptions = new QoSOptionsBuilder()
|
||||
.WithTimeoutValue(1)
|
||||
.WithDurationOfBreak(1)
|
||||
.WithExceptionsAllowedBeforeBreaking(1)
|
||||
.Build();
|
||||
|
||||
var reRoute = new DownstreamReRouteBuilder()
|
||||
.WithQosOptions(qosOptions)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, true, true))
|
||||
.WithLoadBalancerKey("")
|
||||
.Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosFactoryReturnsNull())
|
||||
.And(x => GivenTheTracingFactoryReturns())
|
||||
.And(x => GivenTheServiceProviderReturnsGlobalDelegatingHandlers<FakeDelegatingHandler, FakeDelegatingHandlerTwo>())
|
||||
.And(x => GivenTheServiceProviderReturnsSpecificDelegatingHandlers<FakeDelegatingHandler, FakeDelegatingHandlerTwo>())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenThereIsDelegatesInProvider(4))
|
||||
.And(x => ThenHandlerAtPositionIs<FakeDelegatingHandler>(0))
|
||||
.And(x => ThenHandlerAtPositionIs<FakeDelegatingHandlerTwo>(1))
|
||||
.And(x => ThenHandlerAtPositionIs<FakeTracingHandler>(2))
|
||||
.And(x => ThenHandlerAtPositionIs<NoQosDelegatingHandler>(3))
|
||||
.And(_ => ThenTheWarningIsLogged())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheWarningIsLogged()
|
||||
{
|
||||
_logger.Verify(x => x.LogWarning($"ReRoute {_downstreamReRoute.UpstreamPathTemplate} specifies use QoS but no QosHandler found in DI container. Will use not use a QosHandler, please check your setup!"), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenHandlerAtPositionIs<T>(int pos)
|
||||
where T : DelegatingHandler
|
||||
{
|
||||
@ -396,6 +443,13 @@ namespace Ocelot.UnitTests.Requester
|
||||
.Returns(new ErrorResponse<DelegatingHandler>(new AnyError()));
|
||||
}
|
||||
|
||||
private void GivenTheQosFactoryReturnsNull()
|
||||
{
|
||||
_qosFactory
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns((ErrorResponse<DelegatingHandler>)null);
|
||||
}
|
||||
|
||||
private void ThenItIsQosHandler(int i)
|
||||
{
|
||||
var delegates = _result.Data;
|
||||
@ -411,14 +465,14 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
private void GivenTheFollowingRequest(DownstreamReRoute request)
|
||||
{
|
||||
_request = request;
|
||||
_downstreamReRoute = request;
|
||||
}
|
||||
|
||||
private void WhenIGet()
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider();
|
||||
_factory = new DelegatingHandlerHandlerFactory(_tracingFactory.Object, _qosFactory.Object, _serviceProvider);
|
||||
_result = _factory.Get(_request);
|
||||
_factory = new DelegatingHandlerHandlerFactory(_tracingFactory.Object, _qosFactory.Object, _serviceProvider, _loggerFactory.Object);
|
||||
_result = _factory.Get(_downstreamReRoute);
|
||||
}
|
||||
|
||||
private void ThenNoDelegatesAreInTheProvider()
|
||||
|
@ -89,22 +89,6 @@ namespace Ocelot.UnitTests.Requester
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenARealCache()
|
||||
{
|
||||
_realCache = new MemoryHttpClientCache();
|
||||
_builder = new HttpClientBuilder(_factory.Object, _realCache, _logger.Object);
|
||||
}
|
||||
|
||||
private void ThenTheHttpClientIsFromTheCache()
|
||||
{
|
||||
_againHttpClient.ShouldBe(_firstHttpClient);
|
||||
}
|
||||
|
||||
private void WhenISave()
|
||||
{
|
||||
_builder.Save();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_log_if_ignoring_ssl_errors()
|
||||
{
|
||||
@ -214,9 +198,25 @@ namespace Ocelot.UnitTests.Requester
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenARealCache()
|
||||
{
|
||||
_realCache = new MemoryHttpClientCache();
|
||||
_builder = new HttpClientBuilder(_factory.Object, _realCache, _logger.Object);
|
||||
}
|
||||
|
||||
private void ThenTheHttpClientIsFromTheCache()
|
||||
{
|
||||
_againHttpClient.ShouldBe(_firstHttpClient);
|
||||
}
|
||||
|
||||
private void WhenISave()
|
||||
{
|
||||
_builder.Save();
|
||||
}
|
||||
|
||||
private void GivenCacheIsCalledWithExpectedKey(string expectedKey)
|
||||
{
|
||||
this._cacheHandlers.Verify(x => x.Get(It.Is<string>(p => p.Equals(expectedKey, StringComparison.OrdinalIgnoreCase))), Times.Once);
|
||||
_cacheHandlers.Verify(x => x.Get(It.Is<string>(p => p.Equals(expectedKey, StringComparison.OrdinalIgnoreCase))), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheDangerousAcceptAnyServerCertificateValidatorWarningIsLogged()
|
||||
|
Loading…
x
Reference in New Issue
Block a user