Skip to content

Commit 1f3c207

Browse files
author
dmalanij
committed
Tests for DefaultCacheKeyGenerator
Implemented some basic tests for the key generation
1 parent 5100c53 commit 1f3c207

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
[TestFixture]
9+
public class DefaultCacheKeyGeneratorTests
10+
{
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()
47+
{
48+
context.ActionArguments.Add(ArgumentKey, ArgumentValue);
49+
}
50+
51+
[Test]
52+
public void NoParametersIncludeQueryString_ShouldReturnBaseKeyAndQueryStringAndMediaTypeConcatenated()
53+
{
54+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false);
55+
56+
AssertCacheKeysBasicFormat(cacheKey);
57+
Assert.AreEqual(String.Format("{0}-{1}:{2}", BaseCacheKey, requestUri.Query.Substring(1), mediaType), cacheKey, "Key does not match expected <BaseKey>-<QueryString>:<MediaType>");
58+
}
59+
60+
[Test]
61+
public void NoParametersExcludeQueryString_ShouldReturnBaseKeyAndMediaTypeConcatenated()
62+
{
63+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true);
64+
65+
AssertCacheKeysBasicFormat(cacheKey);
66+
Assert.AreEqual(String.Format("{0}:{1}", BaseCacheKey, mediaType), cacheKey, "Key does not match expected <BaseKey>:<MediaType>");
67+
}
68+
69+
[Test]
70+
public void WithParametersIncludeQueryString_ShouldReturnBaseKeyAndArgumentsAndQueryStringAndMediaTypeConcatenated()
71+
{
72+
AddActionArgumentsToContext();
73+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false);
74+
75+
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>");
77+
}
78+
79+
[Test]
80+
public void WithParametersExcludeQueryString_ShouldReturnBaseKeyAndArgumentsAndMediaTypeConcatenated()
81+
{
82+
AddActionArgumentsToContext();
83+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true);
84+
85+
AssertCacheKeysBasicFormat(cacheKey);
86+
Assert.AreEqual(String.Format("{0}-{1}={2}:{3}", BaseCacheKey, ArgumentKey, ArgumentValue, mediaType), cacheKey, "Key does not match expected <BaseKey>-<Arguments>:<MediaType>");
87+
}
88+
}
89+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web.Http;
5+
6+
namespace WebApi.OutputCache.V2.Tests.TestControllers
7+
{
8+
/// <summary>
9+
/// Controller needed for generating the <see cref="System.Web.Http.Controllers.HttpActionContext" /> needed for testing the <see cref="ICacheKeyGenerator"/> implementations
10+
/// </summary>
11+
[RoutePrefix("cacheKeyGeneration")]
12+
public class CacheKeyGenerationController : ApiController
13+
{
14+
private readonly string[] Values = new string[] { "first", "second", "third" };
15+
16+
[Route("")]
17+
public IEnumerable<string> Get([FromUri(Name="filter")]string filterExpression)
18+
{
19+
return String.IsNullOrWhiteSpace(filterExpression) ? Values : Values.Where(x => x.Contains(filterExpression));
20+
}
21+
22+
[Route("{index}")]
23+
public string GetByIndex(int index)
24+
{
25+
return Values[index];
26+
}
27+
}
28+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<Compile Include="ConnegTests.cs">
8484
<SubType>Code</SubType>
8585
</Compile>
86+
<Compile Include="DefaultCacheKeyGeneratorTests.cs" />
8687
<Compile Include="InlineInvalidateTests.cs">
8788
<SubType>Code</SubType>
8889
</Compile>
@@ -95,6 +96,7 @@
9596
<Compile Include="TestControllers\AutoInvalidateController.cs" />
9697
<Compile Include="TestControllers\AutoInvalidateWithTypeController.cs" />
9798
<Compile Include="TestControllers\CacheKeyController.cs" />
99+
<Compile Include="TestControllers\CacheKeyGenerationController.cs" />
98100
<Compile Include="TestControllers\IgnoreController.cs" />
99101
<Compile Include="TestControllers\InlineInvalidateController.cs" />
100102
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)