forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostedUrlHelperTest.cs
More file actions
183 lines (151 loc) · 8.02 KB
/
Copy pathHostedUrlHelperTest.cs
File metadata and controls
183 lines (151 loc) · 8.02 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
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
// 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.Net.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.TestCommon;
using Moq;
using UrlHelper = System.Web.Http.Routing.UrlHelper;
namespace System.Web.Http.WebHost.Routing
{
public class HostedUrlHelperTest
{
[Theory]
[InlineData(WhichRoute.ApiRoute1)]
[InlineData(WhichRoute.ApiRoute2)]
[InlineData(WhichRoute.WebRoute1)]
public void UrlHelper_GeneratesApiUrl_ForMatchingData(WhichRoute whichRoute)
{
// Mixed mode app with Web API generating URLs to other APIs
var url = GetUrlHelperForMixedApp(whichRoute);
string generatedUrl = url.Route("apiroute2", new { controller = "something", action = "someaction", id = 789 });
Assert.Equal("$APP$/SOMEAPP/api/something/someaction", generatedUrl);
}
[Theory]
[InlineData(WhichRoute.ApiRoute1)]
[InlineData(WhichRoute.ApiRoute2)]
[InlineData(WhichRoute.WebRoute1)]
public void UrlHelper_GeneratesNonApiUrl_ForMatchingData(WhichRoute whichRoute)
{
// Mixed mode app with Web API generating URLs to other non-API routes
var url = GetUrlHelperForMixedApp(whichRoute);
string generatedUrl = url.Route("webroute1", new { controller = "something", action = "someaction", id = 789 });
Assert.Equal("$APP$/SOMEAPP/something/someaction/789", generatedUrl);
}
[Theory]
[InlineData(WhichRoute.ApiRoute1)]
[InlineData(WhichRoute.ApiRoute2)]
[InlineData(WhichRoute.WebRoute1)]
public void UrlHelper_GeneratesNullUrl_ForDataNotMatching(WhichRoute whichRoute)
{
// Mixed mode app with Web API generating URLs to other non-API routes
var url = GetUrlHelperForMixedApp(whichRoute);
string generatedUrl = url.Route("webroute1", new { foo = "bar" });
Assert.Null(generatedUrl);
}
[Theory]
[InlineData(WhichRoute.ApiRoute1)]
[InlineData(WhichRoute.ApiRoute2)]
[InlineData(WhichRoute.WebRoute1)]
public void UrlHelper_SkipsApiRoutesAndMatchesMvcUrl_ForMatchingData(WhichRoute whichRoute)
{
// Mixed mode app with MVC generating URLs to other MVC URLs
RouteCollection routes;
RequestContext requestContext;
var url = GetUrlHelperForMixedApp(whichRoute, out routes, out requestContext);
// Note: This is generating a URL the "hard" way because it's simulating what a regular MVC
// app would do when generating a URL. If we went through the Web API functionality it wouldn't
// be testing what would really happen in a mixed app.
VirtualPathData virtualPathData = routes.GetVirtualPath(requestContext, new RouteValueDictionary(new { controller = "something", action = "someaction", id = 789 }));
Assert.NotNull(virtualPathData);
string generatedUrl = virtualPathData.VirtualPath;
Assert.Equal("$APP$/SOMEAPP/something/someaction/789", generatedUrl);
}
[Theory]
[InlineData(WhichRoute.ApiRoute1)]
[InlineData(WhichRoute.ApiRoute2)]
[InlineData(WhichRoute.WebRoute1)]
public void UrlHelper_MvcAppGeneratesApiRoute_WithSpecialHttpRouteKey(WhichRoute whichRoute)
{
// Mixed mode app with MVC generating URLs to Web APIs
RouteCollection routes;
RequestContext requestContext;
var url = GetUrlHelperForMixedApp(whichRoute, out routes, out requestContext);
// Note: This is generating a URL the "hard" way because it's simulating what a regular MVC
// app would do when generating a URL. If we went through the Web API functionality it wouldn't
// be testing what would really happen in a mixed app.
VirtualPathData virtualPathData = routes.GetVirtualPath(requestContext, new RouteValueDictionary(new { controller = "something", action = "someotheraction", id = 789, httproute = true }));
Assert.NotNull(virtualPathData);
string generatedUrl = virtualPathData.VirtualPath;
Assert.Equal("$APP$/SOMEAPP/api/something/someotheraction", generatedUrl);
}
[Theory]
[InlineData("httproute")]
[InlineData("httpRoute")]
[InlineData("HTTPROUTE")]
[InlineData("hTtProuTE")]
public void UrlHelper_DoesntAddDuplicateHttpRoute_ForHttpRouteInDifferentCasing(string httpRoute)
{
// Mixed mode app with MVC generating URLs to Web APIs
RouteCollection routes;
RequestContext requestContext;
var url = GetUrlHelperForMixedApp(WhichRoute.ApiRoute1, out routes, out requestContext);
string generatedUrl = url.Route("apiroute1", new Dictionary<string, object> { { "controller", "something" }, { "id", 789 }, { httpRoute, true } });
Assert.Equal("$APP$/SOMEAPP/api/something/789", generatedUrl);
}
private static UrlHelper GetUrlHelperForMixedApp(WhichRoute whichRoute)
{
RouteCollection routes;
RequestContext requestContext;
return GetUrlHelperForMixedApp(whichRoute, out routes, out requestContext);
}
private static UrlHelper GetUrlHelperForMixedApp(WhichRoute whichRoute, out RouteCollection routes, out RequestContext requestContext)
{
routes = new RouteCollection();
HttpRequestMessage request = new HttpRequestMessage();
var mockHttpContext = new Mock<HttpContextBase>();
var mockHttpRequest = new Mock<HttpRequestBase>();
mockHttpRequest.SetupGet<string>(x => x.ApplicationPath).Returns("/SOMEAPP/");
var mockHttpResponse = new Mock<HttpResponseBase>();
mockHttpResponse.Setup<string>(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(x => "$APP$" + x);
mockHttpContext.SetupGet<HttpRequestBase>(x => x.Request).Returns(mockHttpRequest.Object);
mockHttpContext.SetupGet<HttpResponseBase>(x => x.Response).Returns(mockHttpResponse.Object);
request.Properties["MS_HttpContext"] = mockHttpContext.Object;
// Set up routes
var hostedRoutes = new HostedHttpRouteCollection(routes);
Route apiRoute1 = routes.MapHttpRoute("apiroute1", "api/{controller}/{id}", new { action = "someaction" });
Route apiRoute2 = routes.MapHttpRoute("apiroute2", "api/{controller}/{action}", new { id = 789 });
Route webRoute1 = routes.MapRoute("webroute1", "{controller}/{action}/{id}");
request.SetConfiguration(new HttpConfiguration(hostedRoutes));
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "people");
routeData.Values.Add("id", "123");
// Specify which route we came in on (e.g. what request matching the incoming URL) because
// it can affect the generated URL due to the ambient route data.
switch (whichRoute)
{
case WhichRoute.ApiRoute1:
routeData.Route = apiRoute1;
break;
case WhichRoute.ApiRoute2:
routeData.Route = apiRoute2;
break;
case WhichRoute.WebRoute1:
routeData.Route = webRoute1;
break;
default:
throw new ArgumentException("Invalid route specified.", "whichRoute");
}
request.SetRouteData(new HostedHttpRouteData(routeData));
requestContext = new RequestContext(mockHttpContext.Object, routeData);
return request.GetUrlHelper();
}
public enum WhichRoute
{
ApiRoute1,
ApiRoute2,
WebRoute1,
}
}
}