forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectRouteTestHelpers.cs
More file actions
77 lines (67 loc) · 2.96 KB
/
Copy pathDirectRouteTestHelpers.cs
File metadata and controls
77 lines (67 loc) · 2.96 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Async;
using System.Web.Mvc.Routing;
namespace System.Web.Routing.Test
{
internal static class DirectRouteTestHelpers
{
public static RouteCollectionRoute BuildDirectRouteFromMethod<T>(Expression<Action<T>> methodCall)
{
SubRouteCollection collector = new SubRouteCollection();
AddDirectRouteFromMethod(collector, methodCall);
return new RouteCollectionRoute(collector);
}
public static void AddDirectRouteFromMethod<T>(SubRouteCollection collector, Expression<Action<T>> methodCall)
{
var method = ((MethodCallExpression)methodCall.Body).Method;
var attributes = method.GetCustomAttributes(false).OfType<IRouteInfoProvider>();
var controllerDescriptor = new ReflectedAsyncControllerDescriptor(method.DeclaringType);
var actionDescriptor = new ReflectedActionDescriptor(method, method.Name, controllerDescriptor);
foreach (var attribute in attributes)
{
var subRoute = new Route(attribute.Template, routeHandler: null);
subRoute.SetTargetActionDescriptors(new ActionDescriptor[] { actionDescriptor });
collector.Add(new RouteEntry(null, subRoute));
}
}
public static RouteCollectionRoute BuildDirectRouteFromController<T>()
{
SubRouteCollection collector = new SubRouteCollection();
AddDirectRouteFromController<T>(collector);
return new RouteCollectionRoute(collector);
}
public static void AddDirectRouteFromController<T>(SubRouteCollection collector)
{
var controllerType = typeof(T);
AttributeRoutingMapper.AddRouteEntries(
collector,
new Type[] { controllerType },
new DefaultInlineConstraintResolver(),
new DefaultDirectRouteProvider());
}
public static void AddDirectRouteMatches(this RouteData routeData, Func<RouteBase, RouteData, bool> selector = null)
{
RouteCollectionRoute route = (RouteCollectionRoute)routeData.Route;
List<RouteData> matches = new List<RouteData>();
foreach (var subRoute in route)
{
RouteData match = new RouteData() { Route = subRoute };
bool isMatch = selector == null ? true : selector(subRoute, match);
if (isMatch)
{
matches.Add(match);
}
}
if (matches.Any())
{
routeData.SetDirectRouteMatches(matches);
}
}
}
}