You've already forked linux-packaging-mono
data
docs
eglib
external
Lucene.Net
Newtonsoft.Json
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
Util
AnonymousObject.cs
DictionaryHelper.cs
HttpContextHelpers.cs
MvcHelper.cs
Resolver.cs
SimpleValueProvider.cs
SimpleViewDataContainer.cs
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
cecil
debian-snapshot
ikdasm
ikvm
referencesource
rx
ikvm-native
libgc
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
AUTHORS
COPYING.LIB
ChangeLog.REMOVED.git-id
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
build-mingw32.sh
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-core.spec
mono-core.spec.in
mono-uninstalled.pc.in
test-driver
winconfig.h
177 lines
7.9 KiB
C#
177 lines
7.9 KiB
C#
![]() |
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
|||
|
|
|||
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.IO;
|
|||
|
using System.Web;
|
|||
|
using System.Web.Mvc;
|
|||
|
using System.Web.Routing;
|
|||
|
using Moq;
|
|||
|
|
|||
|
namespace Microsoft.Web.UnitTestUtil
|
|||
|
{
|
|||
|
public static class MvcHelper
|
|||
|
{
|
|||
|
public const string AppPathModifier = "/$(SESSION)";
|
|||
|
|
|||
|
public static HtmlHelper<object> GetHtmlHelper()
|
|||
|
{
|
|||
|
HttpContextBase httpcontext = GetHttpContext("/app/", null, null);
|
|||
|
RouteCollection rt = new RouteCollection();
|
|||
|
rt.Add(new Route("{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
|
|||
|
rt.Add("namedroute", new Route("named/{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
|
|||
|
RouteData rd = new RouteData();
|
|||
|
rd.Values.Add("controller", "home");
|
|||
|
rd.Values.Add("action", "oldaction");
|
|||
|
|
|||
|
ViewDataDictionary vdd = new ViewDataDictionary();
|
|||
|
|
|||
|
ViewContext viewContext = new ViewContext()
|
|||
|
{
|
|||
|
HttpContext = httpcontext,
|
|||
|
RouteData = rd,
|
|||
|
ViewData = vdd
|
|||
|
};
|
|||
|
Mock<IViewDataContainer> mockVdc = new Mock<IViewDataContainer>();
|
|||
|
mockVdc.Setup(vdc => vdc.ViewData).Returns(vdd);
|
|||
|
|
|||
|
HtmlHelper<object> htmlHelper = new HtmlHelper<object>(viewContext, mockVdc.Object, rt);
|
|||
|
return htmlHelper;
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper GetHtmlHelper(string protocol, int port)
|
|||
|
{
|
|||
|
HttpContextBase httpcontext = GetHttpContext("/app/", null, null, protocol, port);
|
|||
|
RouteCollection rt = new RouteCollection();
|
|||
|
rt.Add(new Route("{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
|
|||
|
rt.Add("namedroute", new Route("named/{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
|
|||
|
RouteData rd = new RouteData();
|
|||
|
rd.Values.Add("controller", "home");
|
|||
|
rd.Values.Add("action", "oldaction");
|
|||
|
|
|||
|
ViewDataDictionary vdd = new ViewDataDictionary();
|
|||
|
|
|||
|
Mock<ViewContext> mockViewContext = new Mock<ViewContext>();
|
|||
|
mockViewContext.Setup(c => c.HttpContext).Returns(httpcontext);
|
|||
|
mockViewContext.Setup(c => c.RouteData).Returns(rd);
|
|||
|
mockViewContext.Setup(c => c.ViewData).Returns(vdd);
|
|||
|
Mock<IViewDataContainer> mockVdc = new Mock<IViewDataContainer>();
|
|||
|
mockVdc.Setup(vdc => vdc.ViewData).Returns(vdd);
|
|||
|
|
|||
|
HtmlHelper htmlHelper = new HtmlHelper(mockViewContext.Object, mockVdc.Object, rt);
|
|||
|
return htmlHelper;
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper GetHtmlHelper(ViewDataDictionary viewData)
|
|||
|
{
|
|||
|
Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { CallBase = true };
|
|||
|
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
|
|||
|
mockViewContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
|
|||
|
IViewDataContainer container = GetViewDataContainer(viewData);
|
|||
|
return new HtmlHelper(mockViewContext.Object, container);
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper<TModel> GetHtmlHelper<TModel>(ViewDataDictionary<TModel> viewData)
|
|||
|
{
|
|||
|
Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { CallBase = true };
|
|||
|
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
|
|||
|
mockViewContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
|
|||
|
IViewDataContainer container = GetViewDataContainer(viewData);
|
|||
|
return new HtmlHelper<TModel>(mockViewContext.Object, container);
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper GetHtmlHelperWithPath(ViewDataDictionary viewData)
|
|||
|
{
|
|||
|
return GetHtmlHelperWithPath(viewData, "/");
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper GetHtmlHelperWithPath(ViewDataDictionary viewData, string appPath)
|
|||
|
{
|
|||
|
ViewContext viewContext = GetViewContextWithPath(appPath, viewData);
|
|||
|
Mock<IViewDataContainer> mockContainer = new Mock<IViewDataContainer>();
|
|||
|
mockContainer.Setup(c => c.ViewData).Returns(viewData);
|
|||
|
IViewDataContainer container = mockContainer.Object;
|
|||
|
return new HtmlHelper(viewContext, container, new RouteCollection());
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper<TModel> GetHtmlHelperWithPath<TModel>(ViewDataDictionary<TModel> viewData, string appPath)
|
|||
|
{
|
|||
|
ViewContext viewContext = GetViewContextWithPath(appPath, viewData);
|
|||
|
Mock<IViewDataContainer> mockContainer = new Mock<IViewDataContainer>();
|
|||
|
mockContainer.Setup(c => c.ViewData).Returns(viewData);
|
|||
|
IViewDataContainer container = mockContainer.Object;
|
|||
|
return new HtmlHelper<TModel>(viewContext, container, new RouteCollection());
|
|||
|
}
|
|||
|
|
|||
|
public static HtmlHelper<TModel> GetHtmlHelperWithPath<TModel>(ViewDataDictionary<TModel> viewData)
|
|||
|
{
|
|||
|
return GetHtmlHelperWithPath(viewData, "/");
|
|||
|
}
|
|||
|
|
|||
|
public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod, string protocol, int port)
|
|||
|
{
|
|||
|
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
|
|||
|
|
|||
|
if (!String.IsNullOrEmpty(appPath))
|
|||
|
{
|
|||
|
mockHttpContext.Setup(o => o.Request.ApplicationPath).Returns(appPath);
|
|||
|
}
|
|||
|
if (!String.IsNullOrEmpty(requestPath))
|
|||
|
{
|
|||
|
mockHttpContext.Setup(o => o.Request.AppRelativeCurrentExecutionFilePath).Returns(requestPath);
|
|||
|
}
|
|||
|
|
|||
|
Uri uri;
|
|||
|
|
|||
|
if (port >= 0)
|
|||
|
{
|
|||
|
uri = new Uri(protocol + "://localhost" + ":" + Convert.ToString(port));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
uri = new Uri(protocol + "://localhost");
|
|||
|
}
|
|||
|
mockHttpContext.Setup(o => o.Request.Url).Returns(uri);
|
|||
|
|
|||
|
mockHttpContext.Setup(o => o.Request.PathInfo).Returns(String.Empty);
|
|||
|
if (!String.IsNullOrEmpty(httpMethod))
|
|||
|
{
|
|||
|
mockHttpContext.Setup(o => o.Request.HttpMethod).Returns(httpMethod);
|
|||
|
}
|
|||
|
|
|||
|
mockHttpContext.Setup(o => o.Session).Returns((HttpSessionStateBase)null);
|
|||
|
mockHttpContext.Setup(o => o.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(r => AppPathModifier + r);
|
|||
|
mockHttpContext.Setup(o => o.Items).Returns(new Hashtable());
|
|||
|
return mockHttpContext.Object;
|
|||
|
}
|
|||
|
|
|||
|
public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod)
|
|||
|
{
|
|||
|
return GetHttpContext(appPath, requestPath, httpMethod, Uri.UriSchemeHttp.ToString(), -1);
|
|||
|
}
|
|||
|
|
|||
|
public static ViewContext GetViewContextWithPath(string appPath, ViewDataDictionary viewData)
|
|||
|
{
|
|||
|
HttpContextBase httpContext = GetHttpContext(appPath, "/request", "GET");
|
|||
|
|
|||
|
Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { DefaultValue = DefaultValue.Mock };
|
|||
|
mockViewContext.Setup(c => c.HttpContext).Returns(httpContext);
|
|||
|
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
|
|||
|
mockViewContext.Setup(c => c.Writer).Returns(new StringWriter());
|
|||
|
return mockViewContext.Object;
|
|||
|
}
|
|||
|
|
|||
|
public static ViewContext GetViewContextWithPath(ViewDataDictionary viewData)
|
|||
|
{
|
|||
|
return GetViewContextWithPath("/", viewData);
|
|||
|
}
|
|||
|
|
|||
|
public static IViewDataContainer GetViewDataContainer(ViewDataDictionary viewData)
|
|||
|
{
|
|||
|
Mock<IViewDataContainer> mockContainer = new Mock<IViewDataContainer>();
|
|||
|
mockContainer.Setup(c => c.ViewData).Returns(viewData);
|
|||
|
return mockContainer.Object;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|