using Ocelot.Infrastructure.RequestData; 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 Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Cache; using Ocelot.Cache.Middleware; using Ocelot.Configuration; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; public class OutputCacheMiddlewareRealCacheTests : ServerHostedMiddlewareTest { private IOcelotCache _cacheManager; private CachedResponse _response; private IRequestScopedDataRepository _repo; public OutputCacheMiddlewareRealCacheTests() { ScopedRepository .Setup(sr => sr.Get("DownstreamRequest")) .Returns(new OkResponse(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123"))); GivenTheTestServerIsConfigured(); } [Fact] public void should_cache_content_headers() { var content = new StringContent("{\"Test\": 1}") { Headers = { ContentType = new MediaTypeHeaderValue("application/json")} }; var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = content, }; this.Given(x => x.GivenResponseIsNotCached(response)) .And(x => x.GivenTheDownstreamRouteIs()) .And(x => x.GivenThereAreNoErrors()) .And(x => x.GivenThereIsADownstreamUrl()) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheContentTypeHeaderIsCached()) .BDDfy(); } 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"); } protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services) { var cacheManagerOutputCache = CacheFactory.Build("OcelotOutputCache", x => { x.WithDictionaryHandle(); }); _cacheManager = new OcelotCacheManagerCache(cacheManagerOutputCache); services.AddSingleton>(cacheManagerOutputCache); services.AddSingleton>(_cacheManager); services.AddSingleton(); services.AddLogging(); services.AddSingleton(_cacheManager); services.AddSingleton(ScopedRepository.Object); services.AddSingleton(); } protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app) { app.UseOutputCacheMiddleware(); } private void GivenResponseIsNotCached(HttpResponseMessage message) { ScopedRepository .Setup(x => x.Get("HttpResponseMessage")) .Returns(new OkResponse(message)); } private void GivenTheDownstreamRouteIs() { var reRoute = new ReRouteBuilder() .WithIsCached(true) .WithCacheOptions(new CacheOptions(100, "kanken")) .WithUpstreamHttpMethod(new List { "Get" }) .Build(); var downstreamRoute = new DownstreamRoute(new List(), reRoute); ScopedRepository .Setup(x => x.Get(It.IsAny())) .Returns(new OkResponse(downstreamRoute)); } private void GivenThereAreNoErrors() { ScopedRepository .Setup(x => x.Get("OcelotMiddlewareError")) .Returns(new OkResponse(false)); } private void GivenThereIsADownstreamUrl() { ScopedRepository .Setup(x => x.Get("DownstreamUrl")) .Returns(new OkResponse("anything")); } } }