public async void RedirectToRoute_Execute_PassesCorrectValuesToRedirect(object values)
        {
            // Arrange
            var expectedUrl = "SampleAction";
            var expectedPermanentFlag = false;
            var httpContext = new Mock<HttpContext>();
            var httpResponse = new Mock<HttpResponse>();
            httpContext.Setup(o => o.Response).Returns(httpResponse.Object);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(ITempDataDictionary)))
                .Returns(Mock.Of<ITempDataDictionary>());

            var actionContext = new ActionContext(httpContext.Object,
                                                  new RouteData(),
                                                  new ActionDescriptor());

            var urlHelper = GetMockUrlHelper(expectedUrl);
            var result = new RedirectToRouteResult(null, TypeHelper.ObjectToDictionary(values))
            {
                UrlHelper = urlHelper,
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // Verifying if Redirect was called with the specific Url and parameter flag.
            // Thus we verify that the Url returned by UrlHelper is passed properly to
            // Redirect method and that the method is called exactly once.
            httpResponse.Verify(r => r.Redirect(expectedUrl, expectedPermanentFlag), Times.Exactly(1));
        }
Beispiel #2
0
        public async Task ExecuteResultAsync_UsesRouteName_ToGenerateLocationHeader()
        {
            // Arrange
            var routeName   = "orders_api";
            var locationUrl = "/api/orders/10";
            var urlHelper   = new Mock <IUrlHelper>();

            urlHelper.Setup(uh => uh.RouteUrl(It.IsAny <UrlRouteContext>()))
            .Returns(locationUrl)
            .Verifiable();

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(sp => sp.GetService(typeof(IUrlHelper)))
            .Returns(urlHelper.Object);
            serviceProvider.Setup(sp => sp.GetService(typeof(ITempDataDictionary)))
            .Returns(new Mock <ITempDataDictionary>().Object);
            var httpContext = new DefaultHttpContext();

            httpContext.RequestServices = serviceProvider.Object;
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var result        = new RedirectToRouteResult(routeName, new { id = 10 });

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            urlHelper.Verify(uh => uh.RouteUrl(
                                 It.Is <UrlRouteContext>(routeContext => string.Equals(routeName, routeContext.RouteName))));
            Assert.True(httpContext.Response.Headers.ContainsKey("Location"), "Location header not found");
            Assert.Equal(locationUrl, httpContext.Response.Headers["Location"]);
        }
Beispiel #3
0
        public async void RedirectToRoute_Execute_PassesCorrectValuesToRedirect(object values)
        {
            // Arrange
            var expectedUrl           = "SampleAction";
            var expectedPermanentFlag = false;
            var httpContext           = new Mock <HttpContext>();
            var httpResponse          = new Mock <HttpResponse>();

            httpContext.Setup(o => o.Response).Returns(httpResponse.Object);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(ITempDataDictionary)))
            .Returns(Mock.Of <ITempDataDictionary>());

            var actionContext = new ActionContext(httpContext.Object,
                                                  new RouteData(),
                                                  new ActionDescriptor());

            var urlHelper = GetMockUrlHelper(expectedUrl);
            var result    = new RedirectToRouteResult(null, PropertyHelper.ObjectToDictionary(values))
            {
                UrlHelper = urlHelper,
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // Verifying if Redirect was called with the specific Url and parameter flag.
            // Thus we verify that the Url returned by UrlHelper is passed properly to
            // Redirect method and that the method is called exactly once.
            httpResponse.Verify(r => r.Redirect(expectedUrl, expectedPermanentFlag), Times.Exactly(1));
        }
    public async Task ExecuteResultAsync_UsesRouteName_ToGenerateLocationHeader()
    {
        // Arrange
        var routeName   = "orders_api";
        var locationUrl = "/api/orders/10";

        var urlHelper = new Mock <IUrlHelper>();

        urlHelper
        .Setup(uh => uh.RouteUrl(It.IsAny <UrlRouteContext>()))
        .Returns(locationUrl)
        .Verifiable();
        var factory = new Mock <IUrlHelperFactory>();

        factory
        .Setup(f => f.GetUrlHelper(It.IsAny <ActionContext>()))
        .Returns(urlHelper.Object);

        var httpContext = GetHttpContext(factory.Object);

        var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
        var result        = new RedirectToRouteResult(routeName, new { id = 10 });

        // Act
        await result.ExecuteResultAsync(actionContext);

        // Assert
        urlHelper.Verify(uh => uh.RouteUrl(
                             It.Is <UrlRouteContext>(routeContext => string.Equals(routeName, routeContext.RouteName))));
        Assert.True(httpContext.Response.Headers.ContainsKey("Location"), "Location header not found");
        Assert.Equal(locationUrl, httpContext.Response.Headers["Location"]);
    }
    public async Task RedirectToRoute_Execute_ThrowsOnNullUrl()
    {
        // Arrange
        var httpContext = new Mock <HttpContext>();

        httpContext
        .Setup(o => o.Response)
        .Returns(new Mock <HttpResponse>().Object);
        httpContext
        .SetupGet(o => o.RequestServices)
        .Returns(CreateServices().BuildServiceProvider());

        var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());

        var urlHelper = GetMockUrlHelper(returnValue: null);
        var result    = new RedirectToRouteResult(null, new Dictionary <string, object>())
        {
            UrlHelper = urlHelper,
        };

        // Act & Assert
        await ExceptionAssert.ThrowsAsync <InvalidOperationException>(
            async() =>
        {
            await result.ExecuteResultAsync(actionContext);
        },
            "No route matches the supplied values.");
    }
