realised the user can just set the region..delete time

This commit is contained in:
Tom Gardham-Pallister 2017-06-28 08:17:48 +01:00
parent 0fa759c76c
commit ab953f28fd
10 changed files with 86 additions and 12 deletions

View File

@ -12,9 +12,9 @@ namespace Ocelot.Cache
{ {
public string Region(ReRoute reRoute) 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; return region;
} }

View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace Ocelot.Cache
{
public class Regions
{
public Regions(List<string> value)
{
Value = value;
}
public List<string> Value {get;private set;}
}
}

View File

@ -8,5 +8,6 @@
} }
public int TtlSeconds { get; private set; } public int TtlSeconds { get; private set; }
public string Region {get;private set;}
} }
} }

View File

@ -3,5 +3,6 @@
public class FileCacheOptions public class FileCacheOptions
{ {
public int TtlSeconds { get; set; } public int TtlSeconds { get; set; }
public string Region {get;private set;}
} }
} }

View File

@ -1,4 +1,5 @@
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Ocelot.Cache; using Ocelot.Cache;
@ -20,10 +21,10 @@ namespace Ocelot.Controllers
} }
[HttpGet] [HttpGet]
public IActionResult Get() public async Task<IActionResult> Get()
{ {
var regions = _regionsGetter.Regions(); var regions = await _regionsGetter.Regions();
return new OkObjectResult(regions); return new OkObjectResult(new Regions(regions));
} }
[HttpDelete] [HttpDelete]

View File

@ -7,6 +7,7 @@ using System.Net.Http.Headers;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ocelot.Cache;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.ManualTest; using Ocelot.ManualTest;
using Shouldly; using Shouldly;
@ -221,7 +222,6 @@ namespace Ocelot.IntegrationTests
[Fact] [Fact]
public void should_return_regions() public void should_return_regions()
{ {
var initialConfiguration = new FileConfiguration var initialConfiguration = new FileConfiguration
{ {
GlobalConfiguration = new FileGlobalConfiguration GlobalConfiguration = new FileGlobalConfiguration
@ -261,8 +261,8 @@ namespace Ocelot.IntegrationTests
var expected = new List<string> var expected = new List<string>
{ {
"get /", "get",
"get /test" "gettest"
}; };
this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) this.Given(x => GivenThereIsAConfiguration(initialConfiguration))
@ -275,6 +275,57 @@ namespace Ocelot.IntegrationTests
.BDDfy(); .BDDfy();
} }
[Fact]
public void should_clear_region()
{
var initialConfiguration = new FileConfiguration
{
GlobalConfiguration = new FileGlobalConfiguration
{
AdministrationPath = "/administration"
},
ReRoutes = new List<FileReRoute>()
{
new FileReRoute()
{
DownstreamHost = "localhost",
DownstreamPort = 80,
DownstreamScheme = "https",
DownstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "get" },
UpstreamPathTemplate = "/",
FileCacheOptions = new FileCacheOptions
{
TtlSeconds = 10
}
},
new FileReRoute()
{
DownstreamHost = "localhost",
DownstreamPort = 80,
DownstreamScheme = "https",
DownstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "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) private void GivenAnotherOcelotIsRunning(string baseUrl)
{ {
_httpClientTwo.BaseAddress = new Uri(baseUrl); _httpClientTwo.BaseAddress = new Uri(baseUrl);
@ -316,8 +367,8 @@ namespace Ocelot.IntegrationTests
private void ThenTheResponseShouldBe(List<string> expected) private void ThenTheResponseShouldBe(List<string> expected)
{ {
var content = _response.Content.ReadAsStringAsync().Result; var content = _response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<List<string>>(content); var result = JsonConvert.DeserializeObject<Regions>(content);
result.ShouldBe(expected); result.Value.ShouldBe(expected);
} }
private void ThenTheResponseShouldBe(FileConfiguration expected) private void ThenTheResponseShouldBe(FileConfiguration expected)
@ -417,6 +468,11 @@ namespace Ocelot.IntegrationTests
_response = _httpClient.GetAsync(url).Result; _response = _httpClient.GetAsync(url).Result;
} }
private void WhenIDeleteOnTheApiGateway(string url)
{
_response = _httpClient.DeleteAsync(url).Result;
}
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
{ {
_response.StatusCode.ShouldBe(expectedHttpStatusCode); _response.StatusCode.ShouldBe(expectedHttpStatusCode);

View File

@ -30,6 +30,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta2-build1317" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta2-build1317" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />

View File

@ -22,6 +22,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />

View File

@ -23,7 +23,7 @@ namespace Ocelot.UnitTests.Cache
this.Given(_ => GivenTheReRoute(reRoute)) this.Given(_ => GivenTheReRoute(reRoute))
.When(_ => WhenICreateTheRegion()) .When(_ => WhenICreateTheRegion())
.Then(_ => ThenTheRegionIs("Get /test/dummy")) .Then(_ => ThenTheRegionIs("Gettestdummy"))
.BDDfy(); .BDDfy();
} }

View File

@ -61,7 +61,7 @@ namespace Ocelot.UnitTests.Controllers
private void WhenIGetTheKeys() private void WhenIGetTheKeys()
{ {
_result = _controller.Get(); _result = _controller.Get().Result;
} }
private void ThenTheKeysAreReturned() private void ThenTheKeysAreReturned()