Пример #1
0
        public void GetActionMapping_IgnoresNonAction()
        {
            var actionSelector            = new ApiControllerActionSelector();
            HttpControllerContext context = ContextUtil.CreateControllerContext();

            context.Request = new HttpRequestMessage {
                Method = HttpMethod.Get
            };
            var controllerDescriptor = new HttpControllerDescriptor(
                context.Configuration,
                "NonAction",
                typeof(NonActionController)
                );

            context.ControllerDescriptor = controllerDescriptor;

            var mapping = actionSelector.GetActionMapping(controllerDescriptor);

            Assert.False(mapping.Contains("GetA"));
            Assert.True(mapping.Contains("GetB"));
        }
Пример #2
0
        public static HttpControllerContext CreateControllerContext(
            string httpMethod,
            string requestUrl,
            string routeUrl,
            object routeDefault = null
            )
        {
            string            baseAddress = "http://localhost/";
            HttpConfiguration config      = new HttpConfiguration();
            HttpRoute         route       =
                routeDefault != null
                    ? new HttpRoute(routeUrl, new HttpRouteValueDictionary(routeDefault))
                    : new HttpRoute(routeUrl);

            config.Routes.Add("test", route);

            HttpRequestMessage request = new HttpRequestMessage(
                HttpMethodHelper.GetHttpMethod(httpMethod),
                baseAddress + requestUrl
                );

            IHttpRouteData routeData = config.Routes.GetRouteData(request);

            if (routeData == null)
            {
                throw new InvalidOperationException(
                          "Could not dispatch to controller based on the route."
                          );
            }

            RemoveOptionalRoutingParameters(routeData.Values);

            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(
                config,
                routeData,
                request
                );

            return(controllerContext);
        }
Пример #3
0
        public void Route_ActionName()
        {
            // Arrange
            ApiController api   = new UsersRpcController();
            HttpRouteData route = new HttpRouteData(new HttpRoute());

            route.Values.Add("action", "Admin");
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(instance: api, routeData: route, request: new HttpRequestMessage()
            {
                Method = HttpMethod.Post
            });

            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersRpcController));

            // Act
            HttpResponseMessage message = api.ExecuteAsync(controllerContext, CancellationToken.None).Result;
            User user = message.Content.ReadAsAsync <User>().Result;

            // Assert
            Assert.Equal("Yao", user.FirstName);
            Assert.Equal("Huang", user.LastName);
        }
Пример #4
0
        public void Setting_CustomActionSelector()
        {
            // Arrange
            ApiController         api = new UsersController();
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();

            HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersController));

            controllerContext.ControllerDescriptor = controllerDescriptor;

            Mock <IHttpActionSelector> mockSelector = new Mock <IHttpActionSelector>();

            mockSelector
            .Setup(invoker => invoker.SelectAction(It.IsAny <HttpControllerContext>()))
            .Returns(() =>
            {
                Func <HttpResponseMessage> testDelegate =
                    () => new HttpResponseMessage {
                    Content = new StringContent("This is a test")
                };
                return(new ReflectedHttpActionDescriptor
                {
                    Configuration = controllerContext.Configuration,
                    ControllerDescriptor = controllerDescriptor,
                    MethodInfo = testDelegate.Method
                });
            });
            controllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), mockSelector.Object);

            // Act
            HttpResponseMessage message = api.ExecuteAsync(
                controllerContext,
                CancellationToken.None).Result;

            // Assert
            Assert.Equal("This is a test", message.Content.ReadAsStringAsync().Result);
        }
Пример #5
0
 public ReflectedHttpActionDescriptorTest()
 {
     _context = ContextUtil.CreateControllerContext(instance: _controller);
 }
 public ReflectedHttpActionDescriptorTest()
 {
     _context = ContextUtil.CreateControllerContext(instance: _controller);
     _context.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
 }
Пример #7
0
 public void Constructor_Throws_IfActionDescriptorIsNull()
 {
     Assert.ThrowsArgumentNull(
         () => new HttpActionContext(ContextUtil.CreateControllerContext(), null),
         "actionDescriptor");
 }
Пример #8
0
 public static HttpActionContext CreateActionContext(HttpControllerContext controllerContext = null, HttpActionDescriptor actionDescriptor = null)
 {
     HttpControllerContext context = controllerContext ?? ContextUtil.CreateControllerContext();
     HttpActionDescriptor descriptor = actionDescriptor ?? new Mock<HttpActionDescriptor>() { CallBase = true }.Object;
     return new HttpActionContext(context, descriptor);
 }