Files
Umbraco-CMS/tests/Umbraco.Tests/Routing/RouteTestExtensions.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

59 lines
1.9 KiB
C#

using System.Web;
using System.Web.Routing;
using Moq;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Routing
{
public static class RouteTestExtensions
{
/// <summary>
/// Return the route data for the URL based on a mocked context
/// </summary>
/// <param name="routes"></param>
/// <param name="requestUrl"></param>
/// <returns></returns>
public static RouteData GetDataForRoute(this RouteCollection routes, string requestUrl)
{
var context = new FakeHttpContextFactory(requestUrl);
return routes.GetDataForRoute(context.HttpContext);
}
/// <summary>
/// Get the route data based on the URL and HTTP context
/// </summary>
/// <param name="routes"></param>
/// <param name="httpContext"></param>
/// <returns></returns>
public static RouteData GetDataForRoute(this RouteCollection routes, HttpContextBase httpContext)
{
var data = routes.GetRouteData(httpContext);
//set the route data on the request context
var requestMock = Mock.Get(httpContext.Request.RequestContext);
requestMock.Setup(x => x.RouteData).Returns(data);
return data;
}
/// <summary>
/// Checks if the URL will be ignored in the RouteTable
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
/// <remarks>
/// MVCContrib has a similar one but is faulty:
/// http://mvccontrib.codeplex.com/workitem/7173
/// </remarks>
public static bool ShouldIgnoreRoute(this string url)
{
var http = new FakeHttpContextFactory(url);
var r = RouteTable.Routes.GetRouteData(http.HttpContext);
if (r == null) return false;
return (r.RouteHandler is StopRoutingHandler);
}
}
}