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); }
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 <HttpError> >(exception.Response.Content); Assert.Equal("The requested resource does not support http method 'PUT'.", ((HttpError)content.Value).Message); }
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); }
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 ); }
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); } ); }
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); }
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); }
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); }
[InlineData("GET", "ActionAttributeTest/NonAction")] // NonAction, 404 public void SelectAction_ThrowsNotFound_OnRouteWithActionParameter( string httpMethod, string requestUrl ) { string routeUrl = "{controller}/{action}/{id}"; object routeDefault = new { id = RouteParameter.Optional }; HttpControllerContext controllerContext = ApiControllerHelper.CreateControllerContext( httpMethod, requestUrl, routeUrl, routeDefault ); controllerContext.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; Type controllerType = typeof(ActionAttributeTestController); controllerContext.ControllerDescriptor = new HttpControllerDescriptor( controllerContext.Configuration, controllerType.Name, controllerType ); var exception = Assert.Throws <HttpResponseException>( () => { HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction( controllerContext ); } ); Assert.Equal(HttpStatusCode.NotFound, exception.Response.StatusCode); var content = Assert.IsType <ObjectContent <HttpError> >(exception.Response.Content); // Error message might be ApiControllerActionSelector_ActionNameNotFound or ApiControllerActionSelector_ActionNotFound string actualMessage = (string)((HttpError)content.Value)["MessageDetail"]; Assert.StartsWith( "No action was found on the controller 'ActionAttributeTestController' that matches", actualMessage ); }
public void SelectAction_ThrowsNotFound_OnRouteWithActionParameter(string httpMethod, string requestUrl) { string routeUrl = "{controller}/{action}/{id}"; object routeDefault = new { id = RouteParameter.Optional }; HttpControllerContext controllerContext = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault); Type controllerType = typeof(ActionAttributeTestController); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, controllerType.Name, controllerType); var exception = Assert.Throws <HttpResponseException>(() => { HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(controllerContext); }); Assert.Equal(HttpStatusCode.NotFound, exception.Response.StatusCode); var content = Assert.IsType <ObjectContent <string> >(exception.Response.Content); Assert.Equal("No action was found on the controller 'ActionAttributeTestController' that matches the request.", content.Value); }
public void SelectAction_ThrowsMethodNotSupported_OnRouteWithActionParameter(string httpMethod, string requestUrl) { string routeUrl = "{controller}/{action}/{id}"; object routeDefault = new { id = RouteParameter.Optional }; HttpControllerContext controllerContext = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault); Type controllerType = typeof(ActionAttributeTestController); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, controllerType.Name, controllerType); var exception = Assert.Throws <HttpResponseException>(() => { HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(controllerContext); }); 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 '" + httpMethod + "'.", content.Value); }