Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
Ajax
Async
ExpressionUtil
Html
Properties
Razor
Test
AcceptVerbsAttributeTest.cs
ActionDescriptorTest.cs
ActionExecutedContextTest.cs
ActionExecutingContextTest.cs
ActionFilterAttributeTest.cs
ActionMethodDispatcherCacheTest.cs
ActionMethodDispatcherTest.cs
ActionMethodSelectorTest.cs
ActionNameAttributeTest.cs
AdditionalMetadataAttributeTest.cs
AjaxHelperTest.cs
AjaxHelper`1Test.cs
AjaxRequestExtensionsTest.cs
AllowHtmlAttributeTest.cs
AreaHelpersTest.cs
AreaRegistrationContextTest.cs
AreaRegistrationTest.cs
AssociatedMetadataProviderTest.cs
AssociatedValidatorProviderTest.cs
AsyncControllerTest.cs
AsyncTimeoutAttributeTest.cs
AuthorizationContextTest.cs
AuthorizeAttributeTest.cs
BindAttributeTest.cs
BuildManagerCompiledViewTest.cs
BuildManagerViewEngineTest.cs
ByteArrayModelBinderTest.cs
CachedAssociatedMetadataProviderTest.cs
CachedDataAnnotationsModelMetadataProviderTest.cs
CancellationTokenModelBinderTest.cs
ChildActionOnlyAttributeTest.cs
ChildActionValueProviderFactoryTest.cs
ClientDataTypeModelValidatorProviderTest.cs
CompareAttributeTest.cs
ContentResultTest.cs
ControllerActionInvokerTest.cs.REMOVED.git-id
ControllerBaseTest.cs
ControllerBuilderTest.cs
ControllerContextTest.cs
ControllerDescriptorCacheTest.cs
ControllerDescriptorTest.cs
ControllerInstanceFilterProviderTest.cs
ControllerTest.cs
DataAnnotationsModelMetadataProviderTest.cs
DataAnnotationsModelMetadataProviderTestBase.cs
DataAnnotationsModelValidatorProviderTest.cs
DataAnnotationsModelValidatorTest.cs
DataErrorInfoModelValidatorProviderTest.cs
DataTypeUtilTest.cs
DefaultControllerFactoryTest.cs
DefaultModelBinderTest.cs.REMOVED.git-id
DefaultViewLocationCacheTest.cs
DependencyResolverTest.cs
DescriptorUtilTest.cs
DictionaryHelpersTest.cs
DictionaryValueProviderTest.cs
DynamicViewDataDictionaryTest.cs
EmptyModelValidatorProviderTest.cs
ExceptionContextTest.cs
ExpressionHelperTest.cs
FieldValidationMetadataTest.cs
FileContentResultTest.cs
FilePathResultTest.cs
FileResultTest.cs
FileStreamResultTest.cs
FilterAttributeFilterProviderTest.cs
FilterInfoTest.cs
FilterProviderCollectionTest.cs
FilterProvidersTest.cs
FilterTest.cs
FormCollectionTest.cs
FormContextTest.cs
FormValueProviderFactoryTest.cs
GlobalFilterCollectionTest.cs
HandleErrorAttributeTest.cs
HandleErrorInfoTest.cs
HtmlHelperTest.cs
HtmlHelper`1Test.cs
HttpDeleteAttributeTest.cs
HttpFileCollectionValueProviderFactoryTest.cs
HttpFileCollectionValueProviderTest.cs
HttpGetAttributeTest.cs
HttpHandlerUtilTest.cs
HttpHeadAttributeTest.cs
HttpNotFoundResultTest.cs
HttpOptionsAttributeTest.cs
HttpPatchAttributeTest.cs
HttpPostAttributeTest.cs
HttpPostedFileBaseModelBinderTest.cs
HttpPutAttributeTest.cs
HttpRequestExtensionsTest.cs
HttpStatusCodeResultTest.cs
HttpUnauthorizedResultTest.cs
HttpVerbAttributeHelper.cs
JavaScriptResultTest.cs
JsonResultTest.cs
JsonValueProviderFactoryTest.cs
LinqBinaryModelBinderTest.cs
MockBuildManager.cs
MockHelpers.cs
MockableUnvalidatedRequestValues.cs
ModelBinderAttributeTest.cs
ModelBinderDictionaryTest.cs
ModelBinderProviderCollectionTest.cs
ModelBinderProvidersTest.cs
ModelBindersTest.cs
ModelBindingContextTest.cs
ModelClientValidationRuleTest.cs
ModelErrorCollectionTest.cs
ModelErrorTest.cs
ModelMetadataProvidersTest.cs
ModelMetadataTest.cs
ModelStateDictionaryTest.cs
ModelStateTest.cs
ModelValidationResultTest.cs
ModelValidatorProviderCollectionTest.cs
ModelValidatorProvidersTest.cs
ModelValidatorTest.cs
MultiSelectListTest.cs
MultiServiceResolverTest.cs
MvcHandlerTest.cs
MvcHtmlStringTest.cs
MvcHttpHandlerTest.cs
MvcRouteHandlerTest.cs
MvcTestHelper.cs
MvcWebRazorHostFactoryTest.cs
NameValueCollectionExtensionsTest.cs
NameValueCollectionValueProviderTest.cs
NoAsyncTimeoutAttributeTest.cs
NonActionAttributeTest.cs
OutputCacheAttributeTest.cs
ParameterBindingInfoTest.cs
ParameterDescriptorTest.cs
ParameterInfoUtilTest.cs
PartialViewResultTest.cs
PathHelpersTest.cs
PreApplicationStartCodeTest.cs
QueryStringValueProviderFactoryTest.cs
RangeAttributeAdapterTest.cs
RazorViewEngineTest.cs
RazorViewTest.cs
ReaderWriterCacheTest.cs
RedirectResultTest.cs
RedirectToRouteResultTest.cs
ReflectedActionDescriptorTest.cs
ReflectedControllerDescriptorTest.cs
ReflectedParameterBindingInfoTest.cs
ReflectedParameterDescriptorTest.cs
RegularExpressionAttributeAdapterTest.cs
RemoteAttributeTest.cs
RequireHttpsAttributeTest.cs
RequiredAttributeAdapterTest.cs
ResultExecutedContextTest.cs
ResultExecutingContextTest.cs
RouteCollectionExtensionsTest.cs
RouteDataValueProviderFactoryTest.cs
SelectListTest.cs
SessionStateTempDataProviderTest.cs
SingleServiceResolverTest.cs
StringLengthAttributeAdapterTest.cs
TempDataDictionaryTest.cs
TypeCacheSerializerTest.cs
TypeCacheUtilTest.cs
TypeHelpersTest.cs
UrlHelperTest.cs
UrlParameterTest.cs
UrlRewriterHelperTest.cs
ValidatableObjectAdapterTest.cs
ValidateAntiForgeryTokenAttributeTest.cs
ValidateInputAttributeTest.cs
ValueProviderCollectionTest.cs
ValueProviderDictionaryTest.cs
ValueProviderFactoriesTest.cs
ValueProviderFactoryCollectionTest.cs
ValueProviderResultTest.cs
ValueProviderUtilTest.cs
ViewContextTest.cs
ViewDataDictionaryTest.cs
ViewDataInfoTest.cs
ViewEngineCollectionTest.cs
ViewEngineResultTest.cs
ViewEnginesTest.cs
ViewMasterPageControlBuilderTest.cs
ViewMasterPageTest.cs
ViewPageControlBuilderTest.cs
ViewPageTest.cs
ViewResultBaseTest.cs
ViewResultTest.cs
ViewStartPageTest.cs
ViewTypeParserFilterTest.cs
ViewUserControlControlBuilderTest.cs
ViewUserControlTest.cs
VirtualPathProviderViewEngineTest.cs
WebFormViewEngineTest.cs
WebFormViewTest.cs
Util
System.Web.Mvc.Test.csproj
packages.config
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
ikdasm
ikvm
linker
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
ikvm-native
libgc
llvm
m4
man
mcs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.Mvc.Test/Test/OutputCacheAttributeTest.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

