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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;
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/4?name=mario&age=20", "GetUserByNameAndId")]
[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")]
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", "GetUser")]
[InlineData("GET", "Test/GetUser?id=3", "GetUser")]
[InlineData("GET", "Test/GetUser/4?id=3", "GetUser")]
[InlineData("GET", "Test/GetUserByNameAgeAndSsn", "GetUserByNameAgeAndSsn")]
[InlineData("GET", "Test/GetUserByNameAndSsn", "GetUserByNameAndSsn")]
[InlineData("POST", "Test/PostUserByNameAndAddress", "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", "GetUser")]
[InlineData("GET", "Test/Getuser?iD=3", "GetUser")]
[InlineData("GET", "Test/GetUser/4?Id=3", "GetUser")]
[InlineData("GET", "Test/GetUserByNameAgeandSsn", "GetUserByNameAgeAndSsn")]
[InlineData("GET", "Test/getUserByNameAndSsn", "GetUserByNameAndSsn")]
[InlineData("POST", "Test/PostUserByNameAndAddress", "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);
}
[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<HttpResponseException>(() =>
{
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<string>>(exception.Response.Content);
Assert.Equal("The requested resource does not support http method 'POST'.", content.Value);
}
[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<string>("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<string>>(exception.Response.Content);
Assert.Equal("The requested resource does not support http method 'PUT'.", content.Value);
}
}
}
|