diff --git a/src/Ocelot/Cache/RegionBuilder.cs b/src/Ocelot/Cache/RegionBuilder.cs index eb9972bc..5ccf8973 100644 --- a/src/Ocelot/Cache/RegionBuilder.cs +++ b/src/Ocelot/Cache/RegionBuilder.cs @@ -12,9 +12,9 @@ namespace Ocelot.Cache { public string Region(ReRoute reRoute) { - var methods = string.Join(",", reRoute.UpstreamHttpMethod.Select(m => m.Method)); + var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m.Method)); - var region = $"{methods} {reRoute.UpstreamPathTemplate.Value}"; + var region = $"{methods}{reRoute.UpstreamPathTemplate.Value.Replace("/", "")}"; return region; } diff --git a/src/Ocelot/Cache/Regions.cs b/src/Ocelot/Cache/Regions.cs new file mode 100644 index 00000000..1dbefbd5 --- /dev/null +++ b/src/Ocelot/Cache/Regions.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Ocelot.Cache +{ + public class Regions + { + public Regions(List value) + { + Value = value; + } + public List Value {get;private set;} + } +} \ No newline at end of file diff --git a/src/Ocelot/Configuration/CacheOptions.cs b/src/Ocelot/Configuration/CacheOptions.cs index 2fdaf2bb..3de9e019 100644 --- a/src/Ocelot/Configuration/CacheOptions.cs +++ b/src/Ocelot/Configuration/CacheOptions.cs @@ -8,5 +8,6 @@ } public int TtlSeconds { get; private set; } + public string Region {get;private set;} } } diff --git a/src/Ocelot/Configuration/File/FileCacheOptions.cs b/src/Ocelot/Configuration/File/FileCacheOptions.cs index 3f86006b..46fc7b87 100644 --- a/src/Ocelot/Configuration/File/FileCacheOptions.cs +++ b/src/Ocelot/Configuration/File/FileCacheOptions.cs @@ -3,5 +3,6 @@ public class FileCacheOptions { public int TtlSeconds { get; set; } + public string Region {get;private set;} } } diff --git a/src/Ocelot/Controllers/OutputCacheController.cs b/src/Ocelot/Controllers/OutputCacheController.cs index 67a07d07..21dccb8d 100644 --- a/src/Ocelot/Controllers/OutputCacheController.cs +++ b/src/Ocelot/Controllers/OutputCacheController.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Ocelot.Cache; @@ -20,10 +21,10 @@ namespace Ocelot.Controllers } [HttpGet] - public IActionResult Get() + public async Task Get() { - var regions = _regionsGetter.Regions(); - return new OkObjectResult(regions); + var regions = await _regionsGetter.Regions(); + return new OkObjectResult(new Regions(regions)); } [HttpDelete] diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index e0826b68..0a0f20e4 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -7,6 +7,7 @@ using System.Net.Http.Headers; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; +using Ocelot.Cache; using Ocelot.Configuration.File; using Ocelot.ManualTest; using Shouldly; @@ -221,7 +222,6 @@ namespace Ocelot.IntegrationTests [Fact] public void should_return_regions() { - var initialConfiguration = new FileConfiguration { GlobalConfiguration = new FileGlobalConfiguration @@ -261,8 +261,8 @@ namespace Ocelot.IntegrationTests var expected = new List { - "get /", - "get /test" + "get", + "gettest" }; this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) @@ -275,6 +275,57 @@ namespace Ocelot.IntegrationTests .BDDfy(); } + [Fact] + public void should_clear_region() + { + var initialConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + AdministrationPath = "/administration" + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHost = "localhost", + DownstreamPort = 80, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/", + FileCacheOptions = new FileCacheOptions + { + TtlSeconds = 10 + } + }, + new FileReRoute() + { + DownstreamHost = "localhost", + DownstreamPort = 80, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/test", + FileCacheOptions = new FileCacheOptions + { + TtlSeconds = 10 + } + } + } + }; + + var regionToClear = "gettest"; + + this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIDeleteOnTheApiGateway($"/administration/outputcache/{regionToClear}")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.NoContent)) + .BDDfy(); + } + private void GivenAnotherOcelotIsRunning(string baseUrl) { _httpClientTwo.BaseAddress = new Uri(baseUrl); @@ -316,8 +367,8 @@ namespace Ocelot.IntegrationTests private void ThenTheResponseShouldBe(List expected) { var content = _response.Content.ReadAsStringAsync().Result; - var result = JsonConvert.DeserializeObject>(content); - result.ShouldBe(expected); + var result = JsonConvert.DeserializeObject(content); + result.Value.ShouldBe(expected); } private void ThenTheResponseShouldBe(FileConfiguration expected) @@ -417,6 +468,11 @@ namespace Ocelot.IntegrationTests _response = _httpClient.GetAsync(url).Result; } + private void WhenIDeleteOnTheApiGateway(string url) + { + _response = _httpClient.DeleteAsync(url).Result; + } + private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) { _response.StatusCode.ShouldBe(expectedHttpStatusCode); diff --git a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj index 59513b38..1556111a 100644 --- a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj +++ b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj @@ -30,6 +30,7 @@ + diff --git a/test/Ocelot.ManualTest/Ocelot.ManualTest.csproj b/test/Ocelot.ManualTest/Ocelot.ManualTest.csproj index f541111f..57f23ad0 100644 --- a/test/Ocelot.ManualTest/Ocelot.ManualTest.csproj +++ b/test/Ocelot.ManualTest/Ocelot.ManualTest.csproj @@ -22,6 +22,7 @@ + diff --git a/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs b/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs index 9d2b5a65..ed8491ec 100644 --- a/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs +++ b/test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs @@ -23,7 +23,7 @@ namespace Ocelot.UnitTests.Cache this.Given(_ => GivenTheReRoute(reRoute)) .When(_ => WhenICreateTheRegion()) - .Then(_ => ThenTheRegionIs("Get /test/dummy")) + .Then(_ => ThenTheRegionIs("Gettestdummy")) .BDDfy(); } diff --git a/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs b/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs index fa43369e..7639ed78 100644 --- a/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs @@ -61,7 +61,7 @@ namespace Ocelot.UnitTests.Controllers private void WhenIGetTheKeys() { - _result = _controller.Get(); + _result = _controller.Get().Result; } private void ThenTheKeysAreReturned()