Esempio n. 1
0
        public void Encode_returnsEncodedValue()
        {
            // Arrange
            var encoder = new CommonTestEncoder();
            var input   = "TestValue";

            // Act
            var htmlEncodedValue             = encoder.HtmlEncode(input);
            var javaScriptStringEncodedValue = encoder.JavaScriptStringEncode(input);
            var urlEncodedValue = encoder.UrlEncode(input);

            // Assert
            Assert.Equal($"HtmlEncode[[{input}]]", htmlEncodedValue);
            Assert.Equal($"JavaScriptStringEncode[[{input}]]", javaScriptStringEncodedValue);
            Assert.Equal($"UrlEncode[[{input}]]", urlEncodedValue);
        }
Esempio n. 2
0
        public async Task FlushAsync_DoesNotThrowWhenInvokedInsideOfASection()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var expected = "HtmlEncode[[layout-1" +
                           Environment.NewLine +
                           "]]section-content-1" +
                           Environment.NewLine +
                           "section-content-2";

            var page = new TestableRazorPage(v =>
           {
               v.HtmlEncoder = htmlEncoder;
               v.Layout = "layout-1";
               v.DefineSection("foo", async _ =>
               {
                   v.WriteLiteral("section-content-1" + Environment.NewLine);
                   await v.FlushAsync();
                   v.WriteLiteral("section-content-2");
               });
           });

            var layout1 = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-1" + Environment.NewLine);
                v.RenderBodyPublic();
                v.Write(v.RenderSection("foo"));
            });

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "layout-1"))
                       .Returns(new RazorPageResult("layout-1", layout1));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
Esempio n. 3
0
        public async Task RenderAsync_ExecutesNestedLayoutsWithNestedSections()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var expected = "HtmlEncode[[BaseLayout" +
                           Environment.NewLine +
                           "]]HtmlEncode[[NestedLayout" +
                           Environment.NewLine +
                           "]]BodyContent" +
                           "foo-content" +
                           Environment.NewLine +
                           "HtmlEncode[[]]" +
                           Environment.NewLine;

            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout1.cshtml";
                v.WriteLiteral("BodyContent");
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("foo-content");
                });
            });
            var nestedLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout2.cshtml";
                v.Write("NestedLayout" + Environment.NewLine);
                v.RenderBodyPublic();
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync(htmlEncoder.HtmlEncode(v.RenderSection("foo").ToString()));
                });
            });
            var baseLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("BaseLayout" + Environment.NewLine);
                v.RenderBodyPublic();
                v.Write(v.RenderSection("foo"));
            });
            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout1.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout1.cshtml", nestedLayout));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout2.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout2.cshtml", baseLayout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
Esempio n. 4
0
        public async Task RenderAsync_WithNestedSectionsOfTheSameName_ThrowsIfSectionsWereDefinedButNotRendered()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout1.cshtml";
                v.WriteLiteral("BodyContent");
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("foo-content");
                });
            })
            {
                Path = "Page"
            };

            var nestedLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout2.cshtml";
                v.Write("NestedLayout" + Environment.NewLine);
                v.RenderBodyPublic();
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("dont-render-inner-foo");
                });
            })
            {
                Path = "/Shared/Layout1.cshtml"
            };

            var baseLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("BaseLayout" + Environment.NewLine);
                v.RenderBodyPublic();
                v.RenderSection("foo");
            })
            {
                Path = "/Shared/Layout2.cshtml"
            };

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout1.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout1.cshtml", nestedLayout));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout2.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout2.cshtml", baseLayout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act and Assert
            var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => view.RenderAsync(viewContext));
            Assert.Equal("The following sections have been defined but have not been rendered by the page at " +
                "'/Shared/Layout1.cshtml': 'foo'.", ex.Message);
        }
