forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiControllerActionSelectorTest.cs
More file actions
304 lines (261 loc) · 18 KB
/
Copy pathApiControllerActionSelectorTest.cs
File metadata and controls
304 lines (261 loc) · 18 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 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.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ValueProviders;
using Microsoft.TestCommon;
namespace System.Web.Http
{
[CLSCompliant(false)]
public class ApiControllerActionSelectorTest
{
[Theory]
[InlineData("GET", "Test", "GetUsers")]
[InlineData("GET", "Test/2", "GetUser")]
[InlineData("GET", "Test/3?name=mario", "GetUserByNameAndId")]
[InlineData("GET", "Test/3?name=mario&ssn=123456", "GetUserByNameIdAndSsn")]
[InlineData("GET", "Test?name=mario&ssn=123456", "GetUserByNameAndSsn")]
[InlineData("GET", "Test?name=mario&ssn=123456&age=3", "GetUserByNameAgeAndSsn")]
[InlineData("GET", "Test/5?random=9", "GetUser")]
[InlineData("Post", "Test", "PostUser")]
[InlineData("Post", "Test?name=mario&age=10", "PostUserByNameAndAge")]
// Note: Normally the following would not match DeleteUserByIdAndOptName because it has 'id' and 'age' as parameters while the DeleteUserByIdAndOptName action has 'id' and 'name'.
// However, because the default value is provided on action parameter 'name', having the 'id' in the request was enough to match the action.
[InlineData("Delete", "Test/6?age=10", "DeleteUserByIdAndOptName")]
[InlineData("Delete", "Test", "DeleteUserByOptName")]
[InlineData("Delete", "Test?name=user", "DeleteUserByOptName")]
[InlineData("Delete", "Test/6?email=user@test.com", "DeleteUserById_Email_OptName_OptPhone")]
[InlineData("Delete", "Test/6?email=user@test.com&name=user", "DeleteUserById_Email_OptName_OptPhone")]
[InlineData("Delete", "Test/6?email=user@test.com&name=user&phone=123456789", "DeleteUserById_Email_OptName_OptPhone")]
[InlineData("Delete", "Test/6?email=user@test.com&height=1.8", "DeleteUserById_Email_Height_OptName_OptPhone")]
[InlineData("Delete", "Test/6?email=user@test.com&height=1.8&name=user", "DeleteUserById_Email_Height_OptName_OptPhone")]
[InlineData("Delete", "Test/6?email=user@test.com&height=1.8&name=user&phone=12345678", "DeleteUserById_Email_Height_OptName_OptPhone")]
[InlineData("Head", "Test/6", "Head_Id_OptSize_OptIndex")]
[InlineData("Head", "Test/6?size=2", "Head_Id_OptSize_OptIndex")]
[InlineData("Head", "Test/6?index=2", "Head_Id_OptSize_OptIndex")]
[InlineData("Head", "Test/6?index=2&size=10", "Head_Id_OptSize_OptIndex")]
[InlineData("Head", "Test/6?index=2&otherParameter=10", "Head_Id_OptSize_OptIndex")]
[InlineData("Head", "Test/6?otherQueryParameter=1234", "Head_Id_OptSize_OptIndex")]
[InlineData("Head", "Test", "Head")]
[InlineData("Head", "Test?otherParam=2", "Head")]
[InlineData("Head", "Test?index=2&size=10", "Head")]
public void Route_Parameters_Default(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "Test", "GetUsers")]
[InlineData("GET", "Test/2", "GetUsersByName")]
[InlineData("GET", "Test/luigi?ssn=123456", "GetUserByNameAndSsn")]
[InlineData("GET", "Test/luigi?ssn=123456&id=2&ssn=12345", "GetUserByNameIdAndSsn")]
[InlineData("GET", "Test?age=10&ssn=123456", "GetUsers")]
[InlineData("GET", "Test?id=3&ssn=123456&name=luigi", "GetUserByNameIdAndSsn")]
[InlineData("POST", "Test/luigi?age=20", "PostUserByNameAndAge")]
public void Route_Parameters_Non_Id(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{name}";
object routeDefault = new { name = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "Test/3?NAME=mario", "GetUserByNameAndId")]
[InlineData("GET", "Test/3?name=mario&SSN=123456", "GetUserByNameIdAndSsn")]
[InlineData("GET", "Test?nAmE=mario&ssn=123456&AgE=3", "GetUserByNameAgeAndSsn")]
[InlineData("Delete", "Test/6?AGe=10", "DeleteUserByIdAndOptName")]
public void Route_Parameters_Casing(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{ID}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "Test/GetUsers", "GetUsers")]
[InlineData("GET", "Test/GetUser/7", "GetUser")]
[InlineData("GET", "Test/GetUser?id=3", "GetUser")]
[InlineData("GET", "Test/GetUser/4?id=3", "GetUser")]
[InlineData("GET", "Test/GetUserByNameAgeAndSsn?name=user&age=90&ssn=123456789", "GetUserByNameAgeAndSsn")]
[InlineData("GET", "Test/GetUserByNameAndSsn?name=user&ssn=123456789", "GetUserByNameAndSsn")]
[InlineData("POST", "Test/PostUserByNameAndAddress?name=user", "PostUserByNameAndAddress")]
public void Route_Action(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{action}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "Test/getusers", "GetUsers")]
[InlineData("GET", "Test/getuseR/1", "GetUser")]
[InlineData("GET", "Test/Getuser?iD=3", "GetUser")]
[InlineData("GET", "Test/GetUser/4?Id=3", "GetUser")]
[InlineData("GET", "Test/GetUserByNameAgeandSsn?name=user&age=90&ssn=123456789", "GetUserByNameAgeAndSsn")]
[InlineData("GET", "Test/getUserByNameAndSsn?name=user&ssn=123456789", "GetUserByNameAndSsn")]
[InlineData("POST", "Test/PostUserByNameAndAddress?name=user", "PostUserByNameAndAddress")]
public void Route_Action_Name_Casing(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{action}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "Test", "GetUsers")]
[InlineData("GET", "Test/?name=peach", "GetUsersByName")]
[InlineData("GET", "Test?name=peach", "GetUsersByName")]
[InlineData("GET", "Test?name=peach&ssn=123456", "GetUserByNameAndSsn")]
[InlineData("GET", "Test?name=peach&ssn=123456&age=3", "GetUserByNameAgeAndSsn")]
public void Route_No_Action(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}";
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "ParameterAttribute/2", "GetUser")]
[InlineData("GET", "ParameterAttribute?id=2", "GetUser")]
[InlineData("GET", "ParameterAttribute?myId=2", "GetUserByMyId")]
[InlineData("POST", "ParameterAttribute/3?name=user", "PostUserNameFromUri")]
[InlineData("POST", "ParameterAttribute/3", "PostUserNameFromBody")]
[InlineData("DELETE", "ParameterAttribute/3?name=user", "DeleteUserWithNullableIdAndName")]
[InlineData("DELETE", "ParameterAttribute?address=userStreet", "DeleteUser")]
public void ModelBindingParameterAttribute_AreAppliedWhenSelectingActions(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "ParameterAttribute", typeof(ParameterAttributeController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "notActionParameterValue1/Test", "GetUsers")]
[InlineData("GET", "notActionParameterValue2/Test/2", "GetUser")]
[InlineData("GET", "notActionParameterValue1/Test?randomQueryVariable=val1", "GetUsers")]
[InlineData("GET", "notActionParameterValue2/Test/2?randomQueryVariable=val2", "GetUser")]
public void ActionsThatHaveSubsetOfRouteParameters_AreConsideredForSelection(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{notActionParameter}/{controller}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Fact]
public void RequestToAmbiguousAction_OnDefaultRoute()
{
string routeUrl = "{controller}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
string httpMethod = "Post";
string requestUrl = "Test?name=mario";
// This would result in ambiguous match because complex parameter is not considered for matching.
// Therefore, PostUserByNameAndAddress(string name, Address address) would conflicts with PostUserByName(string name)
Assert.Throws<InvalidOperationException>(() =>
{
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
});
}
[Fact]
public void RequestToActionWithNotSupportedHttpMethod_OnRouteWithAction()
{
string routeUrl = "{controller}/{action}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
string requestUrl = "Test/GetUsers";
string httpMethod = "POST";
var exception = Assert.Throws<HttpResponseException>(() =>
{
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
});
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Response.StatusCode);
var content = Assert.IsType<ObjectContent<HttpError>>(exception.Response.Content);
AssertAllowedHeaders(exception.Response, HttpMethod.Get);
Assert.Equal("The requested resource does not support http method 'POST'.", ((HttpError)content.Value).Message);
}
[Fact]
public void RequestToActionWith_HttpMethodDefinedByAttributeAndActionName()
{
string routeUrl = "{controller}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
string requestUrl = "Test";
string httpMethod = "PATCH";
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal("PutUser", descriptor.ActionName);
// When you have the HttpMethod attribute, the convention should not be applied.
httpMethod = "PUT";
var exception = Assert.Throws<HttpResponseException>(() =>
{
context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
ApiControllerHelper.SelectAction(context);
});
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Response.StatusCode);
var content = Assert.IsType<ObjectContent<HttpError>>(exception.Response.Content);
Assert.Equal("The requested resource does not support http method 'PUT'.", ((HttpError)content.Value).Message);
AssertAllowedHeaders(exception.Response, HttpMethod.Get, new HttpMethod("PATCH"), HttpMethod.Post, HttpMethod.Delete, HttpMethod.Head);
}
// Verify response has all the methods in its Allow header. values are unsorted.
private void AssertAllowedHeaders(HttpResponseMessage response, params HttpMethod[] allowedMethods)
{
foreach (var method in allowedMethods)
{
Assert.Contains(method.ToString(), response.Content.Headers.Allow);
}
Assert.Equal(allowedMethods.Length, response.Content.Headers.Allow.Count);
}
[Theory]
[InlineData("GET", "Test", "GetUsers")]
[InlineData("GET", "Test/2", "GetUser")]
[InlineData("GET", "Test/3?name=mario", "GetUserByNameAndId")]
[InlineData("GET", "Test/3?name=mario&ssn=123456", "GetUserByNameIdAndSsn")]
[InlineData("GET", "Test?name=mario&ssn=123456", "GetUserByNameAndSsn")]
[InlineData("GET", "Test?name=mario&ssn=123456&age=3", "GetUserByNameAgeAndSsn")]
[InlineData("GET", "Test/5?random=9", "GetUser")]
public void SelectionBasedOnParameter_IsNotAffectedBy_AddingGlobalValueProvider(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}/{id}";
object routeDefault = new { id = RouteParameter.Optional };
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault);
context.Configuration.Services.Add(typeof(ValueProviderFactory), new HeaderValueProviderFactory());
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "test", typeof(TestController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
[Theory]
[InlineData("GET", "Test", "Get")]
[InlineData("GET", "Test?scope=global", "GetWithEnumParameter")]
[InlineData("GET", "Test?level=off&kind=trace", "GetWithTwoEnumParameters")]
[InlineData("GET", "Test?level=", "GetWithNullableEnumParameter")]
public void SelectAction_ReturnsActionDescriptor_ForEnumParameterOverloads(string httpMethod, string requestUrl, string expectedActionName)
{
string routeUrl = "{controller}";
HttpControllerContext context = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl);
context.ControllerDescriptor = new HttpControllerDescriptor(context.Configuration, "EnumParameterOverloadsController", typeof(EnumParameterOverloadsController));
HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(context);
Assert.Equal(expectedActionName, descriptor.ActionName);
}
}
}