Skip to content

Commit 82fc5b7

Browse files
author
dmalanij
committed
Refactor on Cache Key Generation tests
Created a base class with the general behaviour for any key generation test
1 parent 1f3c207 commit 82fc5b7

3 files changed

Lines changed: 66 additions & 40 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Net.Http.Headers;
4+
using System.Web.Http.Controllers;
5+
6+
namespace WebApi.OutputCache.V2.Tests
7+
{
8+
/// <summary>
9+
/// Base class for implementing tests for the generation of cache keys (meaning: implementations of the <see cref="ICacheKeyGenerator"/>
10+
/// </summary>
11+
public abstract class CacheKeyGenerationTestsBase<TCacheKeyGenerator> where TCacheKeyGenerator : ICacheKeyGenerator
12+
{
13+
private const string ArgumentKey = "filterExpression";
14+
private const string ArgumentValue = "val";
15+
protected HttpActionContext context;
16+
protected MediaTypeHeaderValue mediaType;
17+
protected Uri requestUri;
18+
protected TCacheKeyGenerator cacheKeyGenerator;
19+
protected string BaseCacheKey;
20+
21+
[SetUp]
22+
public virtual void Setup()
23+
{
24+
requestUri = new Uri("http://localhost:8080/cacheKeyGeneration?filter=val");
25+
var controllerType = typeof(TestControllers.CacheKeyGenerationController);
26+
var actionMethodInfo = controllerType.GetMethod("Get");
27+
var controllerDescriptor = new HttpControllerDescriptor() { ControllerType = controllerType };
28+
var actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, actionMethodInfo);
29+
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, requestUri.AbsoluteUri);
30+
31+
context = new HttpActionContext(
32+
new HttpControllerContext() { ControllerDescriptor = controllerDescriptor, Request = request },
33+
actionDescriptor
34+
);
35+
mediaType = new MediaTypeHeaderValue("application/json");
36+
37+
BaseCacheKey = new CacheOutputConfiguration(null).MakeBaseCachekey((TestControllers.CacheKeyGenerationController c) => c.Get(String.Empty));
38+
cacheKeyGenerator = BuildCacheKeyGenerator();
39+
}
40+
41+
protected abstract TCacheKeyGenerator BuildCacheKeyGenerator();
42+
43+
protected virtual void AssertCacheKeysBasicFormat(string cacheKey)
44+
{
45+
Assert.IsNotNull(cacheKey);
46+
StringAssert.StartsWith(BaseCacheKey, cacheKey, "Key does not start with BaseKey");
47+
StringAssert.EndsWith(mediaType.ToString(), cacheKey, "Key does not end with MediaType");
48+
}
49+
50+
protected void AddActionArgumentsToContext()
51+
{
52+
context.ActionArguments.Add(ArgumentKey, ArgumentValue);
53+
}
54+
55+
protected string FormatActionArgumentsForKeyAssertion()
56+
{
57+
return String.Format("{0}={1}", ArgumentKey, ArgumentValue);
58+
}
59+
}
60+
}

test/WebApi.OutputCache.V2.Tests/DefaultCacheKeyGeneratorTests.cs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,11 @@
66
namespace WebApi.OutputCache.V2.Tests
77
{
88
[TestFixture]
9-
public class DefaultCacheKeyGeneratorTests
9+
public class DefaultCacheKeyGeneratorTests : CacheKeyGenerationTestsBase<DefaultCacheKeyGenerator>
1010
{
11-
private const string ArgumentKey = "filterExpression";
12-
private const string ArgumentValue = "val";
13-
private HttpActionContext context;
14-
private MediaTypeHeaderValue mediaType;
15-
private Uri requestUri;
16-
private DefaultCacheKeyGenerator cacheKeyGenerator;
17-
private string BaseCacheKey;
18-
19-
[SetUp]
20-
public void Setup()
21-
{
22-
requestUri = new Uri("http://localhost:8080/cacheKeyGeneration?filter=val");
23-
var controllerType = typeof(TestControllers.CacheKeyGenerationController);
24-
var actionMethodInfo = controllerType.GetMethod("Get");
25-
var controllerDescriptor = new HttpControllerDescriptor() { ControllerType = controllerType };
26-
var actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, actionMethodInfo);
27-
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, requestUri.AbsoluteUri);
28-
29-
context = new HttpActionContext(
30-
new HttpControllerContext() { ControllerDescriptor = controllerDescriptor, Request = request },
31-
actionDescriptor
32-
);
33-
mediaType = new MediaTypeHeaderValue("application/json");
34-
35-
cacheKeyGenerator = new DefaultCacheKeyGenerator();
36-
BaseCacheKey = new CacheOutputConfiguration(null).MakeBaseCachekey((TestControllers.CacheKeyGenerationController c) => c.Get(String.Empty));
37-
}
38-
39-
private void AssertCacheKeysBasicFormat(string cacheKey)
40-
{
41-
Assert.IsNotNull(cacheKey);
42-
StringAssert.StartsWith(BaseCacheKey, cacheKey, "Key does not start with BaseKey");
43-
StringAssert.EndsWith(mediaType.ToString(), cacheKey, "Key does not end with MediaType");
44-
}
45-
46-
private void AddActionArgumentsToContext()
11+
protected override DefaultCacheKeyGenerator BuildCacheKeyGenerator()
4712
{
48-
context.ActionArguments.Add(ArgumentKey, ArgumentValue);
13+
return new DefaultCacheKeyGenerator();
4914
}
5015

5116
[Test]
@@ -73,7 +38,7 @@ public void WithParametersIncludeQueryString_ShouldReturnBaseKeyAndArgumentsAndQ
7338
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false);
7439

7540
AssertCacheKeysBasicFormat(cacheKey);
76-
Assert.AreEqual(String.Format("{0}-{1}={2}&{3}:{4}", BaseCacheKey, ArgumentKey, ArgumentValue, requestUri.Query.Substring(1), mediaType), cacheKey, "Key does not match expected <BaseKey>-<Arguments>&<QueryString>:<MediaType>");
41+
Assert.AreEqual(String.Format("{0}-{1}&{2}:{3}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), requestUri.Query.Substring(1), mediaType), cacheKey, "Key does not match expected <BaseKey>-<Arguments>&<QueryString>:<MediaType>");
7742
}
7843

7944
[Test]
@@ -83,7 +48,7 @@ public void WithParametersExcludeQueryString_ShouldReturnBaseKeyAndArgumentsAndM
8348
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true);
8449

8550
AssertCacheKeysBasicFormat(cacheKey);
86-
Assert.AreEqual(String.Format("{0}-{1}={2}:{3}", BaseCacheKey, ArgumentKey, ArgumentValue, mediaType), cacheKey, "Key does not match expected <BaseKey>-<Arguments>:<MediaType>");
51+
Assert.AreEqual(String.Format("{0}-{1}:{2}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), mediaType), cacheKey, "Key does not match expected <BaseKey>-<Arguments>:<MediaType>");
8752
}
8853
}
8954
}

test/WebApi.OutputCache.V2.Tests/WebApi.OutputCache.V2.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<ItemGroup>
7575
<Compile Include="CacheKeyGeneratorRegistrationTests.cs" />
7676
<Compile Include="CacheKeyGeneratorTests.cs" />
77+
<Compile Include="CacheKeyGenerationTestsBase.cs" />
7778
<Compile Include="ClientSideTests.cs">
7879
<SubType>Code</SubType>
7980
</Compile>

0 commit comments

Comments
 (0)