Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aliostad committed Dec 31, 2019
1 parent 41da061 commit 902090c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
13 changes: 8 additions & 5 deletions src/CacheCow.Server.Core.Mvc/HttpCacheFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.IO;
using Microsoft.Net.Http.Headers;
using CacheCow.Server.Headers;
using Microsoft.Extensions.Configuration;

namespace CacheCow.Server.Core.Mvc
{
Expand All @@ -22,13 +23,14 @@ namespace CacheCow.Server.Core.Mvc
public class HttpCacheFilter : IAsyncResourceFilter
{
private ICacheabilityValidator _validator;

private readonly IConfiguration _configuration;
private const string StreamName = "##__travesty_that_I_have_to_do_this__##";

public HttpCacheFilter(ICacheabilityValidator validator,
ICacheDirectiveProvider cacheDirectiveProvider)
ICacheDirectiveProvider cacheDirectiveProvider, IConfiguration configuration)
{
_validator = validator;
_configuration = configuration;
CacheDirectiveProvider = cacheDirectiveProvider;
ApplyNoCacheNoStoreForNonCacheableResponse = true;
}
Expand All @@ -42,7 +44,7 @@ public HttpCacheFilter(ICacheabilityValidator validator,
/// <returns></returns>
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
var pipa = new CachingPipeline(_validator, CacheDirectiveProvider)
var pipa = new CachingPipeline(_validator, CacheDirectiveProvider, _configuration)
{
ApplyNoCacheNoStoreForNonCacheableResponse = ApplyNoCacheNoStoreForNonCacheableResponse,
ConfiguredExpiry = ConfiguredExpiry
Expand Down Expand Up @@ -82,8 +84,9 @@ public async Task OnResourceExecutionAsync(ResourceExecutingContext context, Res
public class HttpCacheFilter<T> : HttpCacheFilter
{
public HttpCacheFilter(ICacheabilityValidator validator,
ICacheDirectiveProvider<T> cacheDirectiveProvider) :
base(validator, cacheDirectiveProvider)
ICacheDirectiveProvider<T> cacheDirectiveProvider,
IConfiguration configuration) :
base(validator, cacheDirectiveProvider, configuration)
{
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/CacheCow.Server.WebApi/HttpCacheAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public async override Task OnActionExecutedAsync(HttpActionExecutedContext conte
if (cacheValidated ?? false)
{
cacheCowHeader.ValidationMatched = true;
context.Response.Headers.Add(CacheCowHeader.Name, cacheCowHeader.ToString());
if (!_doNotEmitHeader)
context.Response.Headers.Add(CacheCowHeader.Name, cacheCowHeader.ToString());
return;
}
}
Expand All @@ -194,7 +195,8 @@ public async override Task OnActionExecutedAsync(HttpActionExecutedContext conte
else
context.Response.Headers.Add(HttpHeaderNames.CacheControl, cacheControl.ToString());

context.Response.Headers.Add(CacheCowHeader.Name, cacheCowHeader.ToString());
if (! _doNotEmitHeader)
context.Response.Headers.Add(CacheCowHeader.Name, cacheCowHeader.ToString());
}


Expand Down
6 changes: 5 additions & 1 deletion src/CacheCow.Server/CachingPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public class CachingPipeline : ICachingPipeline
private CacheCowHeader _cacheCowHeader;
private readonly bool _doNotEmitHeader = false;

/*
public CachingPipeline(ICacheabilityValidator validator,
ICacheDirectiveProvider cacheDirectiveProvider)
{
_validator = validator;
_cacheDirectiveProvider = cacheDirectiveProvider;
}
*/

public CachingPipeline(ICacheabilityValidator validator,
ICacheDirectiveProvider cacheDirectiveProvider,
Expand Down Expand Up @@ -244,7 +246,9 @@ public async Task After(HttpContext context, object viewModel)

public class CachingPipeline<TViewModel> : CachingPipeline, ICachingPipeline<TViewModel>
{
public CachingPipeline(ICacheabilityValidator validator, ICacheDirectiveProvider<TViewModel> cacheDirectiveProvider) : base(validator, cacheDirectiveProvider)
public CachingPipeline(ICacheabilityValidator validator,
ICacheDirectiveProvider<TViewModel> cacheDirectiveProvider,
IConfiguration configuration) : base(validator, cacheDirectiveProvider, configuration)
{
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/CacheCow.Client.Tests/CachingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ private HttpResponseMessage GetOkMessage(bool mustRevalidate = false)
public async Task Issue238_It_cache_even_if_compression_is_on()
{
var compressionHandler = new HttpClientHandler();
if (compressionHandler.SupportsAutomaticDecompression) compressionHandler.AutomaticDecompression = DecompressionMethods.GZip;
if (compressionHandler.SupportsAutomaticDecompression)
compressionHandler.AutomaticDecompression = DecompressionMethods.GZip;

var pipeline = new CachingHandler(new DictionaryBasedCache())
{
InnerHandler = compressionHandler,
DefaultVaryHeaders = new [] { "Accept", "Accept-Encoding" }
InnerHandler = compressionHandler, DefaultVaryHeaders = new[] {"Accept", "Accept-Encoding"}
};

var client = new HttpClient(pipeline);
Expand Down
27 changes: 25 additions & 2 deletions test/CacheCow.Client.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@ namespace CacheCow.Client.Tests

public class IntegrationTests
{
const string Url = "https://ssl.gstatic.com/gb/images/j_e6a6aca6.png";

[Fact]
public async Task Test_GoogleImage_WorksOnFirstSecondRequestNotThird()
{
const string Url = "https://ssl.gstatic.com/gb/images/j_e6a6aca6.png";
var httpClient = new HttpClient(new CachingHandler()
{
InnerHandler = new HttpClientHandler()
});
httpClient.DefaultRequestHeaders.Add(HttpHeaderNames.Accept, "image/png");

var httpResponseMessage = await httpClient.GetAsync(Url);
var httpResponseMessage2 = await httpClient.GetAsync(Url);
var cacheCowHeader = httpResponseMessage2.Headers.GetCacheCowHeader();
Assert.NotNull(cacheCowHeader);
Assert.Equal(true, cacheCowHeader.RetrievedFromCache);
}

[Fact]
public async Task SettingNoHeaderWorks()
{
var cachecow = new CachingHandler()
{
DoNotEmitCacheCowHeader = true,
InnerHandler = new HttpClientHandler()
};

var client = new HttpClient(cachecow);

var request1 = new HttpRequestMessage(HttpMethod.Get, Url);
var request2 = new HttpRequestMessage(HttpMethod.Get, Url);

var response = await client.SendAsync(request1);
var responseFromCache = await client.SendAsync(request2);

var h = responseFromCache.Headers.GetCacheCowHeader();

Assert.Null(h);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;netcoreapp2.0</TargetFrameworks>
Expand Down
27 changes: 26 additions & 1 deletion test/CacheCow.Server.Core.Mvc.Tests/WithQueryProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using CacheCow.Client;
using CacheCow.Server.Headers;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

namespace CacheCow.Server.Core.Mvc.Tests
{
Expand Down Expand Up @@ -54,6 +55,10 @@ public async Task ETagsAreTheSame()
[Fact]
public async Task ETagsAreTheSameForCollection()
{
_server = new TestServer(new WebHostBuilder()
.UseStartup<WithQueryProviderStartup>());
_client = _server.CreateClient();

var response = await _client.GetAsync("/api/withquery");
var response2 = await _client.GetAsync("/api/withquery");
Assert.NotNull(response.Headers.ETag);
Expand All @@ -63,6 +68,26 @@ public async Task ETagsAreTheSameForCollection()
Assert.Equal(response.Headers.ETag.Tag, response2.Headers.ETag.Tag);
}

[Fact]
public async Task NoHeaderWhenISayNoHeader()
{
_server = new TestServer(new WebHostBuilder()
.UseStartup<WithQueryProviderStartup>()
.ConfigureAppConfiguration((ctx, cfg) =>
{
cfg.AddInMemoryCollection(
new Dictionary<string, string>
{
{"do_not_emit_cachecow_header", "true"}
});
})
);
_client = _server.CreateClient();

var response = await _client.GetAsync("/api/withquery");
Assert.False(response.Headers.Contains("x-cachecow-server"));
}

[Fact]
public async Task SecondTimeComesFromCacheBecauseOfQueryProvider()
{
Expand Down Expand Up @@ -101,7 +126,7 @@ public Task<TimedEntityTagHeaderValue> QueryAsync(HttpContext context)

public class TestViewModelCollectionQueryProvider : ITimedETagQueryProvider<IEnumerable<TestViewModel>>
{

public const string HeaderName = "x-test-etag";
public void Dispose()
{
Expand Down
6 changes: 6 additions & 0 deletions test/CacheCow.Server.WebApi.Tests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="do_not_emit_cachecow_header" value="true" />
</appSettings>
</configuration>
4 changes: 2 additions & 2 deletions test/CacheCow.Server.WebApi.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public IntegrationTests()
}

[Fact]
public async Task HasHeaders()
public async Task DoesNotHaveHeaderBecauseOfConfig()
{
var i = new HttpMessageInvoker(_server);
var response = await i.SendAsync(new HttpRequestMessage(HttpMethod.Get, new Uri("http://chiz/api/car/1", UriKind.Absolute)), CancellationToken.None);
Assert.True(response.IsSuccessStatusCode);
var h = response.GetCacheCowHeader();
Assert.NotNull(h);
Assert.Null(h);
}

[Fact]
Expand Down

0 comments on commit 902090c

Please sign in to comment.