Implement mapping of HttpResponseMessage to CachedResponse to fix #152 (#153)

* 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
This commit is contained in:
Charalampos Chomenidis
2017-11-25 16:47:17 +02:00
committed by Tom Pallister
parent 3b27bb376e
commit 48b5a32676
15 changed files with 401 additions and 46 deletions

View File

@ -19,12 +19,12 @@
public class OutputCacheMiddlewareTests : ServerHostedMiddlewareTest
{
private readonly Mock<IOcelotCache<HttpResponseMessage>> _cacheManager;
private HttpResponseMessage _response;
private readonly Mock<IOcelotCache<CachedResponse>> _cacheManager;
private CachedResponse _response;
public OutputCacheMiddlewareTests()
{
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
_cacheManager = new Mock<IOcelotCache<CachedResponse>>();
ScopedRepository
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
@ -36,7 +36,8 @@
[Fact]
public void should_returned_cached_item_when_it_is_in_cache()
{
this.Given(x => x.GivenThereIsACachedResponse(new HttpResponseMessage()))
var cachedResponse = new CachedResponse();
this.Given(x => x.GivenThereIsACachedResponse(cachedResponse))
.And(x => x.GivenTheDownstreamRouteIs())
.And(x => x.GivenThereIsADownstreamUrl())
.When(x => x.WhenICallTheMiddleware())
@ -70,7 +71,7 @@
app.UseOutputCacheMiddleware();
}
private void GivenThereIsACachedResponse(HttpResponseMessage response)
private void GivenThereIsACachedResponse(CachedResponse response)
{
_response = response;
_cacheManager
@ -123,7 +124,7 @@
private void ThenTheCacheAddIsCalledCorrectly()
{
_cacheManager
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<HttpResponseMessage>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<CachedResponse>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
}
}
}