public void SelectAction_OnRouteWithActionParameter(string httpMethod, string requestUrl, string expectedActionName) { string routeUrl = "{controller}/{action}/{id}"; object routeDefault = new { id = RouteParameter.Optional }; HttpControllerContext controllerContext = ApiControllerHelper.CreateControllerContext(httpMethod, requestUrl, routeUrl, routeDefault); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ActionAttributeTestController)); HttpActionDescriptor descriptor = ApiControllerHelper.SelectAction(controllerContext); Assert.Equal <string>(expectedActionName, descriptor.ActionName); }
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); }
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); }
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 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 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); }); }
[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); }
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); Assert.Equal("The requested resource does not support http method 'POST'.", ((HttpError)content.Value).Message); }
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); }
public SchoolsController() { var includes = new[] { "Students" }; apiControllerHelper = new ApiControllerHelper(new StudentsDbRepository(), includes); }
public StudentsController(IRepository repository) { var includes = new[] { "School", "Marks" }; apiControllerHelper = new ApiControllerHelper(repository, includes); }