コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
            });
        }
コード例 #7
0
        [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
                );
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
 public SchoolsController()
 {
     var includes = new[] { "Students" };
     apiControllerHelper = new ApiControllerHelper(new StudentsDbRepository(), includes);
 }
コード例 #14
0
 public StudentsController(IRepository repository)
 {
     var includes = new[] { "School", "Marks" };
     apiControllerHelper = new ApiControllerHelper(repository, includes);
 }