Beispiel #6
0
        //public override void ExecuteResult(ActionContext context)
        //{
        //    foreach (var key in context.HttpContext.Request.Query)
        //    {
        //        RouteValues.Add(key.Key, key.Value);
        //    }

        //    RouteValues["action"] = Id == null ? "Create" : "Index";

        //    var result = new RedirectToRouteResult(RouteValues);

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

        //    var iStringLocalizer = context.HttpContext.RequestServices.GetRequiredService<IStringLocalizer<Resources.Lang>>();

        //    var tempdata = new Dictionary<string, object> { { AlertType.Alerts.Success, Id == null ? iStringLocalizer["AddSuccess"].ToString() : iStringLocalizer["EditSuccess"].ToString() } };

        //    factory.SaveTempData(context.HttpContext, tempdata);

        //    result.ExecuteResult(context);

        //    //base.ExecuteResult(context);
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task ExecuteResultAsync(ActionContext context)
        {
            foreach (var key in context.HttpContext.Request.Query)
            {
                RouteValues.Add(key.Key, key.Value);
            }

            RouteValues["action"] = Id == null ? "Create" : "Index";

            var result = new RedirectToRouteResult(RouteValues);

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

            var tempdata = new Dictionary <string, object> {
                { AlertType.Alerts.Success, Id == null ? "添加成功" : "编辑成功" }
            };

            factory.SaveTempData(context.HttpContext, tempdata);

            return(result.ExecuteResultAsync(context));
        }
Beispiel #7
0
        public void RedirectToRoute_Execute_ThrowsOnNullUrl()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(o => o.Response).Returns(new Mock<HttpResponse>().Object);
            var actionContext = new ActionContext(httpContext.Object,
                                                  new RouteData(),
                                                  new ActionDescriptor());

            IUrlHelper urlHelper = GetMockUrlHelper(returnValue: null);
            RedirectToRouteResult result = new RedirectToRouteResult(urlHelper, 
                                                                     null,
                                                                     new Dictionary<string, object>());

            // Act & Assert
            ExceptionAssert.ThrowsAsync<InvalidOperationException>(
                async () =>
                {
                    await result.ExecuteResultAsync(actionContext);
                },
                "No route matches the supplied values.");
        }
        public void RedirectToRoute_Execute_ThrowsOnNullUrl()
        {
            // Arrange
            var httpContext = new Mock <HttpContext>();

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

            IUrlHelper            urlHelper = GetMockUrlHelper(returnValue: null);
            RedirectToRouteResult result    = new RedirectToRouteResult(urlHelper,
                                                                        null,
                                                                        new Dictionary <string, object>());

            // Act & Assert
            ExceptionAssert.ThrowsAsync <InvalidOperationException>(
                async() =>
            {
                await result.ExecuteResultAsync(actionContext);
            },
                "No route matches the supplied values.");
        }
    public async Task ExecuteResultAsync_WithFragment_PassesCorrectValuesToRedirect_WithPreserveMethod()
    {
        // Arrange
        var expectedUrl        = "/SampleAction#test";
        var expectedStatusCode = StatusCodes.Status308PermanentRedirect;

        var httpContext = GetHttpContext();

        var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

        var urlHelper = GetMockUrlHelper(expectedUrl);
        var result    = new RedirectToRouteResult("Sample", null, true, true, "test")
        {
            UrlHelper = urlHelper,
        };

        // Act
        await result.ExecuteResultAsync(actionContext);

        // Assert
        Assert.Equal(expectedStatusCode, httpContext.Response.StatusCode);
        Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
    }
        public async Task ExecuteResultAsync_UsesRouteName_ToGenerateLocationHeader()
        {
            // Arrange
            var routeName = "orders_api";
            var locationUrl = "/api/orders/10";
            var urlHelper = new Mock<IUrlHelper>();
            urlHelper.Setup(uh => uh.RouteUrl(It.IsAny<UrlRouteContext>()))
                .Returns(locationUrl)
                .Verifiable();

            var serviceProvider = new Mock<IServiceProvider>();
            serviceProvider.Setup(sp => sp.GetService(typeof(IUrlHelper)))
                .Returns(urlHelper.Object);
            serviceProvider.Setup(sp => sp.GetService(typeof(ITempDataDictionary)))
                .Returns(new Mock<ITempDataDictionary>().Object);
            var httpContext = new DefaultHttpContext();
            httpContext.RequestServices = serviceProvider.Object;
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var result = new RedirectToRouteResult(routeName, new { id = 10 });

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            urlHelper.Verify(uh => uh.RouteUrl(
                It.Is<UrlRouteContext>(routeContext => string.Equals(routeName, routeContext.RouteName))));
            Assert.True(httpContext.Response.Headers.ContainsKey("Location"), "Location header not found");
            Assert.Equal(locationUrl, httpContext.Response.Headers["Location"]);
        }