public void GetHttpHandler_TargetUrl_Does_Not_Contain_QueryString_When_Flag_Is_False()
        {
            // Arrange
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.Path).Returns("/foo");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            var query = "?bar=baz";
            httpContext.Setup(c => c.Request.QueryString).Returns(HttpUtility.ParseQueryString(query));
            var requestContext = new RequestContext(httpContext.Object, new RouteData());

            var sourceRouteMock = new Mock<RouteBase>();
            sourceRouteMock.Setup(r => r.GetVirtualPath(It.IsAny<RequestContext>(), It.IsAny<RouteValueDictionary>())).Returns(new VirtualPathData(sourceRouteMock.Object, "/foo"));
            var targetRoute = new Mock<RouteBase>();
            targetRoute.Setup(r => r.GetVirtualPath(It.IsAny<RequestContext>(), It.IsAny<RouteValueDictionary>())).Returns(new VirtualPathData(sourceRouteMock.Object, "/bar"));
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, targetRoute.Object, permanent: true);
            redirectRoute.IncludeQueryStringInRedirect = false;

            // Act
            var handler = redirectRoute.GetHttpHandler(requestContext);

            // Assert
            Assert.IsType(typeof(RedirectHttpHandler), handler);
            PAssert.IsTrue(() => !((RedirectHttpHandler)handler).TargetUrl.EndsWith(query));
        }
Example #2
0
        public void To_WhenAdditionalRouteValuesAlreadySet_ThrowsException()
        {
            // Arrange
            var redirectRoute = new RedirectRoute(new Mock<RouteBase>().Object, null, false, new RouteValueDictionary());

            // Act, Assert
            Assert.Throws<InvalidOperationException>(() => redirectRoute.To(new Mock<RouteBase>().Object));
        }
        public void To_WithTargetRoute_SetsTargetRoute() {
            // Arrange
            var sourceRoute = new Mock<RouteBase>().Object;
            var targetRoute = new Mock<RouteBase>().Object;
            var redirectRoute = new RedirectRoute(sourceRoute, null, false);

            // Act
            redirectRoute.To(targetRoute);

            // Assert
            PAssert.IsTrue(() => redirectRoute.TargetRoute == targetRoute);
        }
        // We always want to map the RedirectRoute *BEFORE* the legacy route that we're going to redirect.
        // Otherwise the redirect route will never match because the legacy route will supersede it.
        // Hence the Func<RouteCollection, RouteBase>.
        public static RedirectRoute Redirect(this RouteCollection routes, Func<RouteCollection, RouteBase> routeMapping, bool permanent = false)
        {
            if (routes == null) {
                throw new ArgumentNullException("routes");
            }
            if (routeMapping == null) {
                throw new ArgumentNullException("routeMapping");
            }

            var routeCollection = new RouteCollection();
            var legacyRoute = routeMapping(routeCollection);

            var redirectRoute = new RedirectRoute(legacyRoute, null, permanent, null);
            routes.Add(new NormalizeRoute(redirectRoute));
            return redirectRoute;
        }
        public void GetRouteData_WithNonMatchingSourceRoute_ReturnsNull() {
            // Arrange
            var sourceRouteMock = new Mock<RouteBase>();
            var targetRoute = new Mock<RouteBase>().Object;
            sourceRouteMock.Setup(r => r.GetRouteData(It.IsAny<HttpContextBase>())).Returns((RouteData)null);
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, targetRoute, permanent: true);

            // Act
            var routeData = redirectRoute.GetRouteData(new Mock<HttpContextBase>().Object);

            // Assert
            PAssert.IsTrue(() => routeData == null);
            // These next few asserts are "laziness" asserts.
            PAssert.IsTrue(() => redirectRoute.Permanent == true);
            PAssert.IsTrue(() => redirectRoute.SourceRoute == sourceRouteMock.Object);
            PAssert.IsTrue(() => redirectRoute.TargetRoute == targetRoute);
        }
        public void GetRouteData_WithMatchingSourceRoute_SwapsRouteHandler() {
            // Arrange
            var routeData = new RouteData();
            var routeHandler = new Mock<IRouteHandler>().Object;
            routeData.RouteHandler = routeHandler;
            var sourceRouteMock = new Mock<RouteBase>();
            sourceRouteMock.Setup(r => r.GetRouteData(It.IsAny<HttpContextBase>())).Returns(routeData);
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, new Mock<RouteBase>().Object, permanent: false);

            // Act
            var redirectRouteData = redirectRoute.GetRouteData(new Mock<HttpContextBase>().Object);

            // Assert
            PAssert.IsTrue(() => redirectRouteData.RouteHandler != routeHandler);
            PAssert.IsTrue(() => redirectRouteData.RouteHandler is RedirectRoute);
            PAssert.IsTrue(() => redirectRouteData == routeData);
            PAssert.IsTrue(() => redirectRoute.Permanent == false);
        }
        public void GetHttpHandler_WithTargetRoute_ButRequestDoesNotMatchTargetRouteReturnsDelegateHttpHandler()
        {
            // Arrange
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.Path).Returns("/qux");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/qux");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            httpContext.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s);
            var routeData = new RouteData();
            var requestContext = new RequestContext(httpContext.Object, new RouteData());
            var targetRoute = new Route("foo/{bar}", null);
            var redirectRouteHandler = new RedirectRoute(null, targetRoute, permanent: false, additionalRouteValues: null);

            // Act
            var httpHandler = redirectRouteHandler.GetHttpHandler(requestContext) as DelegateHttpHandler;

            // Assert
            PAssert.IsTrue(() => httpHandler != null);
        }
        public void GetVirtualPath_WithMatchingRequest_AlwaysReturnsNull() {
            // Arrange
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.Path).Returns("/foo");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            var requestContext = new RequestContext(httpContext.Object, new RouteData());

            var sourceRouteMock = new Mock<RouteBase>();
            sourceRouteMock.Setup(r => r.GetVirtualPath(It.IsAny<RequestContext>(), It.IsAny<RouteValueDictionary>())).Returns(new VirtualPathData(sourceRouteMock.Object, "/foo"));
            var targetRoute = new Mock<RouteBase>().Object;
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, targetRoute, permanent: true);

            // Act
            var virtualPath = redirectRoute.GetVirtualPath(requestContext, new RouteValueDictionary());

            // Assert
            PAssert.IsTrue(() => virtualPath == null);
        }
        public void GetHttpHandler_WithTargetRouteAndAdditionalRouteData_MergesAdditionalRouteValues() {
            // Arrange
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.Path).Returns("/qux");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/qux");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            httpContext.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s);
            var routeData = new RouteData();
            routeData.Values.Add("bar", "the-value-of-bar");
            var requestContext = new RequestContext(httpContext.Object, routeData);
            var targetRoute = new Route("foo/{bar}/{baz}", null);
            var additionalRouteValues = new RouteValueDictionary();
            additionalRouteValues.Add("baz", "bizaz");
            var redirectRouteHandler = new RedirectRoute(null, targetRoute, false, additionalRouteValues);

            // Act
            var httpHandler = redirectRouteHandler.GetHttpHandler(requestContext) as RedirectHttpHandler;

            // Assert
            PAssert.IsTrue(() => httpHandler.TargetUrl == "~/foo/the-value-of-bar/bizaz");
        }
        public void To_WhenTargetRouteIsAlreadySet_ThrowsException() {
            // Arrange
            var redirectRoute = new RedirectRoute(new Mock<RouteBase>().Object, new Mock<RouteBase>().Object, false);

            // Act, Assert
            Assert.Throws<InvalidOperationException>(() => redirectRoute.To(new Mock<RouteBase>().Object));
        }
        public void To_WithNullTargetRoute_ThrowsException() {
            // Arrange
            var redirectRoute = new RedirectRoute(new Mock<RouteBase>().Object, null, false);

            // Act, Assert
            Assert.Throws<ArgumentNullException>(() => redirectRoute.To(null));
        }
        public void GetHttpHandler_WithQueryString_IsPreserved()
        {
            //Arrange
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.QueryString).Returns(new NameValueCollection
            {
                {"bar", "biz"},
                {"car", "cdr"}
            });

            var requestContext = new RequestContext(httpContext.Object, new RouteData());

            var toMockBase = new Mock<RouteBase>();
            toMockBase
                .Setup(r => r.GetVirtualPath(It.IsAny<RequestContext>(), It.IsAny<RouteValueDictionary>()))
                .Returns(new VirtualPathData(toMockBase.Object, "kittens"));

            var redirectRoute = new RedirectRoute(new Mock<RouteBase>().Object, toMockBase.Object, false);

            // Act
            var handler = redirectRoute.GetHttpHandler(requestContext);

            // Assert
            PAssert.IsTrue(() => ((RedirectHttpHandler)handler).TargetUrl == "~/kittens?bar=biz&car=cdr");
        }
        public void GetHttpHandler_OnRedirectionActionIsCalled()
        {
            // Arrange
            var onRedirectCalled = false;
            Action<RequestContext, RedirectRoute> onRedirection = (context, route) => onRedirectCalled = true;

            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.Path).Returns("/foo");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            var requestContext = new RequestContext(httpContext.Object, new RouteData());

            var redirectRoute = new RedirectRoute(new Mock<RouteBase>().Object, new Mock<RouteBase>().Object, false, null, onRedirection);

            // Act
            redirectRoute.GetHttpHandler(requestContext);

            // Assert
            PAssert.IsTrue(() => onRedirectCalled);
        }