public void Execute_Throws_ForNonLocalUrlTilde(
            string appRoot,
            string contentPath,
            string expectedPath)
        {
            // Arrange
            var httpResponse = new Mock <HttpResponse>();

            httpResponse.Setup(o => o.Redirect(expectedPath, false))
            .Verifiable();

            var httpContext   = GetHttpContext(appRoot, contentPath, expectedPath, httpResponse.Object);
            var actionContext = GetActionContext(httpContext);
            var result        = new LocalRedirectResult(contentPath);

            var relaxedLocalRedirectValidation = false;
            var success = AppContext.TryGetSwitch(UrlHelper.UseRelaxedLocalRedirectValidationSwitch, out relaxedLocalRedirectValidation);

            // Act & Assert
            if (relaxedLocalRedirectValidation)
            {
                result.ExecuteResult(actionContext);
                httpResponse.Verify();
            }
            else
            {
                var exception = Assert.Throws <InvalidOperationException>(() => result.ExecuteResult(actionContext));
                Assert.Equal(
                    "The supplied URL is not local. A URL with an absolute path is considered local if it does not " +
                    "have a host/authority part. URLs using virtual paths ('~/') are also local.",
                    exception.Message);
            }
        }
        public void Constructor_WithParameterUrlAndPermanent_SetsResultUrlAndPermanent()
        {
            // Arrange
            var url = "/test/url";

            // Act
            var result = new LocalRedirectResult(url, permanent: true);

            // Assert
            Assert.True(result.Permanent);
            Assert.Same(url, result.Url);
        }
        public void Constructor_WithParameterUrl_SetsResultUrlAndNotPermanentOrPreserveMethod()
        {
            // Arrange
            var url = "/test/url";

            // Act
            var result = new LocalRedirectResult(url);

            // Assert
            Assert.False(result.PreserveMethod);
            Assert.False(result.Permanent);
            Assert.Same(url, result.Url);
        }
示例#4
0
        public async Task Execute_Throws_ForNonLocalUrl(
            string appRoot,
            string contentPath)
        {
            // Arrange
            var httpContext   = GetHttpContext(appRoot);
            var actionContext = GetActionContext(httpContext);
            var result        = new LocalRedirectResult(contentPath);

            // Act & Assert
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => result.ExecuteResultAsync(actionContext));

            Assert.Equal(
                "The supplied URL is not local. A URL with an absolute path is considered local if it does not " +
                "have a host/authority part. URLs using virtual paths ('~/') are also local.",
                exception.Message);
        }
示例#5
0
        public async Task Execute_ReturnsExpectedValues()
        {
            // Arrange
            var appRoot      = "/";
            var contentPath  = "~/Home/About";
            var expectedPath = "/Home/About";

            var httpContext   = GetHttpContext(appRoot);
            var actionContext = GetActionContext(httpContext);
            var result        = new LocalRedirectResult(contentPath);

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            Assert.Equal(expectedPath, httpContext.Response.Headers.Location.ToString());
            Assert.Equal(StatusCodes.Status302Found, httpContext.Response.StatusCode);
        }
        public void Execute_ReturnsExpectedValues()
        {
            // Arrange
            var appRoot      = "/";
            var contentPath  = "~/Home/About";
            var expectedPath = "/Home/About";
            var httpResponse = new Mock <HttpResponse>();

            httpResponse.Setup(o => o.Redirect(expectedPath, false))
            .Verifiable();

            var httpContext   = GetHttpContext(appRoot, contentPath, expectedPath, httpResponse.Object);
            var actionContext = GetActionContext(httpContext);
            var result        = new LocalRedirectResult(contentPath);

            // Act
            result.ExecuteResult(actionContext);

            // Assert
            httpResponse.Verify();
        }
 public static LinkGenerationTestContext FromLocalRedirectResult(LocalRedirectResult localRedirectResult)
 {
     return new LinkGenerationTestContext
     {
         Location = localRedirectResult.Url,
         UrlHelper = localRedirectResult.UrlHelper
     };
 }