Esempio n. 1
0
        public void RedirectToRoute_Execute_Calls_TempDataKeep()
        {
            // Arrange
            var tempData = new Mock <ITempDataDictionary>();

            tempData.Setup(t => t.Keep()).Verifiable();

            var httpContext = new Mock <HttpContext>();

            httpContext.Setup(o => o.Response).Returns(new Mock <HttpResponse>().Object);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(ITempDataDictionary))).Returns(tempData.Object);
            var actionContext = new ActionContext(httpContext.Object,
                                                  new RouteData(),
                                                  new ActionDescriptor());

            var result = new RedirectToRouteResult("SampleRoute", null)
            {
                UrlHelper = GetMockUrlHelper("SampleRoute")
            };

            // Act
            result.ExecuteResult(actionContext);

            // Assert
            tempData.Verify(t => t.Keep(), Times.Once());
        }
Esempio n. 2
0
        public void ExecuteResultCallsResponseRedirect()
        {
            // Arrange
            Mock <Controller>        mockController        = new Mock <Controller>();
            Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>();

            mockControllerContext
            .Setup(c => c.HttpContext.Request.ApplicationPath)
            .Returns("/somepath");
            mockControllerContext
            .Setup(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny <string>()))
            .Returns((string s) => s);
            mockControllerContext
            .Setup(c => c.HttpContext.Response.Redirect("/somepath/c/a/i", false))
            .Verifiable();
            mockControllerContext.Setup(c => c.Controller).Returns(mockController.Object);

            var values = new { Controller = "c", Action = "a", Id = "i" };
            RedirectToRouteResult result = new RedirectToRouteResult(
                new RouteValueDictionary(values)
                )
            {
                Routes = new RouteCollection()
                {
                    new Route("{controller}/{action}/{id}", null)
                },
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
Esempio n. 3
0
        public void ExecuteResultWithNullControllerContextThrows()
        {
            // Arrange
            var result = new RedirectToRouteResult(null);

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { result.ExecuteResult(null /* context */); },
                "context");
        }
Esempio n. 4
0
        public void ExecuteResultThrowsIfVirtualPathDataIsNull()
        {
            // Arrange
            var result = new RedirectToRouteResult(null)
            {
                Routes = new RouteCollection()
            };

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                delegate { result.ExecuteResult(ControllerContextTest.CreateEmptyContext()); },
                "No route in the route table matches the supplied values.");
        }
Esempio n. 5
0
        public void RedirectInChildActionThrows()
        {
            // Arrange
            RouteData routeData = new RouteData();

            routeData.DataTokens[ControllerContext.ParentActionViewContextToken] = new ViewContext();
            ControllerContext     context = new ControllerContext(new Mock <HttpContextBase>().Object, routeData, new Mock <ControllerBase>().Object);
            RedirectToRouteResult result  = new RedirectToRouteResult(new RouteValueDictionary());

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => result.ExecuteResult(context),
                "Child actions are not allowed to perform redirect actions.");
        }
