|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | +using System.Security.Principal; |
| 4 | + |
| 5 | +namespace WebApi.OutputCache.V2.Tests |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class PerUserCacheKeyGeneratorTests : CacheKeyGenerationTestsBase<PerUserCacheKeyGenerator> |
| 9 | + { |
| 10 | + private const string UserIdentityName = "SomeUserIDon'tMind"; |
| 11 | + |
| 12 | + [SetUp] |
| 13 | + public override void Setup() |
| 14 | + { |
| 15 | + base.Setup(); |
| 16 | + context.RequestContext.Principal = new GenericPrincipal(new GenericIdentity(UserIdentityName), new string[0]); |
| 17 | + } |
| 18 | + |
| 19 | + protected override PerUserCacheKeyGenerator BuildCacheKeyGenerator() |
| 20 | + { |
| 21 | + return new PerUserCacheKeyGenerator(); |
| 22 | + } |
| 23 | + |
| 24 | + private string FormatUserIdentityForAssertion() |
| 25 | + { |
| 26 | + return UserIdentityName.ToLower(); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void NoParametersIncludeQueryString_ShouldReturnBaseKeyAndQueryStringAndUserIdentityAndMediaTypeConcatenated() |
| 31 | + { |
| 32 | + var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false); |
| 33 | + |
| 34 | + AssertCacheKeysBasicFormat(cacheKey); |
| 35 | + Assert.AreEqual(String.Format("{0}-{1}:{2}:{3}", BaseCacheKey, requestUri.Query.Substring(1), FormatUserIdentityForAssertion(), mediaType), cacheKey, |
| 36 | + "Key does not match expected <BaseKey>-<QueryString>:<UserIdentity>:<MediaType>"); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void NoParametersExcludeQueryString_ShouldReturnBaseKeyAndUserIdentityAndMediaTypeConcatenated() |
| 41 | + { |
| 42 | + var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true); |
| 43 | + |
| 44 | + AssertCacheKeysBasicFormat(cacheKey); |
| 45 | + Assert.AreEqual(String.Format("{0}:{1}:{2}", BaseCacheKey, FormatUserIdentityForAssertion(), mediaType), cacheKey, |
| 46 | + "Key does not match expected <BaseKey>:<UserIdentity>:<MediaType>"); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public void WithParametersIncludeQueryString_ShouldReturnBaseKeyAndArgumentsAndQueryStringAndUserIdentityAndMediaTypeConcatenated() |
| 51 | + { |
| 52 | + AddActionArgumentsToContext(); |
| 53 | + var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false); |
| 54 | + |
| 55 | + AssertCacheKeysBasicFormat(cacheKey); |
| 56 | + Assert.AreEqual(String.Format("{0}-{1}&{2}:{3}:{4}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), requestUri.Query.Substring(1), FormatUserIdentityForAssertion(), mediaType), cacheKey, |
| 57 | + "Key does not match expected <BaseKey>-<Arguments>&<QueryString>:<UserIdentity>:<MediaType>"); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void WithParametersExcludeQueryString_ShouldReturnBaseKeyAndArgumentsAndUserIdentityAndMediaTypeConcatenated() |
| 62 | + { |
| 63 | + AddActionArgumentsToContext(); |
| 64 | + var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true); |
| 65 | + |
| 66 | + AssertCacheKeysBasicFormat(cacheKey); |
| 67 | + Assert.AreEqual(String.Format("{0}-{1}:{2}:{3}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), FormatUserIdentityForAssertion(), mediaType), cacheKey, |
| 68 | + "Key does not match expected <BaseKey>-<Arguments>:<UserIdentity>:<MediaType>"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments