ProcessRequest() public method

public ProcessRequest ( HttpContext context ) : void
context HttpContext
return void
示例#1
0
        public void AttributeRouting_MixedWithGeneralRouting(Type controllerType, string path, string expectedAction)
        {
            // Arrange
            var    controllerTypes = new[] { controllerType };
            var    routes          = new RouteCollection();
            object defaults        = new { controller = controllerType.Name.Substring(0, controllerType.Name.Length - 10) };

            routes.Add(new Route("standard/{action}", new RouteValueDictionary(defaults), null));
            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            if (expectedAction == null)
            {
                // Act & Assert
                Assert.Throws <HttpException>(() => handler.ProcessRequest(context));
            }
            else
            {
                // Act
                handler.ProcessRequest(context);

                // Assert
                ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);
                Assert.Equal(expectedAction, result.Content);
            }
        }
        public void ProcessRequestDisposesControllerIfExecuteThrowsException()
        {
            // Arrange
            Mock <ControllerBase> mockController = new Mock <ControllerBase>(MockBehavior.Strict);

            mockController.As <IDisposable>().Setup(d => d.Dispose()); // so that Verify can be called on Dispose later
            mockController.Protected().Setup("Execute", ItExpr.IsAny <RequestContext>()).Throws(new Exception("some exception"));

            ControllerBuilder builder = new ControllerBuilder();

            builder.SetControllerFactory(new SimpleControllerFactory(mockController.Object));

            Mock <HttpContextBase> contextMock = new Mock <HttpContextBase>();

            contextMock.ExpectMvcVersionResponseHeader().Verifiable();
            RequestContext requestContext = new RequestContext(contextMock.Object, new RouteData());

            requestContext.RouteData.Values["controller"] = "fooController";
            MvcHandler handler = new MvcHandler(requestContext)
            {
                ControllerBuilder = builder
            };

            // Act
            Assert.Throws <Exception>(
                delegate { handler.ProcessRequest(requestContext.HttpContext); },
                "some exception");

            // Assert
            mockController.Verify();
            contextMock.Verify();
            mockController.As <IDisposable>().Verify(d => d.Dispose(), Times.AtMostOnce());
        }
示例#3
0
        public void AttributeRouting_WithCustomizedRoutePrefixAttribute(string path, string expectedAction)
        {
            // Arrange
            var controllerTypes = new[]
            {
                typeof(ControllersWithCustomizedRoutePrefixAttribute.NS1.HomeController),
                typeof(ControllersWithCustomizedRoutePrefixAttribute.NS2.AccountController),
                typeof(ControllersWithCustomizedRoutePrefixAttribute.NS3.OtherController),
            };

            var routes = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }
        public void ProcessRequestAddsServerHeaderCallsExecute()
        {
            // Arrange
            Mock <HttpContextBase> contextMock = new Mock <HttpContextBase>();

            contextMock.ExpectMvcVersionResponseHeader().Verifiable();

            RouteData rd = new RouteData();

            rd.Values.Add("controller", "foo");
            RequestContext requestContext = new RequestContext(contextMock.Object, rd);
            MvcHandler     mvcHandler     = new MvcHandler(requestContext);

            Mock <ControllerBase> controllerMock = new Mock <ControllerBase>();

            controllerMock.Protected().Setup("Execute", requestContext).Verifiable();

            ControllerBuilder         cb = new ControllerBuilder();
            Mock <IControllerFactory> controllerFactoryMock = new Mock <IControllerFactory>();

            controllerFactoryMock.Setup(o => o.CreateController(requestContext, "foo")).Returns(controllerMock.Object);
            controllerFactoryMock.Setup(o => o.ReleaseController(controllerMock.Object));
            cb.SetControllerFactory(controllerFactoryMock.Object);
            mvcHandler.ControllerBuilder = cb;

            // Act
            mvcHandler.ProcessRequest(contextMock.Object);

            // Assert
            contextMock.Verify();
            controllerMock.Verify();
        }
示例#5
0
        public void ProcessRequestWithDisabledServerHeaderOnlyCallsExecute()
        {
            bool oldResponseHeaderValue = MvcHandler.DisableMvcResponseHeader;

            try {
                // Arrange
                MvcHandler.DisableMvcResponseHeader = true;
                Mock <HttpContextBase> contextMock = new Mock <HttpContextBase>();

                RouteData rd = new RouteData();
                rd.Values.Add("controller", "foo");
                RequestContext requestContext = new RequestContext(contextMock.Object, rd);
                MvcHandler     mvcHandler     = new MvcHandler(requestContext);

                Mock <ControllerBase> controllerMock = new Mock <ControllerBase>();
                controllerMock.Protected().Setup("Execute", requestContext).Verifiable();

                ControllerBuilder         cb = new ControllerBuilder();
                Mock <IControllerFactory> controllerFactoryMock = new Mock <IControllerFactory>();
                controllerFactoryMock.Setup(o => o.CreateController(requestContext, "foo")).Returns(controllerMock.Object);
                controllerFactoryMock.Setup(o => o.ReleaseController(controllerMock.Object));
                cb.SetControllerFactory(controllerFactoryMock.Object);
                mvcHandler.ControllerBuilder = cb;

                // Act
                mvcHandler.ProcessRequest(contextMock.Object);

                // Assert
                controllerMock.Verify();
            }
            finally {
                MvcHandler.DisableMvcResponseHeader = oldResponseHeaderValue;
            }
        }
        public void ProcessRequestDisposesControllerIfExecuteDoesNotThrowException()
        {
            // Arrange
            Mock <ControllerBase> mockController = new Mock <ControllerBase>();

            mockController.Protected().Expect("Execute", ItExpr.IsAny <RequestContext>()).Verifiable();
            mockController.As <IDisposable>().Expect(d => d.Dispose()).AtMostOnce().Verifiable();

            ControllerBuilder builder = new ControllerBuilder();

            builder.SetControllerFactory(new SimpleControllerFactory(mockController.Object));

            Mock <HttpContextBase>  contextMock  = new Mock <HttpContextBase>();
            Mock <HttpResponseBase> responseMock = new Mock <HttpResponseBase>();

            responseMock.Expect(r => r.AppendHeader(MvcHandler.MvcVersionHeaderName, "1.0")).Verifiable();
            contextMock.Expect(c => c.Response).Returns(responseMock.Object);
            RequestContext requestContext = new RequestContext(contextMock.Object, new RouteData());

            requestContext.RouteData.Values["controller"] = "fooController";
            MvcHandler handler = new MvcHandler(requestContext)
            {
                ControllerBuilder = builder
            };

            // Act
            handler.ProcessRequest(requestContext.HttpContext);

            // Assert
            mockController.Verify();
            responseMock.Verify();
        }
        public void ProcessRequestRemovesOptionalParametersFromRouteValueDictionary()
        {
            // Arrange
            Mock <HttpContextBase> contextMock = new Mock <HttpContextBase>();

            contextMock.ExpectMvcVersionResponseHeader();

            RouteData rd = new RouteData();

            rd.Values.Add("controller", "foo");
            rd.Values.Add("optional", UrlParameter.Optional);
            RequestContext requestContext = new RequestContext(contextMock.Object, rd);
            MvcHandler     mvcHandler     = new MvcHandler(requestContext);

            Mock <ControllerBase> controllerMock = new Mock <ControllerBase>();

            controllerMock.Protected().Setup("Execute", requestContext).Verifiable();

            ControllerBuilder         cb = new ControllerBuilder();
            Mock <IControllerFactory> controllerFactoryMock = new Mock <IControllerFactory>();

            controllerFactoryMock.Setup(o => o.CreateController(requestContext, "foo")).Returns(controllerMock.Object);
            controllerFactoryMock.Setup(o => o.ReleaseController(controllerMock.Object));
            cb.SetControllerFactory(controllerFactoryMock.Object);
            mvcHandler.ControllerBuilder = cb;

            // Act
            mvcHandler.ProcessRequest(contextMock.Object);

            // Assert
            controllerMock.Verify();
            Assert.False(rd.Values.ContainsKey("optional"));
        }
示例#8
0
        public void AttributeRouting_OptionalParametersGetRemoved()
        {
            // Arrange
            var controllerTypes = new[] { typeof(OptionalParameterController) };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext("~/Create");
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal("Create()", result.Content);

            // The request context should be updated to to contain the routedata of the direct route
            Assert.Equal("{action}/{id}", ((Route)requestContext.RouteData.Route).Url);
            Assert.Null(requestContext.RouteData.Values["id"]);
        }
        public void ProcessRequestAddsServerHeaderCallsExecute()
        {
            // Arrange
            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            contextMock.ExpectMvcVersionResponseHeader().Verifiable();

            RouteData rd = new RouteData();
            rd.Values.Add("controller", "foo");
            RequestContext requestContext = new RequestContext(contextMock.Object, rd);
            MvcHandler mvcHandler = new MvcHandler(requestContext);

            Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();
            controllerMock.Protected().Setup("Execute", requestContext).Verifiable();

            ControllerBuilder cb = new ControllerBuilder();
            Mock<IControllerFactory> controllerFactoryMock = new Mock<IControllerFactory>();
            controllerFactoryMock.Setup(o => o.CreateController(requestContext, "foo")).Returns(controllerMock.Object);
            controllerFactoryMock.Setup(o => o.ReleaseController(controllerMock.Object));
            cb.SetControllerFactory(controllerFactoryMock.Object);
            mvcHandler.ControllerBuilder = cb;

            // Act
            mvcHandler.ProcessRequest(contextMock.Object);

            // Assert
            contextMock.Verify();
            controllerMock.Verify();
        }
        public void AttributeRouting_MethodOverloads_WithDifferentActionNames(
            Type controllerType,
            string path,
            string expectedAction
            )
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            AttributeRoutingMapper.MapAttributeRoutes(routes, controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }
示例#11
0
        public void Invoke(ControllerContext context)
        {
            RouteData rd = new RouteData(context.RouteData.Route, context.RouteData.RouteHandler);

            foreach (var pair in RouteValues)
            {
                rd.Values.Add(pair.Key, pair.Value);
            }
            IHttpHandler handler = new MvcHandler(new RequestContext(context.HttpContext, rd));

            handler.ProcessRequest(System.Web.HttpContext.Current);
        }
示例#12
0
        public override void ProcessRequest(HttpContext context)
        {
            var contextWrapper = new HttpContextWrapper(HttpContext.Current);
            var routeData      = new RouteData();

            routeData.Values.Add("controller", "Home");
            routeData.Values.Add("action", "Index");
            routeData.Values.Add("language", Localization.DefaultLanguage);
            var requestContext = new RequestContext(contextWrapper, routeData);
            var handler        = new MvcHandler(requestContext) as IHttpHandler;

            handler.ProcessRequest(context);
        }
        public void ProcessRequestWithRouteWithoutControllerThrows()
        {
            // Arrange
            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            contextMock.ExpectMvcVersionResponseHeader().Verifiable();
            RouteData rd = new RouteData();
            MvcHandler mvcHandler = new MvcHandler(new RequestContext(contextMock.Object, rd));

            // Act
            Assert.Throws<InvalidOperationException>(
                delegate { mvcHandler.ProcessRequest(contextMock.Object); },
                "The RouteData must contain an item named 'controller' with a non-empty string value.");

            // Assert
            contextMock.Verify();
        }
        public void ProcessRequestWithRouteWithoutControllerThrows()
        {
            // Arrange
            Mock <HttpContextBase> contextMock = new Mock <HttpContextBase>();

            contextMock.ExpectMvcVersionResponseHeader().Verifiable();
            RouteData  rd         = new RouteData();
            MvcHandler mvcHandler = new MvcHandler(new RequestContext(contextMock.Object, rd));

            // Act
            Assert.Throws <InvalidOperationException>(
                delegate { mvcHandler.ProcessRequest(contextMock.Object); },
                "The RouteData must contain an item named 'controller' with a non-empty string value.");

            // Assert
            contextMock.Verify();
        }
示例#15
0
        public void AttributeRouting_AmbiguousActions_ThrowsAmbiguousException(Type controllerType, string path)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            Assert.Throws <AmbiguousMatchException>(() => handler.ProcessRequest(context));
        }
示例#16
0
        public void AttributeRouting_AmbiguousActions_ThrowsAmbiguousException(Type controllerType, string path)
        {
            // Arrange
            var controllerTypes = new[] { controllerType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            // Bug 1285: Attribute routing with ambiguous actions should throw an AmbiguousMatchException.
            // This test should fail once that is fixed. Uncomment the line below then.
            // Assert.Throws<AmbiguousMatchException>(() => handler.ProcessRequest(context));
            Assert.DoesNotThrow(() => handler.ProcessRequest(context));
        }
示例#17
0
        public void ProcessRequestWithRouteWithoutControllerThrows()
        {
            // Arrange
            Mock <HttpContextBase>  contextMock  = new Mock <HttpContextBase>();
            Mock <HttpResponseBase> responseMock = new Mock <HttpResponseBase>();

            responseMock.Expect(r => r.AppendHeader(MvcHandler.MvcVersionHeaderName, "1.0")).Verifiable();
            contextMock.Expect(c => c.Response).Returns(responseMock.Object);
            RouteData  rd         = new RouteData();
            MvcHandler mvcHandler = new MvcHandler(new RequestContext(contextMock.Object, rd));

            // Act
            ExceptionHelper.ExpectException <InvalidOperationException>(
                delegate {
                mvcHandler.ProcessRequest(contextMock.Object);
            },
                "The RouteData must contain an item named 'controller' with a non-empty string value.");

            // Assert
            responseMock.Verify();
        }
示例#18
0
        public void AttributeRouting_WithInheritance_MethodOverrides(Type derivedController, string path, string expectedAction)
        {
            // Arrange
            var controllerTypes = new[] { derivedController, derivedController.BaseType };
            var routes          = new RouteCollection();

            routes.MapMvcAttributeRoutes(controllerTypes);

            HttpContextBase context        = GetContext(path);
            RouteData       routeData      = routes.GetRouteData(context);
            RequestContext  requestContext = new RequestContext(context, routeData);
            MvcHandler      handler        = new MvcHandler(requestContext);

            handler.ControllerBuilder.SetControllerFactory(GetControllerFactory(controllerTypes));

            // Act
            handler.ProcessRequest(context);

            // Assert
            ContentResult result = Assert.IsType <ContentResult>(context.Items[ResultKey]);

            Assert.Equal(expectedAction, result.Content);
        }
示例#19
0
        public void Render(WidgetContext context)
        {
            var rd = new RouteData(context.RouteData.Route, context.RouteData.RouteHandler);

            // add route values for the widget;
            foreach (var p1 in RouteValues)
            {
                rd.Values.Add(p1.Key, p1.Value);
            }

            // add all existing route data values that doesn't clash
            foreach (var p2 in context.RouteData.Values.Where(r => !rd.Values.ContainsKey(r.Key)))
            {
                rd.Values.Add(p2.Key, p2.Value);
            }

            // add page to the route data
            rd.Values.Add("sourceUrl", context.SourceUrl);

            // add widget to the route data
            //rd.Values.Add("instance", Widget);

            //setup httpcontext
            // allows us to mock it
            var req = new HttpRequest(context.HttpContext.Request.Path,
                                      context.HttpContext.Request.Url.AbsoluteUri,
                                      context.HttpContext.Request.Url.Query.TrimStart('?'));
            var res = new HttpResponse(context.Writer);
            var ctx = new HttpContext(req, res);

            ctx.User = context.HttpContext.User;

            IHttpHandler handler = new MvcHandler(new RequestContext(new HttpContextWrapper(ctx), rd));

            handler.ProcessRequest(ctx);
        }
        public void ProcessRequestRemovesOptionalParametersFromRouteValueDictionary()
        {
            // Arrange
            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            contextMock.ExpectMvcVersionResponseHeader();

            RouteData rd = new RouteData();
            rd.Values.Add("controller", "foo");
            rd.Values.Add("optional", UrlParameter.Optional);
            RequestContext requestContext = new RequestContext(contextMock.Object, rd);
            MvcHandler mvcHandler = new MvcHandler(requestContext);

            Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();
            controllerMock.Protected().Setup("Execute", requestContext).Verifiable();

            ControllerBuilder cb = new ControllerBuilder();
            Mock<IControllerFactory> controllerFactoryMock = new Mock<IControllerFactory>();
            controllerFactoryMock.Setup(o => o.CreateController(requestContext, "foo")).Returns(controllerMock.Object);
            controllerFactoryMock.Setup(o => o.ReleaseController(controllerMock.Object));
            cb.SetControllerFactory(controllerFactoryMock.Object);
            mvcHandler.ControllerBuilder = cb;

            // Act
            mvcHandler.ProcessRequest(contextMock.Object);

            // Assert
            controllerMock.Verify();
            Assert.False(rd.Values.ContainsKey("optional"));
        }
        public void ProcessRequestWithDisabledServerHeaderOnlyCallsExecute()
        {
            bool oldResponseHeaderValue = MvcHandler.DisableMvcResponseHeader;
            try
            {
                // Arrange
                MvcHandler.DisableMvcResponseHeader = true;
                Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();

                RouteData rd = new RouteData();
                rd.Values.Add("controller", "foo");
                RequestContext requestContext = new RequestContext(contextMock.Object, rd);
                MvcHandler mvcHandler = new MvcHandler(requestContext);

                Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();
                controllerMock.Protected().Setup("Execute", requestContext).Verifiable();

                ControllerBuilder cb = new ControllerBuilder();
                Mock<IControllerFactory> controllerFactoryMock = new Mock<IControllerFactory>();
                controllerFactoryMock.Setup(o => o.CreateController(requestContext, "foo")).Returns(controllerMock.Object);
                controllerFactoryMock.Setup(o => o.ReleaseController(controllerMock.Object));
                cb.SetControllerFactory(controllerFactoryMock.Object);
                mvcHandler.ControllerBuilder = cb;

                // Act
                mvcHandler.ProcessRequest(contextMock.Object);

                // Assert
                controllerMock.Verify();
            }
            finally
            {
                MvcHandler.DisableMvcResponseHeader = oldResponseHeaderValue;
            }
        }
        public void ProcessRequestDisposesControllerIfExecuteThrowsException()
        {
            // Arrange
            Mock<ControllerBase> mockController = new Mock<ControllerBase>(MockBehavior.Strict);
            mockController.As<IDisposable>().Setup(d => d.Dispose()); // so that Verify can be called on Dispose later
            mockController.Protected().Setup("Execute", ItExpr.IsAny<RequestContext>()).Throws(new Exception("some exception"));

            ControllerBuilder builder = new ControllerBuilder();
            builder.SetControllerFactory(new SimpleControllerFactory(mockController.Object));

            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            contextMock.ExpectMvcVersionResponseHeader().Verifiable();
            RequestContext requestContext = new RequestContext(contextMock.Object, new RouteData());
            requestContext.RouteData.Values["controller"] = "fooController";
            MvcHandler handler = new MvcHandler(requestContext)
            {
                ControllerBuilder = builder
            };

            // Act
            Assert.Throws<Exception>(
                delegate { handler.ProcessRequest(requestContext.HttpContext); },
                "some exception");

            // Assert
            mockController.Verify();
            contextMock.Verify();
            mockController.As<IDisposable>().Verify(d => d.Dispose(), Times.AtMostOnce());
        }