public bool Should_be_determined_from_MaxAge_and_cacheControl(int maxAge, bool?noCache, bool?noStore, int?cacheControlMaxAge, bool ignoreRevalidation) { CacheControlHeaderValue cacheControl = null; if (noCache.HasValue || noStore.HasValue || cacheControlMaxAge.HasValue) { cacheControl = new CacheControlHeaderValue { NoCache = noCache ?? false, NoStore = noStore ?? false, }; if (cacheControlMaxAge.HasValue) { cacheControl.MaxAge = TimeSpan.FromSeconds(cacheControlMaxAge.Value); } } var att = new OutputCacheAttributeWithPublicMethods { MaxAge = (uint)maxAge, IgnoreRevalidationRequest = ignoreRevalidation }; // Action return(att.ShouldIgnoreCachePublic(cacheControl, new HttpRequestMessage())); }
public void Should_return_md5_hashed_string() { // Arrange var att = new OutputCacheAttributeWithPublicMethods(); // Action Assert.AreEqual("CE27B55B7504602FB088FEA4DBCEE15A", att.HashCacheKeyPublic("CacheKey")); }
public void Should_return_context_with_webApi_related_keys() { // Arrange var att = new OutputCacheAttributeWithPublicMethods(); var dependencyScope = Substitute.For<IDependencyScope>(); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri("http://localhost/api"), Properties = { { "MS_DependencyScope", dependencyScope } }, //https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/Hosting/HttpPropertyKeys.cs Headers = { }, Content = new StringContent("Content...") }; var actionContext = new HttpActionContext( new HttpControllerContext( new HttpConfiguration(), Substitute.For<IHttpRouteData>(), request), Substitute.For<HttpActionDescriptor>()); // Action var context = att.GetInvocationContextPublic(actionContext); // Assert Assert.AreSame(att, context[Global.__flatwhite_outputcache_attribute]); Assert.AreSame(dependencyScope, context[WebApiExtensions.__webApi_dependency_scope]); Assert.IsTrue((bool)context[WebApiExtensions.__webApi]); Assert.IsNotNull(context["headers"]); Assert.IsNotNull(context["method"]); Assert.IsNotNull(context["requestUri"]); Assert.IsNotNull(context["query"]); }
public void Should_dispose_existing_phoenix() { var key = "theCacheKey" + Guid.NewGuid(); // Arrange var objCacheItem = new WebApiCacheItem { MaxAge = 5, StaleWhileRevalidate = 5, StoreId = 1000, CreatedTime = DateTime.UtcNow.AddSeconds(-5).AddMilliseconds(-1), Key = key }; var existingPhoenix = Substitute.For<WebApiPhoenix>(_invocation, objCacheItem, _request); var att = new OutputCacheAttributeWithPublicMethods {MaxAge = 5, CacheStoreId = 1000, StaleWhileRevalidate = 5}; Global.Cache.PhoenixFireCage[key] = existingPhoenix; // Action att.CreatePhoenixPublic(_invocation, objCacheItem, _request); // Assert Assert.That(Global.Cache.PhoenixFireCage[key] is WebApiPhoenix); existingPhoenix.Received(1).Dispose(); }
public void Should_be_able_to_resolve_by_type_from_scope() { var scop = Substitute.For <IDependencyScope>(); var request = CreateHttpRequestMessage(scop); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var cacheStrategy = Substitute.For <ICacheStrategy>(); scop.GetService(cacheStrategy.GetType()).Returns(cacheStrategy); var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = cacheStrategy.GetType() }; // Action var startegy = att.GetCacheStrategyPublic(request, invocation, invocationContext); // Assert Assert.AreSame(cacheStrategy, startegy); scop.Received(1).GetService(Arg.Is <Type>(t => t == att.CacheStrategyType)); }
public void Should_try_to_resolve_strategy_provider_from_scope_first() { var scope = Substitute.For <IDependencyScope>(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = Substitute.For <ICacheStrategy>().GetType() }; var provider = Substitute.For <ICacheStrategyProvider>(); var expectedStrategy = Substitute.For <ICacheStrategy>(); provider.GetStrategy(Arg.Any <_IInvocation>(), Arg.Any <IDictionary <string, object> >()).Returns(expectedStrategy); scope.GetService(Arg.Is <Type>(t => t == typeof(ICacheStrategyProvider))).Returns(provider); // Action var startegy = att.GetCacheStrategyPublic(scope, invocation, invocationContext); // Assert scope.Received(1).GetService(Arg.Is <Type>(t => t == att.CacheStrategyType)); Assert.That(startegy == expectedStrategy); }
public void Should_return_WebApiInvocation() { // Arrange var att = new OutputCacheAttributeWithPublicMethods(); // Action Assert.That(att.GetInvocationPublic(new HttpActionContext()) is WebApiInvocation); }
public void Should_return_context_with_webApi_related_keys() { // Arrange var att = new OutputCacheAttributeWithPublicMethods(); var dependencyScope = Substitute.For <IDependencyScope>(); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri("http://localhost/api"), Properties = { { "MS_DependencyScope", dependencyScope } }, //https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/Hosting/HttpPropertyKeys.cs Headers = { }, Content = new StringContent("Content...") }; var actionContext = new HttpActionContext( new HttpControllerContext( new HttpConfiguration(), Substitute.For <IHttpRouteData>(), request), Substitute.For <HttpActionDescriptor>()); // Action var context = att.GetInvocationContextPublic(actionContext); // Assert Assert.AreSame(att, context[Global.__flatwhite_outputcache_attribute]); Assert.IsTrue((bool)context[WebApiExtensions.__webApi]); Assert.IsNotNull(context["headers"]); Assert.IsNotNull(context["method"]); Assert.IsNotNull(context["requestUri"]); Assert.IsNotNull(context["query"]); }
public void Should_dispose_existing_phoenix() { var key = "theCacheKey" + Guid.NewGuid(); // Arrange var objCacheItem = new WebApiCacheItem { MaxAge = 5, StaleWhileRevalidate = 5, StoreId = 1000, CreatedTime = DateTime.UtcNow.AddSeconds(-5).AddMilliseconds(-1), Key = key }; var existingPhoenix = Substitute.For <WebApiPhoenix>(_invocation, objCacheItem, _request); var att = new OutputCacheAttributeWithPublicMethods { MaxAge = 5, CacheStoreId = 1000, StaleWhileRevalidate = 5 }; Global.Cache.PhoenixFireCage[key] = existingPhoenix; // Action att.DisposeOldPhoenixAndCreateNew_Public(_invocation, objCacheItem, _request); // Assert Assert.That(Global.Cache.PhoenixFireCage[key] is WebApiPhoenix); existingPhoenix.Received(1).Dispose(); }
public bool Should_be_determined_from_MaxAge_and_cacheControl(int maxAge, bool? noCache, bool? noStore, int? cacheControlMaxAge, bool ignoreRevalidation) { CacheControlHeaderValue cacheControl = null; if (noCache.HasValue || noStore.HasValue || cacheControlMaxAge.HasValue) { cacheControl = new CacheControlHeaderValue { NoCache = noCache ?? false, NoStore = noStore ?? false, }; if (cacheControlMaxAge.HasValue) { cacheControl.MaxAge = TimeSpan.FromSeconds(cacheControlMaxAge.Value); } } var att = new OutputCacheAttributeWithPublicMethods { MaxAge = (uint)maxAge, IgnoreRevalidationRequest = ignoreRevalidation }; // Action return att.ShouldIgnoreCachePublic(cacheControl, new HttpRequestMessage()); }
public void Should_not_create_phoenix_for_http_method_not_GET() { var key = "theCacheKey" + Guid.NewGuid(); // Arrange var objCacheItem = new WebApiCacheItem { MaxAge = 5, StaleWhileRevalidate = 5, StoreId = 1000, CreatedTime = DateTime.UtcNow.AddSeconds(-5).AddMilliseconds(-1), Key = key }; _request.Method = HttpMethod.Post; var att = new OutputCacheAttributeWithPublicMethods { MaxAge = 5, CacheStoreId = 1000, StaleWhileRevalidate = 5 }; // Action att.CreatePhoenixPublic(_invocation, objCacheItem, _request); // Assert Assert.That(!Global.Cache.PhoenixFireCage.ContainsKey(key)); }
public void Should_return_CacheResponseBuilder_by_default() { // Arrange var scope = Substitute.For<IDependencyScope>(); var att = new OutputCacheAttributeWithPublicMethods(); // Action var builder = att.GetCacheResponseBuilderPublic(scope); // Assert Assert.That(builder is CacheResponseBuilder); }
public void Should_return_CacheResponseBuilder_by_default() { // Arrange var scope = Substitute.For <IDependencyScope>(); var att = new OutputCacheAttributeWithPublicMethods(); // Action var builder = att.GetCacheResponseBuilderPublic(scope); // Assert Assert.That(builder is CacheResponseBuilder); }
public void Should_return_true_if_request_property_has_key_telling_no_cache() { var att = new OutputCacheAttributeWithPublicMethods { MaxAge = 10 }; var request = new HttpRequestMessage { Properties = {{$"{WebApiExtensions.__flatwhite_dont_cache}_for_test", true}} }; // Action Assert.IsTrue(att.ShouldIgnoreCachePublic(new CacheControlHeaderValue(), request)); }
public void Should_return_true_if_request_property_has_key_telling_no_cache() { var att = new OutputCacheAttributeWithPublicMethods { MaxAge = 10 }; var request = new HttpRequestMessage { Properties = { { $"{WebApiExtensions.__flatwhite_dont_cache}_for_test", true } } }; // Action Assert.IsTrue(att.ShouldIgnoreCachePublic(new CacheControlHeaderValue(), request)); }
public void Should_be_able_to_resolve_from_scope() { // Arrange var scope = Substitute.For<IDependencyScope>(); var expectedBuilder = Substitute.For<ICacheResponseBuilder>(); scope.GetService(Arg.Is<Type>(t => t == typeof (ICacheResponseBuilder))).Returns(expectedBuilder); var att = new OutputCacheAttributeWithPublicMethods(); // Action var builder = att.GetCacheResponseBuilderPublic(scope); // Assert Assert.That(builder == expectedBuilder); }
public void Should_be_able_to_resolve_from_scope() { // Arrange var scope = Substitute.For <IDependencyScope>(); var expectedBuilder = Substitute.For <ICacheResponseBuilder>(); scope.GetService(Arg.Is <Type>(t => t == typeof(ICacheResponseBuilder))).Returns(expectedBuilder); var att = new OutputCacheAttributeWithPublicMethods(); // Action var builder = att.GetCacheResponseBuilderPublic(scope); // Assert Assert.That(builder == expectedBuilder); }
public void Should_throw_exception_if_cannot_find_cache_strategy() { var request = CreateHttpRequestMessage(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var att = new OutputCacheAttributeWithPublicMethods(); Global.CacheStrategyProvider = Substitute.For <ICacheStrategyProvider>(); Global.CacheStrategyProvider.GetStrategy(Arg.Any <_IInvocation>(), Arg.Any <IDictionary <string, object> >()).Returns(default(ICacheStrategy)); // Action Assert.Throws <Exception>(() => { att.GetCacheStrategyPublic(request, invocation, invocationContext); }); }
public void Should_be_able_to_resolve_from_provider_when_CacheStrategyType_not_available() { var request = CreateHttpRequestMessage(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var att = new OutputCacheAttributeWithPublicMethods(); // Action var startegy = att.GetCacheStrategyPublic(request, invocation, invocationContext); // Assert Assert.IsInstanceOf <WebApiCacheStrategy>(startegy); }
public void Should_return_WebApiCacheStrategy_by_default() { // Arrange var scope = Substitute.For <IDependencyScope>(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var att = new OutputCacheAttributeWithPublicMethods(); // Action var startegy = att.GetCacheStrategyPublic(scope, invocation, invocationContext); // Assert Assert.That(startegy is WebApiCacheStrategy); }
public void Should_return_WebApiCacheStrategy_by_default() { // Arrange var scope = Substitute.For<IDependencyScope>(); var invocation = Substitute.For<_IInvocation>(); var invocationContext = new Dictionary<string, object> { {WebApiExtensions.__webApi, true} }; var att = new OutputCacheAttributeWithPublicMethods(); // Action var startegy = att.GetCacheStrategyPublic(scope, invocation, invocationContext); // Assert Assert.That(startegy is WebApiCacheStrategy); }
public void Should_throw_if_cannot_resolve_srategy_from_scope() { var request = CreateHttpRequestMessage(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var cacheStrategy = Substitute.For <ICacheStrategy>(); var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = cacheStrategy.GetType() }; // Action Assert.Throws <Exception>(() => { att.GetCacheStrategyPublic(request, invocation, invocationContext); }); }
public void Should_try_to_resolve_by_type() { var scope = Substitute.For <IDependencyScope>(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = Substitute.For <ICacheStrategy>().GetType() }; // Action var startegy = att.GetCacheStrategyPublic(scope, invocation, invocationContext); // Assert scope.Received(1).GetService(Arg.Is <Type>(t => t == att.CacheStrategyType)); Assert.That(startegy is WebApiCacheStrategy); }
public void Should_try_to_resolve_by_type() { var scope = Substitute.For<IDependencyScope>(); var invocation = Substitute.For<_IInvocation>(); var invocationContext = new Dictionary<string, object> { {WebApiExtensions.__webApi, true} }; var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = Substitute.For<ICacheStrategy>().GetType() }; // Action var startegy = att.GetCacheStrategyPublic(scope, invocation, invocationContext); // Assert scope.Received(1).GetService(Arg.Is<Type>(t => t == att.CacheStrategyType)); Assert.That(startegy is WebApiCacheStrategy); }
public void Should_throw_if_cannot_resolve_srategy() { var scope = Substitute.For <IDependencyScope>(); var invocation = Substitute.For <_IInvocation>(); var invocationContext = new Dictionary <string, object> { { WebApiExtensions.__webApi, true } }; var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = Substitute.For <ICacheStrategy>().GetType() }; var provider = Substitute.For <ICacheStrategyProvider>(); provider.GetStrategy(Arg.Any <_IInvocation>(), Arg.Any <IDictionary <string, object> >()).Returns((ICacheStrategy)null); scope.GetService(Arg.Is <Type>(t => t == typeof(ICacheStrategyProvider))).Returns(provider); // Action Assert.Throws <Exception>(() => { att.GetCacheStrategyPublic(scope, invocation, invocationContext); }); }
public void Should_return_WebApiCacheStrategy_by_default() { // Arrange var att = new OutputCacheAttributeWithPublicMethods { MaxAge = 1, SMaxAge = 2, MustRevalidate = true, ProxyRevalidate = true, Private = true, Public = true, NoStore = true, NoCache = true, NoTransform = true }; var request = new HttpRequestMessage(); var response = new HttpResponseMessage(HttpStatusCode.OK); // Action att.ApplyCacheHeadersPublic(response, request); var control = response.Headers.CacheControl; // Assert Assert.AreEqual(1, control.MaxAge.Value.Seconds); Assert.AreEqual(2, control.SharedMaxAge.Value.Seconds); Assert.IsTrue(control.MustRevalidate); Assert.IsTrue(control.ProxyRevalidate); Assert.IsTrue(control.Private); Assert.IsTrue(control.Public); Assert.IsTrue(control.NoCache); Assert.IsTrue(control.NoStore); Assert.IsTrue(control.NoTransform); Assert.AreEqual("no-cache", response.Headers.Pragma.ToString()); }
public void Should_throw_if_cannot_resolve_srategy() { var scope = Substitute.For<IDependencyScope>(); var invocation = Substitute.For<_IInvocation>(); var invocationContext = new Dictionary<string, object> { {WebApiExtensions.__webApi, true} }; var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = Substitute.For<ICacheStrategy>().GetType() }; var provider = Substitute.For<ICacheStrategyProvider>(); provider.GetStrategy(Arg.Any<_IInvocation>(), Arg.Any<IDictionary<string, object>>()).Returns((ICacheStrategy)null); scope.GetService(Arg.Is<Type>(t => t == typeof(ICacheStrategyProvider))).Returns(provider); // Action Assert.Throws<Exception>(() => { att.GetCacheStrategyPublic(scope, invocation, invocationContext); }); }
public void Should_try_to_resolve_strategy_provider_from_scope_first() { var scope = Substitute.For<IDependencyScope>(); var invocation = Substitute.For<_IInvocation>(); var invocationContext = new Dictionary<string, object> { {WebApiExtensions.__webApi, true} }; var att = new OutputCacheAttributeWithPublicMethods { CacheStrategyType = Substitute.For<ICacheStrategy>().GetType() }; var provider = Substitute.For<ICacheStrategyProvider>(); var expectedStrategy = Substitute.For<ICacheStrategy>(); provider.GetStrategy(Arg.Any<_IInvocation>(), Arg.Any<IDictionary<string, object>>()).Returns(expectedStrategy); scope.GetService(Arg.Is<Type>(t => t == typeof(ICacheStrategyProvider))).Returns(provider); // Action var startegy = att.GetCacheStrategyPublic(scope, invocation, invocationContext); // Assert scope.Received(1).GetService(Arg.Is<Type>(t => t == att.CacheStrategyType)); Assert.That(startegy == expectedStrategy); }