Esempio n. 6
0
        public void ExecuteResultPreservesTempData()
        {
            // Arrange
            TempDataDictionary tempData = new TempDataDictionary();

            tempData["Foo"] = "Foo";
            tempData["Bar"] = "Bar";
            Mock <Controller> mockController = new Mock <Controller>()
            {
                CallBase = true
            };

            mockController.Object.TempData = tempData;
            Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>();

            mockControllerContext
            .Setup(c => c.HttpContext.Request.ApplicationPath)
            .Returns("/somepath");
            mockControllerContext
            .Setup(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny <string>()))
            .Returns((string s) => s);
            mockControllerContext
            .Setup(c => c.HttpContext.Response.Redirect("/somepath/c/a/i", false))
            .Verifiable();
            mockControllerContext.Setup(c => c.Controller).Returns(mockController.Object);

            var values = new { Controller = "c", Action = "a", Id = "i" };
            RedirectToRouteResult result = new RedirectToRouteResult(
                new RouteValueDictionary(values)
                )
            {
                Routes = new RouteCollection()
                {
                    new Route("{controller}/{action}/{id}", null)
                },
            };

            // Act
            object value = tempData["Foo"];

            result.ExecuteResult(mockControllerContext.Object);
            mockController.Object.TempData.Save(
                mockControllerContext.Object,
                new Mock <ITempDataProvider>().Object
                );

            // Assert
            Assert.True(tempData.ContainsKey("Foo"));
            Assert.True(tempData.ContainsKey("Bar"));
        }
        public override void ExecuteResult(ControllerContext context)
        {
            RouteValueDictionary routeDictionary = new RouteValueDictionary {
                { "controller", "Home" }, { "action", "Index" }
            };

            if (!string.IsNullOrEmpty(_homepageMessage))
            {
                routeDictionary.Add("message", _homepageMessage);
            }

            RedirectToRouteResult redirectResult = new RedirectToRouteResult(routeDictionary);

            redirectResult.ExecuteResult(context);
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ControllerContext context)
        {
            context.Controller.TempData[Alerts.Success] = string.IsNullOrEmpty(Id) ? "添加成功" : "编辑成功";

            foreach (var key in context.RequestContext.HttpContext.Request.QueryString.AllKeys.Where(a => !a.IsEmpty()))
            {
                RouteValues.Add(key, context.RequestContext.HttpContext.Request.QueryString[key]);
            }

            RouteValues.Add("action", string.IsNullOrEmpty(Id) ? "Create" : "Index");

            var result = new RedirectToRouteResult(RouteValues);

            result.ExecuteResult(context);
        }
