diff --git a/src/Ocelot/Cache/IOcelotCache.cs b/src/Ocelot/Cache/IOcelotCache.cs index 9d7ead9b1..9abf57610 100644 --- a/src/Ocelot/Cache/IOcelotCache.cs +++ b/src/Ocelot/Cache/IOcelotCache.cs @@ -7,7 +7,7 @@ public interface IOcelotCache { void Add(string key, T value, TimeSpan ttl, string region); void AddAndDelete(string key, T value, TimeSpan ttl, string region); - T Get(string key); + T Get(string key, string region); void ClearRegion(string region); } } diff --git a/src/Ocelot/Cache/IRegionCreator.cs b/src/Ocelot/Cache/IRegionCreator.cs new file mode 100644 index 000000000..8ed186dd2 --- /dev/null +++ b/src/Ocelot/Cache/IRegionCreator.cs @@ -0,0 +1,9 @@ +using Ocelot.Configuration.File; + +namespace Ocelot.Cache +{ + public interface IRegionCreator + { + string Create(FileReRoute reRoute); + } +} \ No newline at end of file diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 8b8b565da..9c6dcf328 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -37,11 +37,11 @@ public async Task Invoke(HttpContext context) return; } - var downstreamUrlKey = DownstreamRequest.RequestUri.OriginalString; + var downstreamUrlKey = $"{DownstreamRequest.Method.Method}-{DownstreamRequest.RequestUri.OriginalString}"; _logger.LogDebug("started checking cache for {downstreamUrlKey}", downstreamUrlKey); - var cached = _outputCache.Get(downstreamUrlKey); + var cached = _outputCache.Get(downstreamUrlKey, DownstreamRoute.ReRoute.CacheOptions.Region); if (cached != null) { @@ -67,9 +67,7 @@ public async Task Invoke(HttpContext context) var response = HttpResponseMessage; - var region = _regionCreator.Region(DownstreamRoute.ReRoute); - - _outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds), region); + _outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.CacheOptions.TtlSeconds), DownstreamRoute.ReRoute.CacheOptions.Region); _logger.LogDebug("finished response added to cache for {downstreamUrlKey}", downstreamUrlKey); } diff --git a/src/Ocelot/Cache/OcelotCacheManagerCache.cs b/src/Ocelot/Cache/OcelotCacheManagerCache.cs index 4edfdaadd..33ee15436 100644 --- a/src/Ocelot/Cache/OcelotCacheManagerCache.cs +++ b/src/Ocelot/Cache/OcelotCacheManagerCache.cs @@ -8,18 +8,15 @@ namespace Ocelot.Cache public class OcelotCacheManagerCache : IOcelotCache { private readonly ICacheManager _cacheManager; - private HashSet _keys; public OcelotCacheManagerCache(ICacheManager cacheManager) { _cacheManager = cacheManager; - _keys = new HashSet(); } public void Add(string key, T value, TimeSpan ttl, string region) { _cacheManager.Add(new CacheItem(key, region, value, ExpirationMode.Absolute, ttl)); - _keys.Add(key); } public void AddAndDelete(string key, T value, TimeSpan ttl, string region) @@ -34,9 +31,9 @@ public void AddAndDelete(string key, T value, TimeSpan ttl, string region) Add(key, value, ttl, region); } - public T Get(string key) + public T Get(string key, string region) { - return _cacheManager.Get(key); + return _cacheManager.Get(key, region); } public void ClearRegion(string region) diff --git a/src/Ocelot/Cache/RegionBuilder.cs b/src/Ocelot/Cache/RegionCreator.cs similarity index 53% rename from src/Ocelot/Cache/RegionBuilder.cs rename to src/Ocelot/Cache/RegionCreator.cs index 5ccf89737..87de751cc 100644 --- a/src/Ocelot/Cache/RegionBuilder.cs +++ b/src/Ocelot/Cache/RegionCreator.cs @@ -1,20 +1,22 @@ using System.Linq; using Ocelot.Configuration; +using Ocelot.Configuration.File; namespace Ocelot.Cache { - public interface IRegionCreator - { - string Region(ReRoute reRoute); - } public class RegionCreator : IRegionCreator { - public string Region(ReRoute reRoute) + public string Create(FileReRoute reRoute) { - var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m.Method)); + if(!string.IsNullOrEmpty(reRoute?.FileCacheOptions?.Region)) + { + return reRoute?.FileCacheOptions?.Region; + } + + var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m)); - var region = $"{methods}{reRoute.UpstreamPathTemplate.Value.Replace("/", "")}"; + var region = $"{methods}{reRoute.UpstreamPathTemplate.Replace("/", "")}"; return region; } diff --git a/src/Ocelot/Cache/RegionsGetter.cs b/src/Ocelot/Cache/RegionsGetter.cs deleted file mode 100644 index ae87a49fb..000000000 --- a/src/Ocelot/Cache/RegionsGetter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Ocelot.Configuration.Provider; -using Ocelot.Logging; - -namespace Ocelot.Cache -{ - public interface IRegionsGetter - { - Task> Regions(); - } - public class RegionsGetter : IRegionsGetter - { - private readonly IOcelotConfigurationProvider _provider; - private readonly IRegionCreator _creator; - private readonly IOcelotLogger _logger; - - public RegionsGetter(IOcelotConfigurationProvider provider, IRegionCreator creator, IOcelotLoggerFactory loggerFactory) - { - _logger = loggerFactory.CreateLogger(); - _provider = provider; - _creator = creator; - } - - public async Task> Regions() - { - var config = await _provider.Get(); - - if(config.IsError) - { - _logger.LogError("unable to find regions", new Exception(string.Join(",", config.Errors))); - return new List(); - } - - var cachedReRoutes = config.Data.ReRoutes.Where(x => x.IsCached); - - var regions = new List(); - - foreach(var reRoute in cachedReRoutes) - { - var region = _creator.Region(reRoute); - regions.Add(region); - } - - return regions; - } - } -} \ No newline at end of file diff --git a/src/Ocelot/Configuration/CacheOptions.cs b/src/Ocelot/Configuration/CacheOptions.cs index 3de9e0193..a8b6a9d32 100644 --- a/src/Ocelot/Configuration/CacheOptions.cs +++ b/src/Ocelot/Configuration/CacheOptions.cs @@ -2,9 +2,10 @@ { public class CacheOptions { - public CacheOptions(int ttlSeconds) + public CacheOptions(int ttlSeconds, string region) { TtlSeconds = ttlSeconds; + Region = region; } public int TtlSeconds { get; private set; } diff --git a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs index c44edd08b..3b9c91d07 100644 --- a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Ocelot.Cache; using Ocelot.Configuration.Builder; using Ocelot.Configuration.File; using Ocelot.Configuration.Parser; @@ -36,6 +37,7 @@ public class FileOcelotConfigurationCreator : IOcelotConfigurationCreator private IQoSOptionsCreator _qosOptionsCreator; private IReRouteOptionsCreator _fileReRouteOptionsCreator; private IRateLimitOptionsCreator _rateLimitOptionsCreator; + private IRegionCreator _regionCreator; public FileOcelotConfigurationCreator( IOptions options, @@ -52,9 +54,11 @@ public FileOcelotConfigurationCreator( IServiceProviderConfigurationCreator serviceProviderConfigCreator, IQoSOptionsCreator qosOptionsCreator, IReRouteOptionsCreator fileReRouteOptionsCreator, - IRateLimitOptionsCreator rateLimitOptionsCreator + IRateLimitOptionsCreator rateLimitOptionsCreator, + IRegionCreator regionCreator ) { + _regionCreator = regionCreator; _rateLimitOptionsCreator = rateLimitOptionsCreator; _requestIdKeyCreator = requestIdKeyCreator; _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator; @@ -137,6 +141,8 @@ private async Task SetUpReRoute(FileReRoute fileReRoute, FileGlobalConf var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting); + var region = _regionCreator.Create(fileReRoute); + var reRoute = new ReRouteBuilder() .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate) .WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate) @@ -151,7 +157,7 @@ private async Task SetUpReRoute(FileReRoute fileReRoute, FileGlobalConf .WithClaimsToQueries(claimsToQueries) .WithRequestIdKey(requestIdKey) .WithIsCached(fileReRouteOptions.IsCached) - .WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds)) + .WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds, region)) .WithDownstreamScheme(fileReRoute.DownstreamScheme) .WithLoadBalancer(fileReRoute.LoadBalancer) .WithDownstreamHost(fileReRoute.DownstreamHost) diff --git a/src/Ocelot/Configuration/File/FileCacheOptions.cs b/src/Ocelot/Configuration/File/FileCacheOptions.cs index 46fc7b87e..df9fb631d 100644 --- a/src/Ocelot/Configuration/File/FileCacheOptions.cs +++ b/src/Ocelot/Configuration/File/FileCacheOptions.cs @@ -3,6 +3,6 @@ public class FileCacheOptions { public int TtlSeconds { get; set; } - public string Region {get;private set;} + public string Region {get; set;} } } diff --git a/src/Ocelot/Configuration/ReRoute.cs b/src/Ocelot/Configuration/ReRoute.cs index a6673d094..2b8734c42 100644 --- a/src/Ocelot/Configuration/ReRoute.cs +++ b/src/Ocelot/Configuration/ReRoute.cs @@ -46,7 +46,7 @@ public ReRoute(PathTemplate downstreamPathTemplate, IsAuthorised = isAuthorised; RequestIdKey = requestIdKey; IsCached = isCached; - FileCacheOptions = fileCacheOptions; + CacheOptions = fileCacheOptions; ClaimsToQueries = claimsToQueries ?? new List(); ClaimsToClaims = claimsToClaims @@ -74,7 +74,7 @@ public ReRoute(PathTemplate downstreamPathTemplate, public Dictionary RouteClaimsRequirement { get; private set; } public string RequestIdKey { get; private set; } public bool IsCached { get; private set; } - public CacheOptions FileCacheOptions { get; private set; } + public CacheOptions CacheOptions { get; private set; } public string DownstreamScheme {get;private set;} public bool IsQos { get; private set; } public QoSOptions QosOptionsOptions { get; private set; } diff --git a/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs index d7929aaa9..c54132742 100644 --- a/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs @@ -30,7 +30,7 @@ public ConsulOcelotConfigurationRepository(ConsulRegistryConfiguration consulReg public async Task> Get() { - var config = _cache.Get(_ocelotConfiguration); + var config = _cache.Get(_ocelotConfiguration, _ocelotConfiguration); if (config != null) { @@ -68,7 +68,7 @@ public async Task AddOrReplace(IOcelotConfiguration ocelotConfiguratio if (result.Response) { - _cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), "OcelotConfiguration"); + _cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), _ocelotConfiguration); return new OkResponse(); } diff --git a/src/Ocelot/Controllers/OutputCacheController.cs b/src/Ocelot/Controllers/OutputCacheController.cs index 21dccb8de..a68201a15 100644 --- a/src/Ocelot/Controllers/OutputCacheController.cs +++ b/src/Ocelot/Controllers/OutputCacheController.cs @@ -12,19 +12,10 @@ namespace Ocelot.Controllers public class OutputCacheController : Controller { private IOcelotCache _cache; - private IRegionsGetter _regionsGetter; - public OutputCacheController(IOcelotCache cache, IRegionsGetter regionsGetter) + public OutputCacheController(IOcelotCache cache) { _cache = cache; - _regionsGetter = regionsGetter; - } - - [HttpGet] - public async Task Get() - { - var regions = await _regionsGetter.Regions(); - return new OkObjectResult(new Regions(regions)); } [HttpDelete] diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index d2ff4b74a..954942dc4 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -145,7 +145,6 @@ public static IServiceCollection AddOcelot(this IServiceCollection services, ICo .AddJsonFormatters(); services.AddLogging(); - services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs b/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs index 5780cff2d..f097ccf15 100644 --- a/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs +++ b/test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs @@ -18,6 +18,7 @@ public class CacheManagerCacheTests private string _resultGet; private TimeSpan _ttlSeconds; private List _resultKeys; + private string _region; public CacheManagerCacheTests() { @@ -28,7 +29,7 @@ public CacheManagerCacheTests() [Fact] public void should_get_from_cache() { - this.Given(x => x.GivenTheFollowingIsCached("someKey", "someValue")) + this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue")) .When(x => x.WhenIGetFromTheCache()) .Then(x => x.ThenTheResultIs("someValue")) .BDDfy(); @@ -88,15 +89,16 @@ private void ThenTheResultIs(string expected) private void WhenIGetFromTheCache() { - _resultGet = _ocelotOcelotCacheManager.Get(_key); + _resultGet = _ocelotOcelotCacheManager.Get(_key, _region); } - private void GivenTheFollowingIsCached(string key, string value) + private void GivenTheFollowingIsCached(string key, string region, string value) { _key = key; _value = value; + _region = region; _mockCacheManager - .Setup(x => x.Get(It.IsAny())) + .Setup(x => x.Get(It.IsAny(), It.IsAny())) .Returns(value); } } diff --git a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs index 0039e2afe..ff74fba05 100644 --- a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs @@ -92,7 +92,7 @@ private void GivenTheDownstreamRouteIs() { var reRoute = new ReRouteBuilder() .WithIsCached(true) - .WithCacheOptions(new CacheOptions(100)) + .WithCacheOptions(new CacheOptions(100, "kanken")) .WithUpstreamHttpMethod(new List { "Get" }) .Build(); @@ -120,7 +120,7 @@ private void GivenThereIsADownstreamUrl() private void ThenTheCacheGetIsCalledCorrectly() { _cacheManager - .Verify(x => x.Get(It.IsAny()), Times.Once); + .Verify(x => x.Get(It.IsAny(), It.IsAny()), Times.Once); } private void ThenTheCacheAddIsCalledCorrectly() @@ -140,7 +140,7 @@ private void GivenThereIsACachedResponse(HttpResponseMessage response) { _response = response; _cacheManager - .Setup(x => x.Get(It.IsAny())) + .Setup(x => x.Get(It.IsAny(), It.IsAny())) .Returns(_response); } diff --git a/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs b/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs index ed8491ecf..6a4754259 100644 --- a/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs +++ b/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs @@ -2,6 +2,7 @@ using Ocelot.Cache; using Ocelot.Configuration; using Ocelot.Configuration.Builder; +using Ocelot.Configuration.File; using Shouldly; using TestStack.BDDfy; using Xunit; @@ -11,23 +12,41 @@ namespace Ocelot.UnitTests.Cache public class RegionCreatorTests { private string _result; - private ReRoute _reRoute; + private FileReRoute _reRoute; [Fact] public void should_create_region() { - var reRoute = new ReRouteBuilder() - .WithUpstreamHttpMethod(new List{"Get"}) - .WithUpstreamPathTemplate("/test/dummy") - .Build(); + var reRoute = new FileReRoute + { + UpstreamHttpMethod = new List { "Get" }, + UpstreamPathTemplate = "/testdummy" + }; this.Given(_ => GivenTheReRoute(reRoute)) .When(_ => WhenICreateTheRegion()) .Then(_ => ThenTheRegionIs("Gettestdummy")) .BDDfy(); } + + [Fact] + public void should_use_region() + { + var reRoute = new FileReRoute + { + FileCacheOptions = new FileCacheOptions + { + Region = "region" + } + }; + + this.Given(_ => GivenTheReRoute(reRoute)) + .When(_ => WhenICreateTheRegion()) + .Then(_ => ThenTheRegionIs("region")) + .BDDfy(); + } - private void GivenTheReRoute(ReRoute reRoute) + private void GivenTheReRoute(FileReRoute reRoute) { _reRoute = reRoute; } @@ -35,7 +54,7 @@ private void GivenTheReRoute(ReRoute reRoute) private void WhenICreateTheRegion() { RegionCreator regionCreator = new RegionCreator(); - _result = regionCreator.Region(_reRoute); + _result = regionCreator.Create(_reRoute); } private void ThenTheRegionIs(string expected) diff --git a/test/Ocelot.UnitTests/Cache/RegionsGetterTests.cs b/test/Ocelot.UnitTests/Cache/RegionsGetterTests.cs deleted file mode 100644 index a7d2b6623..000000000 --- a/test/Ocelot.UnitTests/Cache/RegionsGetterTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -using Xunit; -using TestStack.BDDfy; -using Shouldly; -using Ocelot.Cache; -using Moq; -using Ocelot.Configuration.Provider; -using System.Collections.Generic; -using Ocelot.Responses; -using Ocelot.Configuration; -using System.Threading.Tasks; -using Ocelot.Configuration.Builder; -using System; -using Ocelot.Errors; -using Ocelot.Logging; - -namespace Ocelot.UnitTests.Cache -{ - public class RegionsGetterTests - { - private RegionsGetter _regionsGetter; - private readonly Mock _provider; - private readonly Mock _creator; - private readonly Mock _factory; - private List _result; - - public RegionsGetterTests() - { - _provider = new Mock(); - _creator = new Mock(); - _factory = new Mock(); - var logger = new Mock(); - _factory - .Setup(x => x.CreateLogger()) - .Returns(logger.Object); - _regionsGetter = new RegionsGetter(_provider.Object, _creator.Object, _factory.Object); - } - - [Fact] - public void should_get_regions() - { - var cacheOptions = new CacheOptions(12); - - var reRoute = new ReRouteBuilder() - .WithUpstreamHttpMethod(new List{"Get"}) - .WithUpstreamPathTemplate("/") - .WithCacheOptions(cacheOptions) - .WithIsCached(true) - .Build(); - - var reRoutes = new List - { - reRoute - }; - - var config = new OcelotConfiguration(reRoutes, "whocares!"); - - var expected = new List - { - "balls" - }; - - this.Given(_ => GivenTheFollowingConfig(config)) - .And(_ => GivenTheProviderReturns("balls")) - .When(_ => WhenIGetTheRegions()) - .Then(_ => ThenTheFollowingIsReturned(expected)) - .BDDfy(); - } - - [Fact] - public void should_return_empty_regions() - { - var expected = new List(); - - this.Given(_ => GivenAnErrorGettingTheConfig()) - .When(_ => WhenIGetTheRegions()) - .Then(_ => ThenTheFollowingIsReturned(expected)) - .BDDfy(); - } - - private void GivenAnErrorGettingTheConfig() - { - var config = new OcelotConfiguration(new List(), "whocares!"); - - _provider - .Setup(x => x.Get()) - .ReturnsAsync(new ErrorResponse(It.IsAny())); - } - - private void GivenTheProviderReturns(string expected) - { - _creator - .Setup(x => x.Region(It.IsAny())) - .Returns(expected); - } - - private void GivenTheFollowingConfig(IOcelotConfiguration config) - { - _provider - .Setup(x => x.Get()) - .ReturnsAsync(new OkResponse(config)); - } - - private void WhenIGetTheRegions() - { - _result = _regionsGetter.Regions().Result; - } - - private void ThenTheFollowingIsReturned(List expected) - { - _result.ShouldBe(expected); - } - } -} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs index bd8c46d3a..386958c92 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; +using Ocelot.Cache; using Ocelot.Configuration; using Ocelot.Configuration.Builder; using Ocelot.Configuration.Creator; @@ -39,6 +40,7 @@ public class FileConfigurationCreatorTests private Mock _qosOptionsCreator; private Mock _fileReRouteOptionsCreator; private Mock _rateLimitOptions; + private Mock _regionCreator; public FileConfigurationCreatorTests() { @@ -59,6 +61,7 @@ public FileConfigurationCreatorTests() _qosOptionsCreator = new Mock(); _fileReRouteOptionsCreator = new Mock(); _rateLimitOptions = new Mock(); + _regionCreator = new Mock(); _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( _fileConfig.Object, _validator.Object, _logger.Object, @@ -66,7 +69,51 @@ public FileConfigurationCreatorTests() _qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object, _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object, _serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object, - _rateLimitOptions.Object); + _rateLimitOptions.Object, _regionCreator.Object); + } + + [Fact] + public void should_call_region_creator() + { + var reRouteOptions = new ReRouteOptionsBuilder() + .Build(); + + this.Given(x => x.GivenTheConfigIs(new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamHost = "127.0.0.1", + UpstreamPathTemplate = "/api/products/{productId}", + DownstreamPathTemplate = "/products/{productId}", + UpstreamHttpMethod = new List { "Get" }, + FileCacheOptions = new FileCacheOptions + { + Region = "region" + } + } + }, + })) + .And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions)) + .And(x => x.GivenTheConfigIsValid()) + .And(x => x.GivenTheFollowingRegionIsReturned("region")) + .When(x => x.WhenICreateTheConfig()) + .Then(x => x.ThenTheRegionCreatorIsCalledCorrectly("region")) + .BDDfy(); + } + + private void GivenTheFollowingRegionIsReturned(string region) + { + _regionCreator + .Setup(x => x.Create(It.IsAny())) + .Returns(region); + } + + private void ThenTheRegionCreatorIsCalledCorrectly(string expected) + { + _regionCreator + .Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once); } [Fact] diff --git a/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs b/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs index 7639ed78c..100597a01 100644 --- a/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs @@ -15,23 +15,12 @@ public class OutputCacheControllerTests { private OutputCacheController _controller; private Mock> _cache; - private Mock _getter; private IActionResult _result; public OutputCacheControllerTests() { _cache = new Mock>(); - _getter = new Mock(); - _controller = new OutputCacheController(_cache.Object, _getter.Object); - } - - [Fact] - public void should_get_all_keys_from_server() - { - this.Given(_ => GivenTheFollowingKeys(new List{"b", "a"})) - .When(_ => WhenIGetTheKeys()) - .Then(_ => ThenTheKeysAreReturned()) - .BDDfy(); + _controller = new OutputCacheController(_cache.Object); } [Fact] @@ -53,20 +42,5 @@ private void WhenIDeleteTheKey(string key) { _result = _controller.Delete(key); } - - private void GivenTheFollowingKeys(List keys) - { - - } - - private void WhenIGetTheKeys() - { - _result = _controller.Get().Result; - } - - private void ThenTheKeysAreReturned() - { - _result.ShouldBeOfType(); - } } } \ No newline at end of file