diff --git a/src/Ocelot/Cache/IOcelotCache.cs b/src/Ocelot/Cache/IOcelotCache.cs index b445b647..e70afec1 100644 --- a/src/Ocelot/Cache/IOcelotCache.cs +++ b/src/Ocelot/Cache/IOcelotCache.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; namespace Ocelot.Cache { @@ -9,5 +8,25 @@ namespace Ocelot.Cache void AddAndDelete(string key, T value, TimeSpan ttl, string region); T Get(string key, string region); void ClearRegion(string region); + } + + public class NoCache : IOcelotCache + { + 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); + } } } diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 8a27ccba..00c2e322 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -1,29 +1,25 @@ -using System; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; -using Ocelot.Logging; -using Ocelot.Middleware; -using System.IO; -using Ocelot.Middleware.Multiplexer; - -namespace Ocelot.Cache.Middleware +namespace Ocelot.Cache.Middleware { + using System; + using System.Linq; + using System.Net.Http; + using System.Threading.Tasks; + using Ocelot.Logging; + using Ocelot.Middleware; + using System.IO; + public class OutputCacheMiddleware : OcelotMiddleware { private readonly OcelotRequestDelegate _next; private readonly IOcelotCache _outputCache; - private readonly IRegionCreator _regionCreator; public OutputCacheMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory, - IOcelotCache outputCache, - IRegionCreator regionCreator) + IOcelotCache outputCache) :base(loggerFactory.CreateLogger()) { _next = next; _outputCache = outputCache; - _regionCreator = regionCreator; } public async Task Invoke(DownstreamContext context) diff --git a/src/Ocelot/Cache/OcelotCacheManagerCache.cs b/src/Ocelot/Cache/OcelotCacheManagerCache.cs deleted file mode 100644 index 29f1ed33..00000000 --- a/src/Ocelot/Cache/OcelotCacheManagerCache.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using CacheManager.Core; - -namespace Ocelot.Cache -{ - public class OcelotCacheManagerCache : IOcelotCache - { - private readonly ICacheManager _cacheManager; - - public OcelotCacheManagerCache(ICacheManager cacheManager) - { - _cacheManager = cacheManager; - } - - public void Add(string key, T value, TimeSpan ttl, string region) - { - _cacheManager.Add(new CacheItem(key, region, value, ExpirationMode.Absolute, ttl)); - } - - public void AddAndDelete(string key, T value, TimeSpan ttl, string region) - { - var exists = _cacheManager.Get(key); - - if (exists != null) - { - _cacheManager.Remove(key); - } - - Add(key, value, ttl, region); - } - - public T Get(string key, string region) - { - return _cacheManager.Get(key, region); - } - - public void ClearRegion(string region) - { - _cacheManager.ClearRegion(region); - } - } -} \ No newline at end of file diff --git a/src/Ocelot/Cache/Regions.cs b/src/Ocelot/Cache/Regions.cs index baec6087..2ef0f6a8 100644 --- a/src/Ocelot/Cache/Regions.cs +++ b/src/Ocelot/Cache/Regions.cs @@ -1,14 +1,14 @@ -using System.Collections.Generic; - -namespace Ocelot.Cache -{ - public class Regions - { - public Regions(List value) - { - Value = value; - } - - public List Value {get;private set;} - } +namespace Ocelot.Cache +{ + using System.Collections.Generic; + + public class Regions + { + public Regions(List value) + { + Value = value; + } + + public List Value { get; } + } } diff --git a/src/Ocelot/Configuration/CacheOptions.cs b/src/Ocelot/Configuration/CacheOptions.cs index a3a926a4..46c49280 100644 --- a/src/Ocelot/Configuration/CacheOptions.cs +++ b/src/Ocelot/Configuration/CacheOptions.cs @@ -8,7 +8,8 @@ Region = region; } - public int TtlSeconds { get; private set; } - public string Region {get;private set;} + public int TtlSeconds { get; private set; } + + public string Region { get; private set; } } } diff --git a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs index b6684013..21e52f0b 100644 --- a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs @@ -1,4 +1,3 @@ -using CacheManager.Core; using System; using System.Net.Http; using IdentityServer4.AccessTokenValidation; @@ -14,8 +13,6 @@ namespace Ocelot.DependencyInjection IConfiguration Configuration { get; } IOcelotBuilder AddStoreOcelotConfigurationInConsul(); - IOcelotBuilder AddCacheManager(Action settings); - IOcelotAdministrationBuilder AddAdministration(string path, string secret); IOcelotAdministrationBuilder AddAdministration(string path, Action configOptions); diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 6d4873d5..727241a0 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -1,6 +1,5 @@ namespace Ocelot.DependencyInjection { - using CacheManager.Core; using IdentityServer4.Models; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; @@ -50,118 +49,110 @@ namespace Ocelot.DependencyInjection public class OcelotBuilder : IOcelotBuilder { - private readonly IServiceCollection _services; - private readonly IConfiguration _configurationRoot; - - public IServiceCollection Services => _services; - - public IConfiguration Configuration => _configurationRoot; + public IServiceCollection Services { get; } + public IConfiguration Configuration { get; } public OcelotBuilder(IServiceCollection services, IConfiguration configurationRoot) { - _configurationRoot = configurationRoot; - _services = services; - - //add default cache settings... - Action defaultCachingSettings = x => - { - x.WithDictionaryHandle(); - }; + Configuration = configurationRoot; + Services = services; - AddCacheManager(defaultCachingSettings); + Services.Configure(configurationRoot); + + //default no caches... + Services.TryAddSingleton, NoCache>(); + Services.TryAddSingleton, NoCache>(); - //add ocelot services... - _services.Configure(configurationRoot); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.AddSingleton(); - _services.AddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.AddSingleton(); + Services.AddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); if (UsingEurekaServiceDiscoveryProvider(configurationRoot)) { - _services.AddDiscoveryClient(configurationRoot); + Services.AddDiscoveryClient(configurationRoot); } else { - _services.TryAddSingleton(); + Services.TryAddSingleton(); } - _services.TryAddSingleton(); + Services.TryAddSingleton(); // 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 - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.AddMemoryCache(); - _services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.AddMemoryCache(); + Services.TryAddSingleton(); //add asp.net services.. var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly; - _services.AddMvcCore() + Services.AddMvcCore() .AddApplicationPart(assembly) .AddControllersAsServices() .AddAuthorization() .AddJsonFormatters(); - _services.AddLogging(); - _services.AddMiddlewareAnalysis(); - _services.AddWebEncoders(); + Services.AddLogging(); + Services.AddMiddlewareAnalysis(); + Services.AddWebEncoders(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.AddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.AddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); } public IOcelotAdministrationBuilder AddAdministration(string path, string secret) @@ -176,8 +167,8 @@ namespace Ocelot.DependencyInjection AddIdentityServer(identityServerConfiguration, administrationPath); } - _services.AddSingleton(administrationPath); - return new OcelotAdministrationBuilder(_services, _configurationRoot); + Services.AddSingleton(administrationPath); + return new OcelotAdministrationBuilder(Services, Configuration); } public IOcelotAdministrationBuilder AddAdministration(string path, Action configureOptions) @@ -189,21 +180,21 @@ namespace Ocelot.DependencyInjection AddIdentityServer(configureOptions); } - _services.AddSingleton(administrationPath); - return new OcelotAdministrationBuilder(_services, _configurationRoot); + Services.AddSingleton(administrationPath); + return new OcelotAdministrationBuilder(Services, Configuration); } public IOcelotBuilder AddSingletonDefinedAggregator() where T : class, IDefinedAggregator { - _services.AddSingleton(); + Services.AddSingleton(); return this; } public IOcelotBuilder AddTransientDefinedAggregator() where T : class, IDefinedAggregator { - _services.AddTransient(); + Services.AddTransient(); return this; } @@ -212,15 +203,15 @@ namespace Ocelot.DependencyInjection { if(global) { - _services.AddTransient(); - _services.AddTransient(s => { + Services.AddTransient(); + Services.AddTransient(s => { var service = s.GetService(); return new GlobalDelegatingHandler(service); }); } else { - _services.AddTransient(); + Services.AddTransient(); } return this; @@ -228,59 +219,33 @@ namespace Ocelot.DependencyInjection public IOcelotBuilder AddStoreOcelotConfigurationInConsul() { - _services.AddHostedService(); - _services.AddSingleton(); - return this; - } - - public IOcelotBuilder AddCacheManager(Action settings) - { - var cacheManagerOutputCache = CacheFactory.Build("OcelotOutputCache", settings); - var ocelotOutputCacheManager = new OcelotCacheManagerCache(cacheManagerOutputCache); - - _services.RemoveAll(typeof(ICacheManager)); - _services.RemoveAll(typeof(IOcelotCache)); - _services.AddSingleton>(cacheManagerOutputCache); - _services.AddSingleton>(ocelotOutputCacheManager); - - var ocelotConfigCacheManagerOutputCache = CacheFactory.Build("OcelotConfigurationCache", settings); - var ocelotConfigCacheManager = new OcelotCacheManagerCache(ocelotConfigCacheManagerOutputCache); - _services.RemoveAll(typeof(ICacheManager)); - _services.RemoveAll(typeof(IOcelotCache)); - _services.AddSingleton>(ocelotConfigCacheManagerOutputCache); - _services.AddSingleton>(ocelotConfigCacheManager); - - var fileConfigCacheManagerOutputCache = CacheFactory.Build("FileConfigurationCache", settings); - var fileConfigCacheManager = new OcelotCacheManagerCache(fileConfigCacheManagerOutputCache); - _services.RemoveAll(typeof(ICacheManager)); - _services.RemoveAll(typeof(IOcelotCache)); - _services.AddSingleton>(fileConfigCacheManagerOutputCache); - _services.AddSingleton>(fileConfigCacheManager); + Services.AddHostedService(); + Services.AddSingleton(); return this; } private void AddIdentityServer(Action configOptions) { - _services + Services .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(configOptions); } private void AddIdentityServer(IIdentityServerConfiguration identityServerConfiguration, IAdministrationPath adminPath) { - _services.TryAddSingleton(identityServerConfiguration); - var identityServerBuilder = _services + Services.TryAddSingleton(identityServerConfiguration); + var identityServerBuilder = Services .AddIdentityServer(o => { o.IssuerUri = "Ocelot"; }) .AddInMemoryApiResources(Resources(identityServerConfiguration)) .AddInMemoryClients(Client(identityServerConfiguration)); - var urlFinder = new BaseUrlFinder(_configurationRoot); + var urlFinder = new BaseUrlFinder(Configuration); var baseSchemeUrlAndPort = urlFinder.Find(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); - _services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) + Services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(o => { o.Authority = baseSchemeUrlAndPort + adminPath.Path; diff --git a/src/Ocelot/Ocelot.csproj b/src/Ocelot/Ocelot.csproj index 30e6771d..e4aaee61 100644 --- a/src/Ocelot/Ocelot.csproj +++ b/src/Ocelot/Ocelot.csproj @@ -45,9 +45,6 @@ all - - - diff --git a/test/Ocelot.AcceptanceTests/Caching/InMemoryJsonHandle.cs b/test/Ocelot.AcceptanceTests/Caching/InMemoryJsonHandle.cs deleted file mode 100644 index 3f160a7e..00000000 --- a/test/Ocelot.AcceptanceTests/Caching/InMemoryJsonHandle.cs +++ /dev/null @@ -1,137 +0,0 @@ -using CacheManager.Core; -using CacheManager.Core.Internal; -using CacheManager.Core.Logging; -using System; -using System.Collections.Concurrent; -using System.Linq; -using static CacheManager.Core.Utility.Guard; - -namespace Ocelot.AcceptanceTests.Caching -{ - public class InMemoryJsonHandle : BaseCacheHandle - { - private readonly ICacheSerializer _serializer; - private readonly ConcurrentDictionary> _cache; - - public InMemoryJsonHandle( - ICacheManagerConfiguration managerConfiguration, - CacheHandleConfiguration configuration, - ICacheSerializer serializer, - ILoggerFactory loggerFactory) : base(managerConfiguration, configuration) - { - _cache = new ConcurrentDictionary>(); - _serializer = serializer; - Logger = loggerFactory.CreateLogger(this); - } - - public override int Count => _cache.Count; - - protected override ILogger Logger { get; } - - public override void Clear() => _cache.Clear(); - - public override void ClearRegion(string region) - { - NotNullOrWhiteSpace(region, nameof(region)); - - var key = string.Concat(region, ":"); - foreach (var item in _cache.Where(p => p.Key.StartsWith(key, StringComparison.OrdinalIgnoreCase))) - { - _cache.TryRemove(item.Key, out Tuple val); - } - } - - public override bool Exists(string key) - { - NotNullOrWhiteSpace(key, nameof(key)); - - return _cache.ContainsKey(key); - } - - public override bool Exists(string key, string region) - { - NotNullOrWhiteSpace(region, nameof(region)); - var fullKey = GetKey(key, region); - return _cache.ContainsKey(fullKey); - } - - protected override bool AddInternalPrepared(CacheItem item) - { - NotNull(item, nameof(item)); - - var key = GetKey(item.Key, item.Region); - - var serializedItem = _serializer.SerializeCacheItem(item); - - return _cache.TryAdd(key, new Tuple(item.Value.GetType(), serializedItem)); - } - - protected override CacheItem GetCacheItemInternal(string key) => GetCacheItemInternal(key, null); - - protected override CacheItem GetCacheItemInternal(string key, string region) - { - var fullKey = GetKey(key, region); - - CacheItem deserializedResult = null; - - if (_cache.TryGetValue(fullKey, out Tuple result)) - { - deserializedResult = _serializer.DeserializeCacheItem(result.Item2, result.Item1); - - if (deserializedResult.ExpirationMode != ExpirationMode.None && IsExpired(deserializedResult, DateTime.UtcNow)) - { - _cache.TryRemove(fullKey, out Tuple removeResult); - TriggerCacheSpecificRemove(key, region, CacheItemRemovedReason.Expired, deserializedResult.Value); - return null; - } - } - - return deserializedResult; - } - - protected override void PutInternalPrepared(CacheItem item) - { - NotNull(item, nameof(item)); - - var serializedItem = _serializer.SerializeCacheItem(item); - - _cache[GetKey(item.Key, item.Region)] = new Tuple(item.Value.GetType(), serializedItem); - } - - protected override bool RemoveInternal(string key) => RemoveInternal(key, null); - - protected override bool RemoveInternal(string key, string region) - { - var fullKey = GetKey(key, region); - return _cache.TryRemove(fullKey, out Tuple val); - } - - private static string GetKey(string key, string region) - { - NotNullOrWhiteSpace(key, nameof(key)); - - if (string.IsNullOrWhiteSpace(region)) - { - return key; - } - - return string.Concat(region, ":", key); - } - - private static bool IsExpired(CacheItem item, DateTime now) - { - if (item.ExpirationMode == ExpirationMode.Absolute - && item.CreatedUtc.Add(item.ExpirationTimeout) < now) - { - return true; - } - else if (item.ExpirationMode == ExpirationMode.Sliding - && item.LastAccessedUtc.Add(item.ExpirationTimeout) < now) - { - return true; - } - - return false; - } - } -} diff --git a/test/Ocelot.AcceptanceTests/CachingTests.cs b/test/Ocelot.AcceptanceTests/CachingTests.cs deleted file mode 100644 index bd8f0882..00000000 --- a/test/Ocelot.AcceptanceTests/CachingTests.cs +++ /dev/null @@ -1,225 +0,0 @@ -namespace Ocelot.AcceptanceTests -{ - using System; - using System.Collections.Generic; - using System.Net; - using System.Threading; - using Microsoft.AspNetCore.Http; - using Ocelot.Configuration.File; - using TestStack.BDDfy; - using Xunit; - - public class CachingTests : IDisposable - { - private readonly Steps _steps; - private readonly ServiceHandler _serviceHandler; - - public CachingTests() - { - _serviceHandler = new ServiceHandler(); - _steps = new Steps(); - } - - [Fact] - public void should_return_cached_response() - { - var configuration = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamPathTemplate = "/", - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 51899, - } - }, - DownstreamScheme = "http", - UpstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "Get" }, - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 100 - } - } - } - }; - - this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51899", 200, "Hello from Laura", null, null)) - .And(x => _steps.GivenThereIsAConfiguration(configuration)) - .And(x => _steps.GivenOcelotIsRunning()) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .Given(x => x.GivenTheServiceNowReturns("http://localhost:51899", 200, "Hello from Tom")) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .And(x => _steps.ThenTheContentLengthIs(16)) - .BDDfy(); - } - - [Fact] - public void should_return_cached_response_with_expires_header() - { - var configuration = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamPathTemplate = "/", - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 52839, - } - }, - DownstreamScheme = "http", - UpstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "Get" }, - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 100 - } - } - } - }; - - this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:52839", 200, "Hello from Laura", "Expires", "-1")) - .And(x => _steps.GivenThereIsAConfiguration(configuration)) - .And(x => _steps.GivenOcelotIsRunning()) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .Given(x => x.GivenTheServiceNowReturns("http://localhost:52839", 200, "Hello from Tom")) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .And(x => _steps.ThenTheContentLengthIs(16)) - .And(x => _steps.ThenTheResponseBodyHeaderIs("Expires", "-1")) - .BDDfy(); - } - - [Fact] - public void should_return_cached_response_when_using_jsonserialized_cache() - { - var configuration = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamPathTemplate = "/", - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 51899, - } - }, - DownstreamScheme = "http", - UpstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "Get" }, - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 100 - } - } - } - }; - - this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51899", 200, "Hello from Laura", null, null)) - .And(x => _steps.GivenThereIsAConfiguration(configuration)) - .And(x => _steps.GivenOcelotIsRunningUsingJsonSerializedCache()) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .Given(x => x.GivenTheServiceNowReturns("http://localhost:51899", 200, "Hello from Tom")) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .BDDfy(); - } - - [Fact] - public void should_not_return_cached_response_as_ttl_expires() - { - var configuration = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamPathTemplate = "/", - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 51899, - } - }, - DownstreamScheme = "http", - UpstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "Get" }, - FileCacheOptions = new FileCacheOptions - { - TtlSeconds = 1 - } - } - } - }; - - this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51899", 200, "Hello from Laura", null, null)) - .And(x => _steps.GivenThereIsAConfiguration(configuration)) - .And(x => _steps.GivenOcelotIsRunning()) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .Given(x => x.GivenTheServiceNowReturns("http://localhost:51899", 200, "Hello from Tom")) - .And(x => x.GivenTheCacheExpires()) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Tom")) - .BDDfy(); - } - - private void GivenTheCacheExpires() - { - Thread.Sleep(1000); - } - - private void GivenTheServiceNowReturns(string url, int statusCode, string responseBody) - { - _serviceHandler.Dispose(); - GivenThereIsAServiceRunningOn(url, statusCode, responseBody, null, null); - } - - private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, string key, string value) - { - _serviceHandler.GivenThereIsAServiceRunningOn(url, async context => - { - if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(key)) - { - context.Response.Headers.Add(key, value); - } - context.Response.StatusCode = statusCode; - await context.Response.WriteAsync(responseBody); - }); - } - - public void Dispose() - { - _serviceHandler?.Dispose(); - _steps.Dispose(); - } - } -} diff --git a/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs b/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs index 23ce3d11..e44f7496 100644 --- a/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs +++ b/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs @@ -17,6 +17,8 @@ using static Ocelot.Infrastructure.Wait; namespace Ocelot.AcceptanceTests { + using Cache; + public class ConfigurationInConsulTests : IDisposable { private IWebHost _builder; @@ -76,51 +78,6 @@ namespace Ocelot.AcceptanceTests .BDDfy(); } - [Fact] - public void should_return_response_200_with_simple_url_when_using_jsonserialized_cache() - { - var configuration = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamPathTemplate = "/", - DownstreamScheme = "http", - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "localhost", - Port = 51779, - } - }, - UpstreamPathTemplate = "/", - UpstreamHttpMethod = new List { "Get" }, - } - }, - GlobalConfiguration = new FileGlobalConfiguration() - { - ServiceDiscoveryProvider = new FileServiceDiscoveryProvider() - { - Host = "localhost", - Port = 9502 - } - } - }; - - var fakeConsulServiceDiscoveryUrl = "http://localhost:9502"; - - this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, "")) - .And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "", 200, "Hello from Laura")) - .And(x => _steps.GivenThereIsAConfiguration(configuration)) - .And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache()) - .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) - .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) - .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) - .BDDfy(); - } - [Fact] public void should_load_configuration_out_of_consul() { @@ -485,5 +442,28 @@ namespace Ocelot.AcceptanceTests _builder?.Dispose(); _steps.Dispose(); } + + class FakeCache : IOcelotCache + { + public void Add(string key, FileConfiguration value, TimeSpan ttl, string region) + { + throw new NotImplementedException(); + } + + public void AddAndDelete(string key, FileConfiguration value, TimeSpan ttl, string region) + { + throw new NotImplementedException(); + } + + public FileConfiguration Get(string key, string region) + { + throw new NotImplementedException(); + } + + public void ClearRegion(string region) + { + throw new NotImplementedException(); + } + } } } diff --git a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj index c4e902fd..02082c9f 100644 --- a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj +++ b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj @@ -32,7 +32,6 @@ - diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index 63d3a540..c67c953b 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -1,40 +1,35 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading; -using System.Threading.Tasks; -using CacheManager.Core; -using IdentityServer4.AccessTokenValidation; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.TestHost; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using Ocelot.Configuration.File; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; -using Shouldly; -using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; -using Ocelot.AcceptanceTests.Caching; -using System.IO.Compression; -using System.Text; -using static Ocelot.AcceptanceTests.HttpDelegatingHandlersTests; -using Ocelot.Requester; -using Ocelot.Middleware.Multiplexer; -using static Ocelot.Infrastructure.Wait; - -namespace Ocelot.AcceptanceTests +namespace Ocelot.AcceptanceTests { - using Butterfly; + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Net.Http.Headers; + using System.Threading; + using System.Threading.Tasks; + using IdentityServer4.AccessTokenValidation; + using Microsoft.AspNetCore.Builder; + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.TestHost; + using Microsoft.Extensions.Configuration; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Logging; + using Newtonsoft.Json; + using Ocelot.Configuration.File; + using Ocelot.DependencyInjection; + using Ocelot.Middleware; + using Shouldly; + using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; + using System.IO.Compression; + using System.Text; + using static Ocelot.AcceptanceTests.HttpDelegatingHandlersTests; + using Ocelot.Middleware.Multiplexer; + using static Ocelot.Infrastructure.Wait; using Configuration.Repository; - using Microsoft.Net.Http.Headers; using Ocelot.Configuration.Creator; + using CookieHeaderValue = System.Net.Http.Headers.CookieHeaderValue; using MediaTypeHeaderValue = System.Net.Http.Headers.MediaTypeHeaderValue; public class Steps : IDisposable @@ -44,7 +39,6 @@ namespace Ocelot.AcceptanceTests private HttpResponseMessage _response; private HttpContent _postContent; private BearerToken _token; - public HttpClient OcelotClient => _ocelotClient; public string RequestIdKey = "OcRequestId"; private readonly Random _random; private IWebHostBuilder _webHostBuilder; @@ -428,55 +422,6 @@ namespace Ocelot.AcceptanceTests header.First().ShouldBe(value); } - public void ThenTheResponseBodyHeaderIs(string key, string value) - { - var header = _response.Content.Headers.GetValues(key); - header.First().ShouldBe(value); - } - - public void ThenTheTraceHeaderIsSet(string key) - { - var header = _response.Headers.GetValues(key); - header.First().ShouldNotBeNullOrEmpty(); - } - - public void GivenOcelotIsRunningUsingJsonSerializedCache() - { - _webHostBuilder = new WebHostBuilder(); - - _webHostBuilder - .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(s => - { - s.AddOcelot() - .AddCacheManager((x) => - { - x.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithJsonSerializer() - .WithHandle(typeof(InMemoryJsonHandle<>)); - }); - }) - .Configure(app => - { - app.UseOcelot().Wait(); - }); - - _ocelotServer = new TestServer(_webHostBuilder); - - _ocelotClient = _ocelotServer.CreateClient(); - } - public void GivenOcelotIsRunningUsingConsulToStoreConfig() { _webHostBuilder = new WebHostBuilder(); @@ -505,69 +450,6 @@ namespace Ocelot.AcceptanceTests _ocelotClient = _ocelotServer.CreateClient(); } - public void GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache() - { - _webHostBuilder = new WebHostBuilder(); - - _webHostBuilder - .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", optional: true, reloadOnChange: false); - config.AddEnvironmentVariables(); - }) - .ConfigureServices(s => - { - s.AddOcelot() - .AddCacheManager((x) => - { - x.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithJsonSerializer() - .WithHandle(typeof(InMemoryJsonHandle<>)); - }) - .AddStoreOcelotConfigurationInConsul(); - }) - .Configure(app => - { - app.UseOcelot().Wait(); - }); - - _ocelotServer = new TestServer(_webHostBuilder); - - _ocelotClient = _ocelotServer.CreateClient(); - } - - internal 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); - } - } - /// /// 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. /// @@ -590,15 +472,6 @@ namespace Ocelot.AcceptanceTests .UseConfiguration(configuration) .ConfigureServices(s => { - Action settings = (x) => - { - x.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithDictionaryHandle(); - }; - s.AddOcelot(configuration); }) .ConfigureLogging(l => @@ -688,26 +561,6 @@ namespace Ocelot.AcceptanceTests } } - public 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("username", "admin"), - new KeyValuePair("password", "admin"), - new KeyValuePair("grant_type", "password") - }; - var content = new FormUrlEncodedContent(formData); - - var response = _ocelotClient.PostAsync(tokenUrl, content).Result; - var responseContent = response.Content.ReadAsStringAsync().Result; - response.EnsureSuccessStatusCode(); - _token = JsonConvert.DeserializeObject(responseContent); - } - public void VerifyIdentiryServerStarted(string url) { using (var httpClient = new HttpClient()) @@ -883,11 +736,6 @@ namespace Ocelot.AcceptanceTests _response.Headers.GetValues(RequestIdKey).First().ShouldBe(expected); } - public void ThenTheContentLengthIs(int expected) - { - _response.Content.Headers.ContentLength.ShouldBe(expected); - } - public void WhenIMakeLotsOfDifferentRequestsToTheApiGateway() { int numberOfRequests = 100; diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index cf382b63..f3835b0c 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -5,7 +5,6 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Claims; -using CacheManager.Core; using IdentityServer4.AccessTokenValidation; using IdentityServer4.Models; using IdentityServer4.Test; @@ -557,17 +556,7 @@ namespace Ocelot.IntegrationTests }) .ConfigureServices(x => { - Action settings = (s) => - { - s.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithDictionaryHandle(); - }; - x.AddOcelot() - .AddCacheManager(settings) .AddAdministration("/administration", "secret"); }) .Configure(app => @@ -682,10 +671,6 @@ namespace Ocelot.IntegrationTests .ConfigureServices(x => { x.AddSingleton(_webHostBuilder); x.AddOcelot() - .AddCacheManager(c => - { - c.WithDictionaryHandle(); - }) .AddAdministration("/administration", configOptions); }) .Configure(app => { @@ -714,17 +699,7 @@ namespace Ocelot.IntegrationTests }) .ConfigureServices(x => { - Action settings = (s) => - { - s.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithDictionaryHandle(); - }; - x.AddOcelot() - .AddCacheManager(settings) .AddAdministration("/administration", "secret"); }) .Configure(app => @@ -755,10 +730,6 @@ namespace Ocelot.IntegrationTests .ConfigureServices(x => { x.AddSingleton(_webHostBuilder); x.AddOcelot() - .AddCacheManager(c => - { - c.WithDictionaryHandle(); - }) .AddAdministration("/administration", "secret"); }) .Configure(app => { diff --git a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs index 61e9c17f..a1a29f52 100644 --- a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs +++ b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs @@ -4,7 +4,6 @@ using System.IO; using System.Net.Http; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Ocelot.Configuration.File; using Shouldly; @@ -13,7 +12,6 @@ using Xunit; using Microsoft.AspNetCore.Http; using System.Threading.Tasks; using System.Collections.Concurrent; -using CacheManager.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Ocelot.DependencyInjection; @@ -113,17 +111,7 @@ namespace Ocelot.IntegrationTests }) .ConfigureServices(x => { - Action settings = (s) => - { - s.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithDictionaryHandle(); - }; - x.AddOcelot() - .AddCacheManager(settings) .AddAdministration("/administration", "secret"); }) .Configure(app => diff --git a/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs b/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs index e287d5f4..1be11401 100644 --- a/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs @@ -1,5 +1,6 @@ -using Ocelot.Configuration; -using Ocelot.Middleware; +using Xunit; + +[assembly: CollectionBehavior(DisableTestParallelization = true)] namespace Ocelot.UnitTests.Authentication { @@ -15,14 +16,16 @@ namespace Ocelot.UnitTests.Authentication using Shouldly; using TestStack.BDDfy; using Xunit; + using Ocelot.Configuration; + using Ocelot.Middleware; public class AuthenticationMiddlewareTests { private AuthenticationMiddleware _middleware; - private Mock _factory; + private readonly Mock _factory; private Mock _logger; private OcelotRequestDelegate _next; - private DownstreamContext _downstreamContext; + private readonly DownstreamContext _downstreamContext; public AuthenticationMiddlewareTests() { diff --git a/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs b/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs deleted file mode 100644 index 4b891d73..00000000 --- a/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using CacheManager.Core; -using Moq; -using Ocelot.Cache; -using Shouldly; -using TestStack.BDDfy; -using Xunit; - -namespace Ocelot.UnitTests.Cache -{ - public class CacheManagerCacheTests - { - private OcelotCacheManagerCache _ocelotOcelotCacheManager; - private Mock> _mockCacheManager; - private string _key; - private string _value; - private string _resultGet; - private TimeSpan _ttlSeconds; - private string _region; - - public CacheManagerCacheTests() - { - _mockCacheManager = new Mock>(); - _ocelotOcelotCacheManager = new OcelotCacheManagerCache(_mockCacheManager.Object); - } - - [Fact] - public void should_get_from_cache() - { - this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue")) - .When(x => x.WhenIGetFromTheCache()) - .Then(x => x.ThenTheResultIs("someValue")) - .BDDfy(); - } - - [Fact] - public void should_add_to_cache() - { - this.When(x => x.WhenIAddToTheCache("someKey", "someValue", TimeSpan.FromSeconds(1))) - .Then(x => x.ThenTheCacheIsCalledCorrectly()) - .BDDfy(); - } - - [Fact] - public void should_delete_key_from_cache() - { - this.Given(_ => GivenTheFollowingRegion("fookey")) - .When(_ => WhenIDeleteTheRegion("fookey")) - .Then(_ => ThenTheRegionIsDeleted("fookey")) - .BDDfy(); - } - - private void WhenIDeleteTheRegion(string region) - { - _ocelotOcelotCacheManager.ClearRegion(region); - } - - private void ThenTheRegionIsDeleted(string region) - { - _mockCacheManager - .Verify(x => x.ClearRegion(region), Times.Once); - } - - private void GivenTheFollowingRegion(string key) - { - _ocelotOcelotCacheManager.Add(key, "doesnt matter", TimeSpan.FromSeconds(10), "region"); - } - - private void WhenIAddToTheCache(string key, string value, TimeSpan ttlSeconds) - { - _key = key; - _value = value; - _ttlSeconds = ttlSeconds; - _ocelotOcelotCacheManager.Add(_key, _value, _ttlSeconds, "region"); - } - - private void ThenTheCacheIsCalledCorrectly() - { - _mockCacheManager - .Verify(x => x.Add(It.IsAny>()), Times.Once); - } - - private void ThenTheResultIs(string expected) - { - _resultGet.ShouldBe(expected); - } - - private void WhenIGetFromTheCache() - { - _resultGet = _ocelotOcelotCacheManager.Get(_key, _region); - } - - private void GivenTheFollowingIsCached(string key, string region, string value) - { - _key = key; - _value = value; - _region = region; - _mockCacheManager - .Setup(x => x.Get(It.IsAny(), It.IsAny())) - .Returns(value); - } - } -} diff --git a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs deleted file mode 100644 index b3bbface..00000000 --- a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs +++ /dev/null @@ -1,95 +0,0 @@ -namespace Ocelot.UnitTests.Cache -{ - using System.Linq; - using System.Net; - using System.Net.Http.Headers; - using CacheManager.Core; - using Shouldly; - using System.Collections.Generic; - using System.Net.Http; - using System.Threading.Tasks; - using Moq; - using Ocelot.Cache; - using Ocelot.Cache.Middleware; - using Ocelot.Configuration; - using Ocelot.Configuration.Builder; - using Ocelot.Logging; - using Ocelot.Middleware; - using TestStack.BDDfy; - using Xunit; - using Microsoft.AspNetCore.Http; - using Ocelot.Middleware.Multiplexer; - - public class OutputCacheMiddlewareRealCacheTests - { - private readonly IOcelotCache _cacheManager; - private readonly OutputCacheMiddleware _middleware; - private readonly DownstreamContext _downstreamContext; - private OcelotRequestDelegate _next; - private Mock _loggerFactory; - private IRegionCreator _regionCreator; - private Mock _logger; - - public OutputCacheMiddlewareRealCacheTests() - { - _loggerFactory = new Mock(); - _logger = new Mock(); - _loggerFactory.Setup(x => x.CreateLogger()).Returns(_logger.Object); - _regionCreator = new RegionCreator(); - var cacheManagerOutputCache = CacheFactory.Build("OcelotOutputCache", x => - { - x.WithDictionaryHandle(); - }); - _cacheManager = new OcelotCacheManagerCache(cacheManagerOutputCache); - _downstreamContext = new DownstreamContext(new DefaultHttpContext()); - _downstreamContext.DownstreamRequest = new Ocelot.Request.Middleware.DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123")); - _next = context => Task.CompletedTask; - _middleware = new OutputCacheMiddleware(_next, _loggerFactory.Object, _cacheManager, _regionCreator); - } - - [Fact] - public void should_cache_content_headers() - { - var content = new StringContent("{\"Test\": 1}") - { - Headers = { ContentType = new MediaTypeHeaderValue("application/json")} - }; - - var response = new DownstreamResponse(content, HttpStatusCode.OK, new List>>()); - - this.Given(x => x.GivenResponseIsNotCached(response)) - .And(x => x.GivenTheDownstreamRouteIs()) - .When(x => x.WhenICallTheMiddleware()) - .Then(x => x.ThenTheContentTypeHeaderIsCached()) - .BDDfy(); - } - - private void WhenICallTheMiddleware() - { - _middleware.Invoke(_downstreamContext).GetAwaiter().GetResult(); - } - - private void ThenTheContentTypeHeaderIsCached() - { - var result = _cacheManager.Get("GET-https://some.url/blah?abcd=123", "kanken"); - var header = result.ContentHeaders["Content-Type"]; - header.First().ShouldBe("application/json"); - } - - private void GivenResponseIsNotCached(DownstreamResponse response) - { - _downstreamContext.DownstreamResponse = response; - } - - private void GivenTheDownstreamRouteIs() - { - var reRoute = new DownstreamReRouteBuilder() - .WithIsCached(true) - .WithCacheOptions(new CacheOptions(100, "kanken")) - .WithUpstreamHttpMethod(new List { "Get" }) - .Build(); - - _downstreamContext.DownstreamReRoute = reRoute; - } - } -} diff --git a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs index c0b59350..d13b22f2 100644 --- a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs @@ -16,24 +16,22 @@ using TestStack.BDDfy; using Xunit; using System.Net; + using Microsoft.Extensions.DependencyInjection; using Ocelot.Middleware; - using Ocelot.Middleware.Multiplexer; public class OutputCacheMiddlewareTests { - private readonly Mock> _cacheManager; + private readonly Mock> _cache; private readonly Mock _loggerFactory; private Mock _logger; private OutputCacheMiddleware _middleware; private readonly DownstreamContext _downstreamContext; private readonly OcelotRequestDelegate _next; private CachedResponse _response; - private readonly IRegionCreator _regionCreator; public OutputCacheMiddlewareTests() { - _cacheManager = new Mock>(); - _regionCreator = new RegionCreator(); + _cache = new Mock>(); _downstreamContext = new DownstreamContext(new DefaultHttpContext()); _loggerFactory = new Mock(); _logger = new Mock(); @@ -91,14 +89,14 @@ private void WhenICallTheMiddleware() { - _middleware = new OutputCacheMiddleware(_next, _loggerFactory.Object, _cacheManager.Object, _regionCreator); + _middleware = new OutputCacheMiddleware(_next, _loggerFactory.Object, _cache.Object); _middleware.Invoke(_downstreamContext).GetAwaiter().GetResult(); } private void GivenThereIsACachedResponse(CachedResponse response) { _response = response; - _cacheManager + _cache .Setup(x => x.Get(It.IsAny(), It.IsAny())) .Returns(_response); } @@ -127,13 +125,13 @@ private void ThenTheCacheGetIsCalledCorrectly() { - _cacheManager + _cache .Verify(x => x.Get(It.IsAny(), It.IsAny()), Times.Once); } private void ThenTheCacheAddIsCalledCorrectly() { - _cacheManager + _cache .Verify(x => x.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } } diff --git a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs index 368124d3..9b80757c 100644 --- a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs +++ b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; -using CacheManager.Core; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.Extensions.Configuration; @@ -81,16 +80,6 @@ namespace Ocelot.UnitTests.DependencyInjection .BDDfy(); } - [Fact] - public void should_set_up_cache_manager() - { - this.Given(x => WhenISetUpOcelotServices()) - .When(x => WhenISetUpCacheManager()) - .Then(x => ThenAnExceptionIsntThrown()) - .And(x => OnlyOneVersionOfEachCacheIsRegistered()) - .BDDfy(); - } - [Fact] public void should_set_up_consul() { @@ -282,24 +271,6 @@ namespace Ocelot.UnitTests.DependencyInjection first.ShouldBe(second); } - private void OnlyOneVersionOfEachCacheIsRegistered() - { - var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); - var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager)); - var instance = (ICacheManager)outputCacheManager.ImplementationInstance; - var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); - var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager)); - var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); - var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager)); - - instance.Configuration.MaxRetries.ShouldBe(_maxRetries); - outputCache.ShouldNotBeNull(); - ocelotConfigCache.ShouldNotBeNull(); - ocelotConfigCacheManager.ShouldNotBeNull(); - fileConfigCache.ShouldNotBeNull(); - fileConfigCacheManager.ShouldNotBeNull(); - } - private void WhenISetUpConsul() { try @@ -365,21 +336,6 @@ namespace Ocelot.UnitTests.DependencyInjection } } - private void WhenISetUpCacheManager() - { - try - { - _ocelotBuilder.AddCacheManager(x => { - x.WithMaxRetries(_maxRetries); - x.WithDictionaryHandle(); - }); - } - catch (Exception e) - { - _ex = e; - } - } - private void WhenIAccessLoggerFactory() { try diff --git a/test/Ocelot.UnitTests/Middleware/OcelotPipelineExtensionsTests.cs b/test/Ocelot.UnitTests/Middleware/OcelotPipelineExtensionsTests.cs index fd6fd8bf..0de6cae2 100644 --- a/test/Ocelot.UnitTests/Middleware/OcelotPipelineExtensionsTests.cs +++ b/test/Ocelot.UnitTests/Middleware/OcelotPipelineExtensionsTests.cs @@ -1,7 +1,9 @@ namespace Ocelot.UnitTests.Middleware { + using System; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; + using Ocelot.Cache; using Ocelot.DependencyInjection; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamUrlCreator.Middleware; @@ -40,7 +42,6 @@ namespace Ocelot.UnitTests.Middleware .BDDfy(); } - private void ThenThePipelineIsBuilt() { _handlers.ShouldNotBeNull(); @@ -67,7 +68,6 @@ namespace Ocelot.UnitTests.Middleware _handlers = _builder.BuildOcelotPipeline(new OcelotPipelineConfiguration()); } - private void GivenTheDepedenciesAreSetUp() { IConfigurationBuilder test = new ConfigurationBuilder();