Esempio n. 9
0
        public void RedirectInChildActionThrows()
        {
            // Arrange
            RouteData routeData = new RouteData();

            routeData.DataTokens[ControllerContext.PARENT_ACTION_VIEWCONTEXT] = new ViewContext();
            ControllerContext     context = new ControllerContext(new Mock <HttpContextBase>().Object, routeData, new Mock <ControllerBase>().Object);
            RedirectToRouteResult result  = new RedirectToRouteResult(new RouteValueDictionary());

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                () => result.ExecuteResult(context),
                MvcResources.RedirectAction_CannotRedirectInChildAction
                );
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ControllerContext context)
        {
            context.Controller.TempData[Alerts.Success] = Lang.DeleteSuccess;

            foreach (var key in context.RequestContext.HttpContext.Request.QueryString.AllKeys.Where(a => !a.IsEmpty()))
            {
                RouteValues.Add(key, context.RequestContext.HttpContext.Request.QueryString[key]);
            }

            RouteValues.Add("action", Index);

            var result = new RedirectToRouteResult(RouteValues);

            result.ExecuteResult(context);
        }
        public void ExecuteResult() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>();
            mockControllerContext.Expect(c => c.HttpContext.Request.ApplicationPath).Returns("/somepath");
            mockControllerContext.Expect(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns((string s) => s);
            mockControllerContext.Expect(c => c.HttpContext.Response.Redirect("/somepath/c/a/i", false)).Verifiable();

            var values = new { Controller = "c", Action = "a", Id = "i" };
            RedirectToRouteResult result = new RedirectToRouteResult(new RouteValueDictionary(values)) {
                Routes = new RouteCollection() { new Route("{controller}/{action}/{id}", null) },
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            mockControllerContext.Verify();
        }
Esempio n. 12
0
        public override void ExecuteResult(ControllerContext context)
        {
            // Set error statuc code as 403 Forbidden. For details see the following link: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
            SetErrorResponse(context, HttpStatusCode.Forbidden);
            var log = LogManager.GetCurrentClassLogger();

            log.Error(BuildErrorMessage(context));
            // HAL: It is tempopary implementation. Separate view should be implemented
            var route = new RouteValueDictionary(new Dictionary <string, object>
            {
                { Constants.RouteKeys.Controller, ErrorsController.Name },
                { Constants.RouteKeys.Action, ErrorsController.NoPermissionAction },
                { Constants.RouteKeys.AspxErrorPath, context.HttpContext.Request.RawUrl }
            });
            var result = new RedirectToRouteResult(route);

            result.ExecuteResult(context);
        }
Esempio n. 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ActionContext context)
        {
            foreach (var key in context.HttpContext.Request.Query)
            {
                RouteValues.Add(key.Key, key.Value);
            }

            RouteValues["action"] = Index;

            var result = new RedirectToRouteResult(RouteValues);

            var factory = context.HttpContext.RequestServices.GetRequiredService <ITempDataProvider>();

            var tempdata = new Dictionary <string, object> {
                { AlertType.Alerts.Success, "删除成功" }
            };                                                                                     // "删除成功" } };

            factory.SaveTempData(context.HttpContext, tempdata);

            result.ExecuteResult(context);
        }
        public void ExecuteResultWithNullControllerContextThrows() {
            // Arrange
            var result = new RedirectToRouteResult(null);

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    result.ExecuteResult(null /* context */);
                },
                "context");
        }
        public void RedirectInChildActionThrows()
        {
            // Arrange
            RouteData routeData = new RouteData();
            routeData.DataTokens[ControllerContext.ParentActionViewContextToken] = new ViewContext();
            ControllerContext context = new ControllerContext(new Mock<HttpContextBase>().Object, routeData, new Mock<ControllerBase>().Object);
            RedirectToRouteResult result = new RedirectToRouteResult(new RouteValueDictionary());

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => result.ExecuteResult(context),
                "Child actions are not allowed to perform redirect actions.");
        }
        public void ExecuteResultThrowsIfVirtualPathDataIsNull() {
            // Arrange
            var result = new RedirectToRouteResult(null) {
                Routes = new RouteCollection()
            };

            // Act & Assert
            ExceptionHelper.ExpectException<InvalidOperationException>(
                delegate {
                    result.ExecuteResult(ControllerContextTest.CreateEmptyContext());
                },
                "No route in the route table matches the supplied values.");
        }
        public void ExecuteResultPreservesTempData() {
            // Arrange
            TempDataDictionary tempData = new TempDataDictionary();
            tempData["Foo"] = "Foo";
            tempData["Bar"] = "Bar";
            Mock<Controller> mockController = new Mock<Controller>() { CallBase = true };
            mockController.Object.TempData = tempData;
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>();
            mockControllerContext.Expect(c => c.HttpContext.Request.ApplicationPath).Returns("/somepath");
            mockControllerContext.Expect(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns((string s) => s);
            mockControllerContext.Expect(c => c.HttpContext.Response.Redirect("/somepath/c/a/i", false)).Verifiable();
            mockControllerContext.Expect(c => c.Controller).Returns(mockController.Object);

            var values = new { Controller = "c", Action = "a", Id = "i" };
            RedirectToRouteResult result = new RedirectToRouteResult(new RouteValueDictionary(values)) {
                Routes = new RouteCollection() { new Route("{controller}/{action}/{id}", null) },
            };

            // Act
            object value = tempData["Foo"];
            result.ExecuteResult(mockControllerContext.Object);
            mockController.Object.TempData.Save(mockControllerContext.Object, new Mock<ITempDataProvider>().Object);

            // Assert
            Assert.IsTrue(tempData.ContainsKey("Foo"));
            Assert.IsTrue(tempData.ContainsKey("Bar"));
        }
        public void RedirectToRoute_Execute_Calls_TempDataKeep()
        {
            // Arrange
            var tempData = new Mock<ITempDataDictionary>();
            tempData.Setup(t => t.Keep()).Verifiable();

            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(o => o.Response).Returns(new Mock<HttpResponse>().Object);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(ITempDataDictionary))).Returns(tempData.Object);
            var actionContext = new ActionContext(httpContext.Object,
                                                  new RouteData(),
                                                  new ActionDescriptor());

            var result = new RedirectToRouteResult("SampleRoute", null)
            {
                UrlHelper = GetMockUrlHelper("SampleRoute")
            };

            // Act
            result.ExecuteResult(actionContext);

            // Assert
            tempData.Verify(t => t.Keep(), Times.Once());
        }
        public void RedirectInChildActionThrows() {
            // Arrange
            RouteData routeData = new RouteData();
            routeData.DataTokens[ControllerContext.PARENT_ACTION_VIEWCONTEXT] = new ViewContext();
            ControllerContext context = new ControllerContext(new Mock<HttpContextBase>().Object, routeData, new Mock<ControllerBase>().Object);
            RedirectToRouteResult result = new RedirectToRouteResult(new RouteValueDictionary());

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                () => result.ExecuteResult(context),
                MvcResources.RedirectAction_CannotRedirectInChildAction
            );
        }