-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from filipw/dev
Sync master with latest dev
- Loading branch information
Showing
11 changed files
with
274 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Net.Http.Headers; | ||
using System.Web.Http.Controllers; | ||
|
||
namespace WebApi.OutputCache.V2 | ||
{ | ||
public class PerUserCacheKeyGenerator : DefaultCacheKeyGenerator | ||
{ | ||
public override string MakeCacheKey(HttpActionContext context, MediaTypeHeaderValue mediaType, bool excludeQueryString = false) | ||
{ | ||
var baseKey = MakeBaseKey(context); | ||
var parameters = FormatParameters(context, excludeQueryString); | ||
var userIdentity = FormatUserIdentity(context); | ||
|
||
return string.Format("{0}{1}:{2}:{3}", baseKey, parameters, userIdentity, mediaType); | ||
} | ||
|
||
protected virtual string FormatUserIdentity(HttpActionContext context) | ||
{ | ||
return context.RequestContext.Principal.Identity.Name.ToLower(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
test/WebApi.OutputCache.V2.Tests/CacheKeyGenerationTestsBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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 ArgumentKey = "filterExpression"; | ||
private const string ArgumentValue = "val"; | ||
protected HttpActionContext context; | ||
protected MediaTypeHeaderValue mediaType; | ||
protected Uri requestUri; | ||
protected TCacheKeyGenerator cacheKeyGenerator; | ||
protected string BaseCacheKey; | ||
|
||
[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(ArgumentKey, ArgumentValue); | ||
} | ||
|
||
protected string FormatActionArgumentsForKeyAssertion() | ||
{ | ||
return String.Format("{0}={1}", ArgumentKey, ArgumentValue); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
test/WebApi.OutputCache.V2.Tests/DefaultCacheKeyGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using NUnit.Framework; | ||
using System; | ||
|
||
namespace WebApi.OutputCache.V2.Tests | ||
{ | ||
[TestFixture] | ||
public class DefaultCacheKeyGeneratorTests : CacheKeyGenerationTestsBase<DefaultCacheKeyGenerator> | ||
{ | ||
protected override DefaultCacheKeyGenerator BuildCacheKeyGenerator() | ||
{ | ||
return new DefaultCacheKeyGenerator(); | ||
} | ||
|
||
[Test] | ||
public void NoParametersIncludeQueryString_ShouldReturnBaseKeyAndQueryStringAndMediaTypeConcatenated() | ||
{ | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}-{1}:{2}", BaseCacheKey, requestUri.Query.Substring(1), mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>-<QueryString>:<MediaType>"); | ||
} | ||
|
||
[Test] | ||
public void NoParametersExcludeQueryString_ShouldReturnBaseKeyAndMediaTypeConcatenated() | ||
{ | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}:{1}", BaseCacheKey, mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>:<MediaType>"); | ||
} | ||
|
||
[Test] | ||
public void WithParametersIncludeQueryString_ShouldReturnBaseKeyAndArgumentsAndQueryStringAndMediaTypeConcatenated() | ||
{ | ||
AddActionArgumentsToContext(); | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
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>"); | ||
} | ||
|
||
[Test] | ||
public void WithParametersExcludeQueryString_ShouldReturnBaseKeyAndArgumentsAndMediaTypeConcatenated() | ||
{ | ||
AddActionArgumentsToContext(); | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}-{1}:{2}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>-<Arguments>:<MediaType>"); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
test/WebApi.OutputCache.V2.Tests/PerUserCacheKeyGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Security.Principal; | ||
|
||
namespace WebApi.OutputCache.V2.Tests | ||
{ | ||
[TestFixture] | ||
public class PerUserCacheKeyGeneratorTests : CacheKeyGenerationTestsBase<PerUserCacheKeyGenerator> | ||
{ | ||
private const string UserIdentityName = "SomeUserIDon'tMind"; | ||
|
||
[SetUp] | ||
public override void Setup() | ||
{ | ||
base.Setup(); | ||
context.RequestContext.Principal = new GenericPrincipal(new GenericIdentity(UserIdentityName), new string[0]); | ||
} | ||
|
||
protected override PerUserCacheKeyGenerator BuildCacheKeyGenerator() | ||
{ | ||
return new PerUserCacheKeyGenerator(); | ||
} | ||
|
||
private string FormatUserIdentityForAssertion() | ||
{ | ||
return UserIdentityName.ToLower(); | ||
} | ||
|
||
[Test] | ||
public void NoParametersIncludeQueryString_ShouldReturnBaseKeyAndQueryStringAndUserIdentityAndMediaTypeConcatenated() | ||
{ | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}-{1}:{2}:{3}", BaseCacheKey, requestUri.Query.Substring(1), FormatUserIdentityForAssertion(), mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>-<QueryString>:<UserIdentity>:<MediaType>"); | ||
} | ||
|
||
[Test] | ||
public void NoParametersExcludeQueryString_ShouldReturnBaseKeyAndUserIdentityAndMediaTypeConcatenated() | ||
{ | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}:{1}:{2}", BaseCacheKey, FormatUserIdentityForAssertion(), mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>:<UserIdentity>:<MediaType>"); | ||
} | ||
|
||
[Test] | ||
public void WithParametersIncludeQueryString_ShouldReturnBaseKeyAndArgumentsAndQueryStringAndUserIdentityAndMediaTypeConcatenated() | ||
{ | ||
AddActionArgumentsToContext(); | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}-{1}&{2}:{3}:{4}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), requestUri.Query.Substring(1), FormatUserIdentityForAssertion(), mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>-<Arguments>&<QueryString>:<UserIdentity>:<MediaType>"); | ||
} | ||
|
||
[Test] | ||
public void WithParametersExcludeQueryString_ShouldReturnBaseKeyAndArgumentsAndUserIdentityAndMediaTypeConcatenated() | ||
{ | ||
AddActionArgumentsToContext(); | ||
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true); | ||
|
||
AssertCacheKeysBasicFormat(cacheKey); | ||
Assert.AreEqual(String.Format("{0}-{1}:{2}:{3}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), FormatUserIdentityForAssertion(), mediaType), cacheKey, | ||
"Key does not match expected <BaseKey>-<Arguments>:<UserIdentity>:<MediaType>"); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
test/WebApi.OutputCache.V2.Tests/TestControllers/CacheKeyGenerationController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Http; | ||
|
||
namespace WebApi.OutputCache.V2.Tests.TestControllers | ||
{ | ||
/// <summary> | ||
/// Controller needed for generating the <see cref="System.Web.Http.Controllers.HttpActionContext" /> needed for testing the <see cref="ICacheKeyGenerator"/> implementations | ||
/// </summary> | ||
[RoutePrefix("cacheKeyGeneration")] | ||
public class CacheKeyGenerationController : ApiController | ||
{ | ||
private readonly string[] Values = new string[] { "first", "second", "third" }; | ||
|
||
[Route("")] | ||
public IEnumerable<string> Get([FromUri(Name="filter")]string filterExpression) | ||
{ | ||
return String.IsNullOrWhiteSpace(filterExpression) ? Values : Values.Where(x => x.Contains(filterExpression)); | ||
} | ||
|
||
[Route("{index}")] | ||
public string GetByIndex(int index) | ||
{ | ||
return Values[index]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters