Exemplo n.º 1
0
        public void WrapForServerExecute_ProcessRequest_WrapsHttpExceptionIfStatusCodeNot500()
        {
            // Arrange
            HttpContext         httpContext     = GetHttpContext();
            Mock <IHttpHandler> mockHttpHandler = new Mock <IHttpHandler>();

            mockHttpHandler
            .Setup(o => o.ProcessRequest(httpContext))
            .Throws(new HttpException(404, "Some exception."));

            IHttpHandler wrapper = HttpHandlerUtil.WrapForServerExecute(mockHttpHandler.Object);

            // Act & assert
            HttpException outerException = Assert.ThrowsHttpException(
                delegate
            {
                wrapper.ProcessRequest(httpContext);
            },
                @"Execution of the child request failed. Please examine the InnerException for more information.",
                500
                );

            HttpException innerException = outerException.InnerException as HttpException;

            Assert.NotNull(innerException);
            Assert.Equal(404, innerException.GetHttpCode());
            Assert.Equal("Some exception.", innerException.Message);
        }
Exemplo n.º 2
0
        public void WrapForServerExecute_ReturnsIHttpHandler()
        {
            // Arrange
            IHttpHandler httpHandler = new Mock <IHttpHandler>().Object;

            // Act
            IHttpHandler wrapper = HttpHandlerUtil.WrapForServerExecute(httpHandler);

            // Assert
            Assert.False(wrapper is IHttpAsyncHandler);
        }
Exemplo n.º 3
0
        public void WrapForServerExecute_ReturnsIHttpHandler()
        {
            // Arrange
            IHttpHandler httpHandler = new Mock <IHttpHandler>().Object;

            // Act
            IHttpHandler wrapper = HttpHandlerUtil.WrapForServerExecute(httpHandler);

            // Assert
            Assert.IsFalse(wrapper is IHttpAsyncHandler, "Wrapper should have been only a sync handler.");
        }
Exemplo n.º 4
0
        // Helpers

        internal static void ActionHelper(HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException("htmlHelper");
            }
            if (String.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
            }

            RouteValueDictionary additionalRouteValues = routeValues;

            routeValues = MergeDictionaries(routeValues, htmlHelper.ViewContext.RouteData.Values);

            routeValues["action"] = actionName;
            if (!String.IsNullOrEmpty(controllerName))
            {
                routeValues["controller"] = controllerName;
            }

            bool            usingAreas;
            VirtualPathData vpd = htmlHelper.RouteCollection.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, null /* name */, routeValues, out usingAreas);

            if (vpd == null)
            {
                throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);
            }

            if (usingAreas)
            {
                routeValues.Remove("area");
                if (additionalRouteValues != null)
                {
                    additionalRouteValues.Remove("area");
                }
            }

            if (additionalRouteValues != null)
            {
                routeValues[ChildActionValueProvider.ChildActionValuesKey] = new DictionaryValueProvider <object>(additionalRouteValues, CultureInfo.InvariantCulture);
            }

            RouteData             routeData      = CreateRouteData(vpd.Route, routeValues, vpd.DataTokens, htmlHelper.ViewContext);
            HttpContextBase       httpContext    = htmlHelper.ViewContext.HttpContext;
            RequestContext        requestContext = new RequestContext(httpContext, routeData);
            ChildActionMvcHandler handler        = new ChildActionMvcHandler(requestContext);

            httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(handler), textWriter, true /* preserveForm */);
        }
Exemplo n.º 5
0
        public void WrapForServerExecute_ProcessRequest_PropagatesExceptionsIfNotHttpException()
        {
            // Arrange
            HttpContext         httpContext     = GetHttpContext();
            Mock <IHttpHandler> mockHttpHandler = new Mock <IHttpHandler>();

            mockHttpHandler.Setup(o => o.ProcessRequest(httpContext)).Throws(new InvalidOperationException("Some exception."));

            IHttpHandler wrapper = HttpHandlerUtil.WrapForServerExecute(mockHttpHandler.Object);

            // Act & assert
            Assert.Throws <InvalidOperationException>(
                delegate { wrapper.ProcessRequest(httpContext); },
                @"Some exception.");
        }
Exemplo n.º 6
0
        public void WrapForServerExecute_ProcessRequest_PropagatesHttpExceptionIfStatusCode500()
        {
            // Arrange
            HttpContext         httpContext     = GetHttpContext();
            Mock <IHttpHandler> mockHttpHandler = new Mock <IHttpHandler>();

            mockHttpHandler.Setup(o => o.ProcessRequest(httpContext)).Throws(new HttpException(500, "Some exception."));

            IHttpHandler wrapper = HttpHandlerUtil.WrapForServerExecute(mockHttpHandler.Object);

            // Act & assert
            Assert.ThrowsHttpException(
                delegate { wrapper.ProcessRequest(httpContext); },
                @"Some exception.",
                500);
        }
Exemplo n.º 7
0
        public void WrapForServerExecute_ProcessRequest_DelegatesCorrectly()
        {
            // Arrange
            HttpContext         httpContext     = GetHttpContext();
            Mock <IHttpHandler> mockHttpHandler = new Mock <IHttpHandler>();

            mockHttpHandler.Setup(o => o.ProcessRequest(httpContext)).Verifiable();

            IHttpHandler wrapper = HttpHandlerUtil.WrapForServerExecute(mockHttpHandler.Object);

            // Act
            wrapper.ProcessRequest(httpContext);

            // Assert
            mockHttpHandler.Verify();
        }
Exemplo n.º 8
0
        protected override void DoRender(HtmlTextWriter output)
        {
            var rendering = global::Sitecore.Context.Page.GetRenderingReference(this);

            if (rendering == null)
            {
                return;
            }

            var action = ControllerAction.GetControllerAction(MvcSubLayoutDataProvider.parentId, rendering.RenderingID);

            if (action == null)
            {
                return;
            }

            var httpContext           = new HttpContextWrapper(Context);
            var existingRouteData     = RouteTable.Routes.GetRouteData(httpContext);
            var additionalRouteValues = existingRouteData == null ? new RouteValueDictionary() : existingRouteData.Values;

            if (!string.IsNullOrEmpty(DataSource))
            {
                var item = rendering.Database.GetItem(DataSource);
                if (item != null)
                {
                    additionalRouteValues["_sitecoreitem"] = item;
                }
            }
            additionalRouteValues["_sitecorerendering"] = rendering;
            var parameters = WebUtil.ParseUrlParameters(Parameters);

            foreach (var key in parameters.AllKeys)
            {
                additionalRouteValues[key] = parameters[key];
            }

            var routeData = MvcActionHelper.GetRouteData(
                httpContext,
                action.ActionName,
                action.ControllerType.ControllerName,
                additionalRouteValues,
                true
                );
            var handler = new MvcHandler(new RequestContext(httpContext, routeData));

            httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(handler), output, true /* preserveForm */);
        }
Exemplo n.º 9
0
        public void WrapForServerExecute_EndProcessRequest_DelegatesCorrectly()
        {
            // Arrange
            IAsyncResult asyncResult = new Mock <IAsyncResult>().Object;

            HttpContext httpContext = GetHttpContext();
            Mock <IHttpAsyncHandler> mockHttpHandler = new Mock <IHttpAsyncHandler>();

            mockHttpHandler.Setup(o => o.EndProcessRequest(asyncResult)).Verifiable();

            IHttpAsyncHandler wrapper = (IHttpAsyncHandler)HttpHandlerUtil.WrapForServerExecute(mockHttpHandler.Object);

            // Act
            wrapper.EndProcessRequest(asyncResult);

            // Assert
            mockHttpHandler.Verify();
        }
Exemplo n.º 10
0
        public void WrapForServerExecute_BeginProcessRequest_DelegatesCorrectly()
        {
            // Arrange
            IAsyncResult  expectedResult = new Mock <IAsyncResult>().Object;
            AsyncCallback cb             = delegate { };

            HttpContext httpContext = GetHttpContext();
            Mock <IHttpAsyncHandler> mockHttpHandler = new Mock <IHttpAsyncHandler>();

            mockHttpHandler.Setup(o => o.BeginProcessRequest(httpContext, cb, "extraData")).Returns(expectedResult);

            IHttpAsyncHandler wrapper = (IHttpAsyncHandler)HttpHandlerUtil.WrapForServerExecute(mockHttpHandler.Object);

            // Act
            IAsyncResult actualResult = wrapper.BeginProcessRequest(httpContext, cb, "extraData");

            // Assert
            Assert.Equal(expectedResult, actualResult);
        }
 private static void ExecuteHandler(TextWriter textWriter, HttpContextBase httpContext, IHttpHandler handler)
 {
     httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(handler), textWriter, true /* preserveForm */);
 }