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(); } } }