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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Web.Mvc.Properties;
using System.Web.Routing;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Html.Test
{
public class ChildActionExtensionsTest
{
Mock<HtmlHelper> htmlHelper;
Mock<HttpContextBase> httpContext;
Mock<RouteBase> route;
Mock<IViewDataContainer> viewDataContainer;
RouteData originalRouteData;
RouteCollection routes;
ViewContext viewContext;
VirtualPathData virtualPathData;
public ChildActionExtensionsTest()
{
route = new Mock<RouteBase>();
route.Setup(r => r.GetVirtualPath(It.IsAny<RequestContext>(), It.IsAny<RouteValueDictionary>()))
.Returns(() => virtualPathData);
virtualPathData = new VirtualPathData(route.Object, "~/VirtualPath");
routes = new RouteCollection();
routes.Add(route.Object);
originalRouteData = new RouteData();
string returnValue = "";
httpContext = new Mock<HttpContextBase>();
httpContext.Setup(hc => hc.Request.ApplicationPath).Returns("~");
httpContext.Setup(hc => hc.Response.ApplyAppPathModifier(It.IsAny<string>()))
.Callback<string>(s => returnValue = s)
.Returns(() => returnValue);
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()));
viewContext = new ViewContext
{
RequestContext = new RequestContext(httpContext.Object, originalRouteData)
};
viewDataContainer = new Mock<IViewDataContainer>();
htmlHelper = new Mock<HtmlHelper>(viewContext, viewDataContainer.Object, routes);
}
[Fact]
public void GuardClauses()
{
// Act & Assert
Assert.ThrowsArgumentNull(
() => ChildActionExtensions.ActionHelper(null /* htmlHelper */, "abc", null /* controllerName */, null /* routeValues */, null /* textWriter */),
"htmlHelper"
);
Assert.ThrowsArgumentNullOrEmpty(
() => ChildActionExtensions.ActionHelper(htmlHelper.Object, null /* actionName */, null /* controllerName */, null /* routeValues */, null /* textWriter */),
"actionName"
);
Assert.ThrowsArgumentNullOrEmpty(
() => ChildActionExtensions.ActionHelper(htmlHelper.Object, String.Empty /* actionName */, null /* controllerName */, null /* routeValues */, null /* textWriter */),
"actionName"
);
}
[Fact]
public void ServerExecuteCalledWithWrappedChildActionMvcHandler()
{
// Arrange
IHttpHandler callbackHandler = null;
TextWriter callbackTextWriter = null;
bool callbackPreserveForm = false;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>(
(handler, textWriter, preserveForm) =>
{
callbackHandler = handler;
callbackTextWriter = textWriter;
callbackPreserveForm = preserveForm;
});
TextWriter stringWriter = new StringWriter();
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, stringWriter);
// Assert
Assert.NotNull(callbackHandler);
HttpHandlerUtil.ServerExecuteHttpHandlerWrapper wrapper = callbackHandler as HttpHandlerUtil.ServerExecuteHttpHandlerWrapper;
Assert.NotNull(wrapper);
Assert.NotNull(wrapper.InnerHandler);
ChildActionExtensions.ChildActionMvcHandler childHandler = wrapper.InnerHandler as ChildActionExtensions.ChildActionMvcHandler;
Assert.NotNull(childHandler);
Assert.Same(stringWriter, callbackTextWriter);
Assert.True(callbackPreserveForm);
}
[Fact]
public void RouteDataTokensIncludesParentActionViewContext()
{
// Arrange
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */);
// Assert
Assert.Same(viewContext, mvcHandler.RequestContext.RouteData.DataTokens[ControllerContext.ParentActionViewContextToken]);
}
[Fact]
public void RouteValuesIncludeNewActionName()
{
// Arrange
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.Equal("actionName", routeData.Values["action"]);
}
[Fact]
public void RouteValuesIncludeOldControllerNameWhenControllerNameIsNullOrEmpty()
{
// Arrange
originalRouteData.Values["controller"] = "oldController";
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.Equal("oldController", routeData.Values["controller"]);
}
[Fact]
public void RouteValuesIncludeNewControllerNameWhenControllNameIsNotEmpty()
{
// Arrange
originalRouteData.Values["controller"] = "oldController";
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", "newController", null /* routeValues */, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.Equal("newController", routeData.Values["controller"]);
}
[Fact]
public void PassedRouteValuesOverrideParentRequestRouteValues()
{
// Arrange
originalRouteData.Values["name1"] = "value1";
originalRouteData.Values["name2"] = "value2";
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, new RouteValueDictionary { { "name2", "newValue2" } }, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.Equal("value1", routeData.Values["name1"]);
Assert.Equal("newValue2", routeData.Values["name2"]);
Assert.Equal("newValue2", (routeData.Values[ChildActionValueProvider.ChildActionValuesKey] as DictionaryValueProvider<object>).GetValue("name2").RawValue);
}
[Fact]
public void NoChildActionValuesDictionaryCreatedIfNoRouteValuesPassed()
{
// Arrange
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.Null(routeData.Values[ChildActionValueProvider.ChildActionValuesKey]);
}
[Fact]
public void RouteValuesDoesNotIncludeExplicitlyPassedAreaName()
{
// Arrange
Route route = routes.MapRoute("my-area", "my-area");
route.DataTokens["area"] = "myArea";
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, new RouteValueDictionary { { "area", "myArea" } }, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.False(routeData.Values.ContainsKey("area"));
Assert.Null((routeData.Values[ChildActionValueProvider.ChildActionValuesKey] as DictionaryValueProvider<object>).GetValue("area"));
}
[Fact]
public void RouteValuesIncludeExplicitlyPassedAreaNameIfAreasNotInUse()
{
// Arrange
Route route = routes.MapRoute("my-area", "my-area");
MvcHandler mvcHandler = null;
httpContext.Setup(hc => hc.Server.Execute(It.IsAny<IHttpHandler>(), It.IsAny<TextWriter>(), It.IsAny<bool>()))
.Callback<IHttpHandler, TextWriter, bool>((handler, _, __) => mvcHandler = (MvcHandler)((HttpHandlerUtil.ServerExecuteHttpHandlerWrapper)handler).InnerHandler);
// Act
ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, new RouteValueDictionary { { "area", "myArea" } }, null /* textWriter */);
// Assert
RouteData routeData = mvcHandler.RequestContext.RouteData;
Assert.True(routeData.Values.ContainsKey("area"));
Assert.Equal("myArea", (routeData.Values[ChildActionValueProvider.ChildActionValuesKey] as DictionaryValueProvider<object>).GetValue("area").RawValue);
}
[Fact]
public void NoMatchingRouteThrows()
{
// Arrange
routes.Clear();
// Act & Assert
Assert.Throws<InvalidOperationException>(
() => ChildActionExtensions.ActionHelper(htmlHelper.Object, "actionName", null /* controllerName */, null /* routeValues */, null /* textWriter */),
MvcResources.Common_NoRouteMatched
);
}
}
}
|