mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-03 01:02:51 +08:00

* started messing around with this on the train last night * mega hacking away to change middleware into Ocelot iddleware * scoped data repo back in * broken commit getting tests working * another broken commit farting around with tests * all unit tests passing again * mw pipeline for ocelot...still loads of hacks but getting there now to get acceptance tests working, then fix config so you can have aggregate and then imlement multiplexer, then mapping to response...loads to do * all tests passing before aggregation feature implemented * removed all the request middleware stuff we dont need it * updated how errors work...tho i think there could be edge case here when aggregating because one downstream could error and this would effect another * removed multiplexer so you dont have to send route down, this isnt very thread safe...sigh * hacking around getting the config for aggregates in, this might change * refactored builder and unit tests passing now * Updated a bunch of ports for tests * plugged in code to create reroutes that are aggregates * made multiplexer a class * hacked test to death * simple aggregator done, initial validation done * removed request id from context, it is still specific for http request * now aggregates to json always * docs for aggregate reroutes * Updated docs
115 lines
4.1 KiB
C#
115 lines
4.1 KiB
C#
using Ocelot.Errors;
|
|
using Ocelot.Infrastructure.RequestData;
|
|
using Ocelot.Middleware;
|
|
|
|
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;
|
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
public class OutputCacheMiddlewareRealCacheTests
|
|
{
|
|
private IOcelotCache<CachedResponse> _cacheManager;
|
|
private CachedResponse _response;
|
|
private OutputCacheMiddleware _middleware;
|
|
private DownstreamContext _downstreamContext;
|
|
private OcelotRequestDelegate _next;
|
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
|
private IRegionCreator _regionCreator;
|
|
private Mock<IOcelotLogger> _logger;
|
|
|
|
public OutputCacheMiddlewareRealCacheTests()
|
|
{
|
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
|
_logger = new Mock<IOcelotLogger>();
|
|
_loggerFactory.Setup(x => x.CreateLogger<OutputCacheMiddleware>()).Returns(_logger.Object);
|
|
_regionCreator = new RegionCreator();
|
|
var cacheManagerOutputCache = CacheFactory.Build<CachedResponse>("OcelotOutputCache", x =>
|
|
{
|
|
x.WithDictionaryHandle();
|
|
});
|
|
_cacheManager = new OcelotCacheManagerCache<CachedResponse>(cacheManagerOutputCache);
|
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
|
_downstreamContext.DownstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123");
|
|
_next = async context => {
|
|
//do nothing..
|
|
};
|
|
_middleware = new OutputCacheMiddleware(_next, _loggerFactory.Object, _cacheManager, _regionCreator);
|
|
}
|
|
|
|
[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())
|
|
.When(x => x.WhenICallTheMiddleware())
|
|
.Then(x => x.ThenTheContentTypeHeaderIsCached())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void WhenICallTheMiddleware()
|
|
{
|
|
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
private void GivenResponseIsNotCached(HttpResponseMessage message)
|
|
{
|
|
_downstreamContext.DownstreamResponse = message;
|
|
}
|
|
|
|
private void GivenTheDownstreamRouteIs()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithIsCached(true)
|
|
.WithCacheOptions(new CacheOptions(100, "kanken"))
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build();
|
|
|
|
_downstreamContext.DownstreamReRoute = reRoute;
|
|
}
|
|
|
|
private void GivenThereAreNoErrors()
|
|
{
|
|
_downstreamContext.Errors = new List<Error>();
|
|
}
|
|
}
|
|
}
|