Beispiel #1
0
        public async Task ExecuteAsync_WritesOutputWithoutBOM()
        {
            // Arrange
            var expected = new byte[] { 97, 98, 99, 100 };

            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                view.ToString();
                v.Writer.Write("abcd");
            })
            .Returns(Task.FromResult(0));

            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(context,
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await ViewExecutor.ExecuteAsync(view.Object, actionContext, viewData, contentType : null);

            // Assert
            Assert.Equal(expected, memoryStream.ToArray());
            Assert.Equal("text/html; charset=utf-8", context.Response.ContentType);
        }
Beispiel #2
0
        public async Task ExecuteAsync_DoesNotWriteToResponse_OnceExceptionIsThrown(int writtenLength, int expectedLength)
        {
            // Arrange
            var longString = new string('a', writtenLength);

            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                view.ToString();
                v.Writer.Write(longString);
                throw new Exception();
            });

            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(context,
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await Record.ExceptionAsync(
                () => ViewExecutor.ExecuteAsync(view.Object, actionContext, viewData, contentType: null));

            // Assert
            Assert.Equal(expectedLength, memoryStream.Length);
        }
Beispiel #3
0
        /// <inheritdoc />
        public override async Task ExecuteResultAsync([NotNull] ActionContext context)
        {
            var viewEngine = ViewEngine ??
                             context.HttpContext.RequestServices.GetRequiredService <ICompositeViewEngine>();

            var viewName = ViewName ?? context.ActionDescriptor.Name;
            var view     = viewEngine.FindView(context, viewName)
                           .EnsureSuccessful()
                           .View;

            using (view as IDisposable)
            {
                await ViewExecutor.ExecuteAsync(view, context, ViewData, contentType : null);
            }
        }
Beispiel #4
0
        /// <inheritdoc />
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var viewEngine = ViewEngine ??
                             context.HttpContext.RequestServices.GetRequiredService <ICompositeViewEngine>();

            var logger = context.HttpContext.RequestServices.GetRequiredService <ILogger <PartialViewResult> >();

            var options = context.HttpContext.RequestServices.GetRequiredService <IOptions <MvcViewOptions> >();

            var viewName         = ViewName ?? context.ActionDescriptor.Name;
            var viewEngineResult = viewEngine.FindPartialView(context, viewName);

            if (!viewEngineResult.Success)
            {
                logger.LogError(
                    "The partial view '{PartialViewName}' was not found. Searched locations: {SearchedViewLocations}",
                    viewName,
                    viewEngineResult.SearchedLocations);
            }

            var view = viewEngineResult.EnsureSuccessful().View;

            logger.LogVerbose("The partial view '{PartialViewName}' was found.", viewName);

            if (StatusCode != null)
            {
                context.HttpContext.Response.StatusCode = StatusCode.Value;
            }

            using (view as IDisposable)
            {
                await ViewExecutor.ExecuteAsync(
                    view,
                    context,
                    ViewData,
                    TempData,
                    options.Value.HtmlHelperOptions,
                    ContentType);
            }
        }
Beispiel #5
0
        public async Task ExecuteAsync_UsesSpecifiedContentType()
        {
            // Arrange
            var contentType  = "some-content-type";
            var view         = Mock.Of <IView>();
            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(context,
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await ViewExecutor.ExecuteAsync(view, actionContext, viewData, contentType);

            // Assert
            Assert.Equal(contentType, context.Response.ContentType);
        }
        public async Task ExecuteAsync_SetsContentTypeAndEncoding(
            MediaTypeHeaderValue contentType,
            string expectedContentType,
            byte[] expectedContentData)
        {
            // Arrange
            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                view.ToString();
                v.Writer.Write("abcd");
            })
            .Returns(Task.FromResult(0));

            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(context,
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await ViewExecutor.ExecuteAsync(
                view.Object,
                actionContext,
                viewData,
                null,
                new HtmlHelperOptions(),
                contentType);

            // Assert
            Assert.Equal(expectedContentType, context.Response.ContentType);
            Assert.Equal(expectedContentData, memoryStream.ToArray());
        }
Beispiel #7
0
        /// <inheritdoc />
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var services   = context.HttpContext.RequestServices;
            var viewEngine = ViewEngine ?? services.GetRequiredService <ICompositeViewEngine>();

            var logger    = services.GetRequiredService <ILogger <ViewResult> >();
            var telemetry = services.GetRequiredService <TelemetrySource>();

            var options = services.GetRequiredService <IOptions <MvcViewOptions> >();

            var viewName         = ViewName ?? context.ActionDescriptor.Name;
            var viewEngineResult = viewEngine.FindView(context, viewName);

            if (!viewEngineResult.Success)
            {
                if (telemetry.IsEnabled("Microsoft.AspNet.Mvc.ViewResultViewNotFound"))
                {
                    telemetry.WriteTelemetry(
                        "Microsoft.AspNet.Mvc.ViewResultViewNotFound",
                        new
                    {
                        actionContext     = context,
                        result            = this,
                        viewName          = viewName,
                        searchedLocations = viewEngineResult.SearchedLocations
                    });
                }

                logger.LogError(
                    "The view '{ViewName}' was not found. Searched locations: {SearchedViewLocations}",
                    viewName,
                    viewEngineResult.SearchedLocations);
            }

            var view = viewEngineResult.EnsureSuccessful().View;

            if (telemetry.IsEnabled("Microsoft.AspNet.Mvc.ViewResultViewFound"))
            {
                telemetry.WriteTelemetry(
                    "Microsoft.AspNet.Mvc.ViewResultViewFound",
                    new { actionContext = context, result = this, viewName, view = view });
            }

            logger.LogVerbose("The view '{ViewName}' was found.", viewName);

            if (StatusCode != null)
            {
                context.HttpContext.Response.StatusCode = StatusCode.Value;
            }

            using (view as IDisposable)
            {
                await ViewExecutor.ExecuteAsync(
                    view,
                    context,
                    ViewData,
                    TempData,
                    options.Value.HtmlHelperOptions,
                    ContentType);
            }
        }