Example #1
0
        public void GetHttpHandlerReturnsMvcHandlerWithRouteData() {
            // Arrange
            RequestContext context =new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            IRouteHandler rh = new MvcRouteHandler();

            // Act
            IHttpHandler httpHandler = rh.GetHttpHandler(context);

            // Assert
            MvcHandler h = httpHandler as MvcHandler;
            Assert.IsNotNull(h, "The handler should be a valid MvcHandler instance");
            Assert.AreEqual<RequestContext>(context, h.RequestContext);
        }
        public void GetHttpHandlerThrowsIfTheRouteValuesDoesNotIncludeAControllerName()
        {
            // Arrange
            var httpContext = new Mock<HttpContextBase>();
            var routeData = new RouteData();
            var requestContext = new RequestContext(httpContext.Object, routeData);
            IRouteHandler routeHandler = new MvcRouteHandler();

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => routeHandler.GetHttpHandler(requestContext),
                "The matched route does not include a 'controller' route value, which is required."
                );
        }
        public void GetHttpHandlerReturnsMvcHandlerWithRouteData() {
            // Arrange
            var routeData = new RouteData();
            routeData.Values["controller"] = "controllerName";
            var context = new RequestContext(new Mock<HttpContextBase>().Object, routeData);
            var controllerFactory = new Mock<IControllerFactory>();
            controllerFactory.Setup(f => f.GetControllerSessionBehavior(context, "controllerName"))
                             .Returns(SessionStateBehavior.Default)
                             .Verifiable();
            IRouteHandler rh = new MvcRouteHandler(controllerFactory.Object);

            // Act
            IHttpHandler httpHandler = rh.GetHttpHandler(context);

            // Assert
            MvcHandler h = httpHandler as MvcHandler;
            Assert.IsNotNull(h, "The handler should be a valid MvcHandler instance");
            Assert.AreEqual<RequestContext>(context, h.RequestContext);
        }
        public void GetHttpHandlerAsksControllerFactoryForSessionBehaviorOfController() {
            // Arrange
            var httpContext = new Mock<HttpContextBase>();
            var routeData = new RouteData();
            routeData.Values["controller"] = "controllerName";
            var requestContext = new RequestContext(httpContext.Object, routeData);
            var controllerFactory = new Mock<IControllerFactory>();
            controllerFactory.Setup(f => f.GetControllerSessionBehavior(requestContext, "controllerName"))
                             .Returns(SessionStateBehavior.ReadOnly)
                             .Verifiable();
            IRouteHandler routeHandler = new MvcRouteHandler(controllerFactory.Object);

            // Act
            routeHandler.GetHttpHandler(requestContext);

            // Assert
            controllerFactory.Verify();
            httpContext.Verify(c => c.SetSessionStateBehavior(SessionStateBehavior.ReadOnly));
        }