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

* changed name to cache options to fix issue #146 * Add acceptance test that exposes JSON deserialization bug from issue #146 - Create InMemoryJsonHandle for CacheManager that mimics DictionaryHandle but uses ICacheSerializer to serialize/deserialize values instead of saving references - Add CacheManager.Serialization.Json package - Add StartupWithCustomCacheHandle class that extends Startup and overrides ConfigureServices to register InMemoryJsonHandle - Add GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache method to initiate Ocelot with StartupWithCustomCacheHandle - Add test should_return_response_200_with_simple_url_when_using_jsonserialized_cache * Create Acceptance test that exposes issue #152 - Add GivenOcelotIsRunningUsingJsonSerializedCache() that initializes Ocelot with InMemoryJsonHandle - Add should_return_cached_response_when_using_jsonserialized_cache test * Change Consul port to 9502 on should_return_response_200_with_simple_url_when_using_jsonserialized_cache() test * Implement mapping of HttpResponseMessage to CachedResponse to enable distributed caching - Add CachedResponse class that holds HttpResponse data - Add mapping methods in OutputCacheMiddleware to create HttpResponseMessage from CachedResponse and vice versa - Replace HttpResponseMessage with CachedResponse in services registrations - Replace HttpResponseMessage with CachedResponse in OutputCacheController's IOcelotCache * Fix unit tests for OutputCacheMiddleware and OutputCacheController by replacing HttpResponseMessage with CachedResponse * Add .editorconfig with default identation settings (spaces with size 4) * Re-format broken files with new identation settings * Add Startup_WithConsul_And_CustomCacheHandle class - Use Startup_WithConsul_And_CustomCacheHandle in GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache step * Do minor cleanups - Rename StartupWithCustomCacheHandle to Startup_WithCustomCacheHandle for better readability - Remove cachemanager settings Action in Startup since it is not used anymore * Drop Task in CreateHttpResponseMessage - unnecessary overhead * Make setters private in CachedResponse - Rework CreateCachedResponse to use new CachedResponse constructor
136 lines
4.2 KiB
C#
136 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using CacheManager.Core;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Ocelot.Cache;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.File;
|
|
using Ocelot.DependencyInjection;
|
|
using Shouldly;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
namespace Ocelot.UnitTests.DependencyInjection
|
|
{
|
|
public class OcelotBuilderTests
|
|
{
|
|
private IServiceCollection _services;
|
|
private IConfigurationRoot _configRoot;
|
|
private IOcelotBuilder _ocelotBuilder;
|
|
private int _maxRetries;
|
|
|
|
public OcelotBuilderTests()
|
|
{
|
|
IWebHostBuilder builder = new WebHostBuilder();
|
|
_configRoot = new ConfigurationRoot(new List<IConfigurationProvider>());
|
|
_services = new ServiceCollection();
|
|
_services.AddSingleton(builder);
|
|
_maxRetries = 100;
|
|
}
|
|
private Exception _ex;
|
|
|
|
[Fact]
|
|
public void should_set_up_services()
|
|
{
|
|
this.When(x => WhenISetUpOcelotServices())
|
|
.Then(x => ThenAnExceptionIsntThrown())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_ocelot_builder()
|
|
{
|
|
this.When(x => WhenISetUpOcelotServices())
|
|
.Then(x => ThenAnOcelotBuilderIsReturned())
|
|
.BDDfy();
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void should_set_up_cache_manager()
|
|
{
|
|
this.Given(x => WhenISetUpOcelotServices())
|
|
.When(x => WhenISetUpCacheManager())
|
|
.Then(x => ThenAnExceptionIsntThrown())
|
|
.And(x => OnlyOneVersionOfEachCacheIsRegistered())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_set_up_consul()
|
|
{
|
|
this.Given(x => WhenISetUpOcelotServices())
|
|
.When(x => WhenISetUpConsul())
|
|
.Then(x => ThenAnExceptionIsntThrown())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void OnlyOneVersionOfEachCacheIsRegistered()
|
|
{
|
|
var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>));
|
|
var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<CachedResponse>));
|
|
var thing = (CacheManager.Core.ICacheManager<CachedResponse>)outputCacheManager.ImplementationInstance;
|
|
thing.Configuration.MaxRetries.ShouldBe(_maxRetries);
|
|
|
|
var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<IOcelotConfiguration>));
|
|
var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<IOcelotConfiguration>));
|
|
|
|
var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<FileConfiguration>));
|
|
var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<FileConfiguration>));
|
|
}
|
|
|
|
private void WhenISetUpConsul()
|
|
{
|
|
try
|
|
{
|
|
_ocelotBuilder.AddStoreOcelotConfigurationInConsul();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_ex = e;
|
|
}
|
|
}
|
|
|
|
private void ThenAnOcelotBuilderIsReturned()
|
|
{
|
|
_ocelotBuilder.ShouldBeOfType<OcelotBuilder>();
|
|
}
|
|
|
|
private void WhenISetUpOcelotServices()
|
|
{
|
|
try
|
|
{
|
|
_ocelotBuilder = _services.AddOcelot(_configRoot);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_ex = e;
|
|
}
|
|
}
|
|
private void WhenISetUpCacheManager()
|
|
{
|
|
try
|
|
{
|
|
_ocelotBuilder.AddCacheManager(x => {
|
|
x.WithMaxRetries(_maxRetries);
|
|
x.WithDictionaryHandle();
|
|
});
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_ex = e;
|
|
}
|
|
}
|
|
|
|
private void ThenAnExceptionIsntThrown()
|
|
{
|
|
_ex.ShouldBeNull();
|
|
}
|
|
}
|
|
}
|