Esempio n. 5
0
        public async Task RenderAsync_SucceedsIfRenderBodyIsNotInvoked_ButAllSectionsAreRendered()
        {
            // Arrange
            var expected = string.Join(
                Environment.NewLine,
                "layout-section-content",
                "page-section-content");

            var htmlEncoder = new CommonTestEncoder();
            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "NestedLayout";
                v.WriteLiteral("Page body content that will not be written");
                v.DefineSection("sectionA", async writer =>
                {
                    await writer.WriteAsync("page-section-content");
                });
            });
            var nestedLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "Layout";
                v.WriteLiteral("Nested layout content that will not be written");
                v.DefineSection("sectionB", async writer =>
                {
                    await writer.WriteLineAsync("layout-section-content");
                    await v.RenderSectionAsync("sectionA");
                });
            });
            var baseLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.RenderSection("sectionB");
            });

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "NestedLayout"))
                       .Returns(new RazorPageResult("NestedLayout", nestedLayout));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "Layout"))
                       .Returns(new RazorPageResult("Layout", baseLayout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
Esempio n. 6
0
        public async Task RenderAsync_SucceedsIfNestedSectionsAreRendered()
        {
            // Arrange
            var expected = string.Join(
                Environment.NewLine,
                "layout-section-content",
                "page-section-content");

            var htmlEncoder = new CommonTestEncoder();
            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout1.cshtml";
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteAsync("page-section-content");
                });
            });
            var nestedLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout2.cshtml";
                v.RenderBodyPublic();
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("layout-section-content");
                    await v.RenderSectionAsync("foo");
                });
            })
            {
                Path = "/Shared/Layout1.cshtml"
            };
            var baseLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.RenderBodyPublic();
                v.RenderSection("foo");
            })
            {
                Path = "/Shared/Layout2.cshtml"
            };

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout1.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout1.cshtml", nestedLayout));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout2.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout2.cshtml", baseLayout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
Esempio n. 7
0
        public async Task RenderAsync_ExecutesLayoutPages()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var htmlEncodedNewLine = htmlEncoder.HtmlEncode(Environment.NewLine);
            var expected = "HtmlEncode[[layout-content" +
                           Environment.NewLine +
                           "]]head-content" +
                           htmlEncodedNewLine +
                           "body-content" +
                           htmlEncodedNewLine +
                           "foot-content";

            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.WriteLiteral("body-content");
                v.Layout = LayoutPath;
                v.DefineSection("head", async writer =>
                {
                    await writer.WriteAsync("head-content");
                });
                v.DefineSection("foot", async writer =>
                {
                    await writer.WriteAsync("foot-content");
                });
            });
            var layout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-content" + Environment.NewLine);
                v.Write(v.RenderSection("head"));
                v.Write(Environment.NewLine);
                v.RenderBodyPublic();
                v.Write(Environment.NewLine);
                v.Write(v.RenderSection("foot"));
            });
            var activator = new Mock<IRazorPageActivator>();
            activator.Setup(a => a.Activate(page, It.IsAny<ViewContext>()))
                     .Verifiable();
            activator.Setup(a => a.Activate(layout, It.IsAny<ViewContext>()))
                     .Verifiable();
            var viewEngine = new Mock<IRazorViewEngine>();

            var view = new RazorView(viewEngine.Object,
                                     activator.Object,
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);
            viewEngine.Setup(p => p.FindPage(viewContext, LayoutPath))
                       .Returns(new RazorPageResult(LayoutPath, layout))
                       .Verifiable();

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            // Verify the activator was invoked for the primary page and layout page.
            activator.Verify();
            Assert.Equal(expected, viewContext.Writer.ToString());
            viewEngine.Verify();
        }
Esempio n. 8
0
        public async Task RenderAsync_AsPartial_ExecutesLayout_ButNotViewStartPages()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var expected = string.Join(Environment.NewLine,
                                       "HtmlEncode[[layout-content",
                                       "]]HtmlEncode[[page-content]]");
            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = LayoutPath;
                v.Write("page-content");
            });

            var layout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-content" + Environment.NewLine);
                v.RenderBodyPublic();
            });
            var pageFactory = new Mock<IRazorPageFactory>();
            pageFactory.Setup(p => p.CreateInstance(LayoutPath))
                       .Returns(layout);

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(v => v.FindPage(It.IsAny<ActionContext>(), LayoutPath))
                      .Returns(new RazorPageResult(LayoutPath, layout));

            var viewStartProvider = CreateViewStartProvider();
            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     viewStartProvider,
                                     page,
                                     isPartial: true);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Mock.Get(viewStartProvider)
                .Verify(v => v.GetViewStartPages(It.IsAny<string>()), Times.Never());
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
Esempio n. 9
0
 public TestableRazorPage()
 {
     HtmlEncoder = new CommonTestEncoder();
 }
Esempio n. 10
0
        public async Task RenderAsync_ExecutesNestedLayoutPages()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var expected = "HtmlEncode[[layout-2" +
                           Environment.NewLine +
                           "]]bar-content" +
                           Environment.NewLine +
                           "HtmlEncode[[layout-1" +
                           Environment.NewLine +
                           "]]foo-content" +
                           Environment.NewLine +
                           "body-content";

            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("foo-content");
                });
                v.Layout = "~/Shared/Layout1.cshtml";
                v.WriteLiteral("body-content");
            });
            var layout1 = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-1" + Environment.NewLine);
                v.Write(v.RenderSection("foo"));
                v.DefineSection("bar", writer => writer.WriteLineAsync("bar-content"));
                v.RenderBodyPublic();
                v.Layout = "~/Shared/Layout2.cshtml";
            });
            layout1.Path = "~/Shared/Layout1.cshtml";

            var layout2 = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-2" + Environment.NewLine);
                v.Write(v.RenderSection("bar"));
                v.RenderBodyPublic();
            });
            layout2.Path = "~/Shared/Layout2.cshtml";

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout1.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout1.cshtml", layout1));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout2.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout2.cshtml", layout2));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     new CommonTestEncoder(),
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
Esempio n. 11
0
 public TestableRazorPage(Action<TestableRazorPage> executeAction)
 {
     _executeAction = executeAction;
     HtmlEncoder = new CommonTestEncoder();
 }