282 lines
9.5 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.TestUtil;
using System.Web.UI;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class OutputCacheAttributeTest
{
[Fact]
public void CacheProfileProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestStringProperty(attr, "CacheProfile", String.Empty);
}
[Fact]
public void CacheSettingsProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute()
{
CacheProfile = "SomeProfile",
Duration = 50,
Location = OutputCacheLocation.Downstream,
NoStore = true,
SqlDependency = "SomeSqlDependency",
VaryByContentEncoding = "SomeContentEncoding",
VaryByCustom = "SomeCustom",
VaryByHeader = "SomeHeader",
VaryByParam = "SomeParam",
};
// Act
OutputCacheParameters cacheSettings = attr.CacheSettings;
// Assert
Assert.Equal("SomeProfile", cacheSettings.CacheProfile);
Assert.Equal(50, cacheSettings.Duration);
Assert.Equal(OutputCacheLocation.Downstream, cacheSettings.Location);
Assert.Equal(true, cacheSettings.NoStore);
Assert.Equal("SomeSqlDependency", cacheSettings.SqlDependency);
Assert.Equal("SomeContentEncoding", cacheSettings.VaryByContentEncoding);
Assert.Equal("SomeCustom", cacheSettings.VaryByCustom);
Assert.Equal("SomeHeader", cacheSettings.VaryByHeader);
Assert.Equal("SomeParam", cacheSettings.VaryByParam);
}
[Fact]
public void DurationProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestInt32Property(attr, "Duration", 10, 20);
}
[Fact]
public void LocationProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestPropertyValue(attr, "Location", OutputCacheLocation.ServerAndClient);
}
[Fact]
public void NoStoreProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestBooleanProperty(attr, "NoStore", false /* initialValue */, false /* testDefaultValue */);
}
[Fact]
public void OnResultExecutingThrowsIfFilterContextIsNull()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
Assert.ThrowsArgumentNull(
delegate { attr.OnResultExecuting(null); }, "filterContext");
}
[Fact]
public void SqlDependencyProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestStringProperty(attr, "SqlDependency", String.Empty);
}
[Fact]
public void VaryByContentEncodingProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestStringProperty(attr, "VaryByContentEncoding", String.Empty);
}
[Fact]
public void VaryByCustomProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestStringProperty(attr, "VaryByCustom", String.Empty);
}
[Fact]
public void VaryByHeaderProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestStringProperty(attr, "VaryByHeader", String.Empty);
}
[Fact]
public void VaryByParamProperty()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
// Act & assert
MemberHelper.TestStringProperty(attr, "VaryByParam", "*");
}
[Fact]
public void OutputCacheDoesNotExecuteIfInChildAction()
{
// Arrange
OutputCacheAttribute attr = new OutputCacheAttribute();
Mock<ResultExecutingContext> context = new Mock<ResultExecutingContext>();
context.Setup(c => c.IsChildAction).Returns(true);
// Act
attr.OnResultExecuting(context.Object);
// Assert
context.Verify();
context.Verify(c => c.Result, Times.Never());
}
// GetChildActionUniqueId
[Fact]
public void GetChildActionUniqueId_ReturnsRepeatableValueForIdenticalContext()
{
// Arrange
var attr = new OutputCacheAttribute();
var context = new MockActionExecutingContext();
// Act
string result1 = attr.GetChildActionUniqueId(context.Object);
string result2 = attr.GetChildActionUniqueId(context.Object);
// Assert
Assert.Equal(result1, result2);
}
[Fact]
public void GetChildActionUniqueId_VariesByActionDescriptorsUniqueId()
{
// Arrange
var attr = new OutputCacheAttribute();
var context1 = new MockActionExecutingContext();
context1.Setup(c => c.ActionDescriptor.UniqueId).Returns("1");
var context2 = new MockActionExecutingContext();
context2.Setup(c => c.ActionDescriptor.UniqueId).Returns("2");
// Act
string result1 = attr.GetChildActionUniqueId(context1.Object);
string result2 = attr.GetChildActionUniqueId(context2.Object);
// Assert
Assert.NotEqual(result1, result2);
}
[Fact]
public void GetChildActionUniqueId_VariesByCustom()
{
// Arrange
var attr = new OutputCacheAttribute { VaryByCustom = "foo" };
var context1 = new MockActionExecutingContext();
context1.Setup(c => c.HttpContext.ApplicationInstance.GetVaryByCustomString(It.IsAny<HttpContext>(), "foo")).Returns("1");
var context2 = new MockActionExecutingContext();
context2.Setup(c => c.HttpContext.ApplicationInstance.GetVaryByCustomString(It.IsAny<HttpContext>(), "foo")).Returns("2");
// Act
string result1 = attr.GetChildActionUniqueId(context1.Object);
string result2 = attr.GetChildActionUniqueId(context2.Object);
// Assert
Assert.NotEqual(result1, result2);
}
[Fact]
public void GetChildActionUniqueId_VariesByActionParameters_AllParametersByDefault()
{
// Arrange
var attr = new OutputCacheAttribute();
var context1 = new MockActionExecutingContext();
context1.ActionParameters["foo"] = "1";
var context2 = new MockActionExecutingContext();
context2.ActionParameters["foo"] = "2";
// Act
string result1 = attr.GetChildActionUniqueId(context1.Object);
string result2 = attr.GetChildActionUniqueId(context2.Object);
// Assert
Assert.NotEqual(result1, result2);
}
[Fact]
public void GetChildActionUniqueId_DoesNotVaryByActionParametersWhenVaryByParamIsNone()
{
// Arrange
var attr = new OutputCacheAttribute { VaryByParam = "none" };
var context1 = new MockActionExecutingContext();
context1.ActionParameters["foo"] = "1";
var context2 = new MockActionExecutingContext();
context2.ActionParameters["foo"] = "2";
// Act
string result1 = attr.GetChildActionUniqueId(context1.Object);
string result2 = attr.GetChildActionUniqueId(context2.Object);
// Assert
Assert.Equal(result1, result2);
}
[Fact]
public void GetChildActionUniqueId_VariesByActionParameters_OnlyVariesByTheGivenParameters()
{
// Arrange
var attr = new OutputCacheAttribute { VaryByParam = "bar" };
var context1 = new MockActionExecutingContext();
context1.ActionParameters["foo"] = "1";
var context2 = new MockActionExecutingContext();
context2.ActionParameters["foo"] = "2";
// Act
string result1 = attr.GetChildActionUniqueId(context1.Object);
string result2 = attr.GetChildActionUniqueId(context2.Object);
// Assert
Assert.Equal(result1, result2);
}
class MockActionExecutingContext : Mock<ActionExecutingContext>
{
public Dictionary<string, object> ActionParameters = new Dictionary<string, object>();
public MockActionExecutingContext()
{
Setup(c => c.ActionDescriptor.UniqueId).Returns("abc123");
Setup(c => c.ActionParameters).Returns(() => ActionParameters);
}
}
}
}