-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathCacheKeyGenerationTestsBase.cs
More file actions
70 lines (60 loc) · 3.06 KB
/
CacheKeyGenerationTestsBase.cs
File metadata and controls
70 lines (60 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using NUnit.Framework;
using System;
using System.Net.Http.Headers;
using System.Web.Http.Controllers;
namespace WebApi.OutputCache.V2.Tests
{
/// <summary>
/// Base class for implementing tests for the generation of cache keys (meaning: implementations of the <see cref="ICacheKeyGenerator"/>
/// </summary>
public abstract class CacheKeyGenerationTestsBase<TCacheKeyGenerator> where TCacheKeyGenerator : ICacheKeyGenerator
{
private const string Argument1Key = "filterExpression1";
private const string Argument1Value = "val";
private const string Argument2WithFromUriAttributeKey = "filterExpression2";
private static readonly ClassArgument Argument2WithFromUriAttributeValue = new ClassArgument() { Value1 = "val1", Value2 = 7 };
protected HttpActionContext context;
protected MediaTypeHeaderValue mediaType;
protected Uri requestUri;
protected TCacheKeyGenerator cacheKeyGenerator;
protected string BaseCacheKey;
private class ClassArgument
{
public string Value1 { get; set; }
public int Value2 { get; set; }
}
[SetUp]
public virtual void Setup()
{
requestUri = new Uri("http://localhost:8080/cacheKeyGeneration?filter=val");
var controllerType = typeof(TestControllers.CacheKeyGenerationController);
var actionMethodInfo = controllerType.GetMethod("Get");
var controllerDescriptor = new HttpControllerDescriptor() { ControllerType = controllerType };
var actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, actionMethodInfo);
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, requestUri.AbsoluteUri);
context = new HttpActionContext(
new HttpControllerContext() { ControllerDescriptor = controllerDescriptor, Request = request },
actionDescriptor
);
mediaType = new MediaTypeHeaderValue("application/json");
BaseCacheKey = new CacheOutputConfiguration(null).MakeBaseCachekey((TestControllers.CacheKeyGenerationController c) => c.Get(String.Empty));
cacheKeyGenerator = BuildCacheKeyGenerator();
}
protected abstract TCacheKeyGenerator BuildCacheKeyGenerator();
protected virtual void AssertCacheKeysBasicFormat(string cacheKey)
{
Assert.IsNotNull(cacheKey);
StringAssert.StartsWith(BaseCacheKey, cacheKey, "Key does not start with BaseKey");
StringAssert.EndsWith(mediaType.ToString(), cacheKey, "Key does not end with MediaType");
}
protected void AddActionArgumentsToContext()
{
context.ActionArguments.Add(Argument1Key, Argument1Value);
context.ActionArguments.Add(Argument2WithFromUriAttributeKey, Argument2WithFromUriAttributeValue);
}
protected string FormatActionArgumentsForKeyAssertion()
{
return String.Format("{0}={1}", Argument1Key, Argument1Value);
}
}
}