コード例 #1
0
        void Run(Type controller, FromRouteBinderPrecedence.BinderPrecedence expected)
        {
            routes.Clear();
            routes.MapCodeRoutes(controller);

            var httpContextMock = new Mock <HttpContextBase>();

            httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/index/foo");

            var httpResponseMock = new Mock <HttpResponseBase>();

            httpContextMock.Setup(c => c.Response).Returns(httpResponseMock.Object);

            var routeData = routes.GetRouteData(httpContextMock.Object);

            var controllerInstance = (Controller)Activator.CreateInstance(controller);

            controllerInstance.ValidateRequest = false;

            var requestContext    = new RequestContext(httpContextMock.Object, routeData);
            var controllerContext = new ControllerContext(requestContext, controllerInstance);

            controllerInstance.ValueProvider = new ValueProviderCollection(new IValueProvider[] { new RouteDataValueProvider(controllerContext) });

            ((IController)controllerInstance).Execute(requestContext);

            httpResponseMock.Verify(c => c.Write(It.Is <string>(s => s == expected.ToString())), Times.AtLeastOnce());
        }
コード例 #2
0
        void Run(Type controller, FromRouteBinderPrecedence.BinderPrecedence expected)
        {
            config.MapCodeRoutes(controller);

            var request   = new HttpRequestMessage(HttpMethod.Get, "http://localhost/foo");
            var routeData = routes.GetRouteData(request);

            var controllerInstance = (ApiController)Activator.CreateInstance(controller);

            var controllerContext = new HttpControllerContext(config, routeData, request)
            {
                ControllerDescriptor = new HttpControllerDescriptor(config, (string)routeData.Values["controller"], controller),
                Controller           = controllerInstance
            };

            string value;

            controllerInstance.ExecuteAsync(controllerContext, CancellationToken.None)
            .Result
            .TryGetContentValue(out value);

            Assert.AreEqual(expected.ToString(), value);
        }