Beispiel #1
0
        public void SuppliesNoIndexHtmlFileGivenNoTemplate()
        {
            // Arrange
            var instance = new IndexHtmlFileProvider(
                null, "fakeassembly", Enumerable.Empty <IFileInfo>());

            // Act
            var file = instance.GetFileInfo("/index.html");

            // Assert
            Assert.False(file.Exists);
        }
Beispiel #2
0
        public void RootDirectoryContainsOnlyIndexHtml()
        {
            // Arrange
            var htmlTemplate = "test";
            var instance     = new IndexHtmlFileProvider(
                htmlTemplate, "fakeassembly", Enumerable.Empty <IFileInfo>());

            // Act
            var directory = instance.GetDirectoryContents(string.Empty);

            // Assert
            Assert.True(directory.Exists);
            Assert.Collection(directory,
                              item => Assert.Equal("/index.html", item.PhysicalPath));
        }
Beispiel #3
0
        public void SuppliesIndexHtmlFileGivenTemplate()
        {
            // Arrange
            var htmlTemplate = "test";
            var instance     = new IndexHtmlFileProvider(
                htmlTemplate, "fakeassembly", Enumerable.Empty <IFileInfo>());

            // Act
            var file = instance.GetFileInfo("/index.html");

            // Assert
            Assert.True(file.Exists);
            Assert.False(file.IsDirectory);
            Assert.Equal("/index.html", file.PhysicalPath);
            Assert.Equal("index.html", file.Name);
            Assert.Equal(htmlTemplate, ReadString(file));
        }
        public void InjectsScriptTagReferencingAssemblyAndDependencies()
        {
            // Arrange
            var htmlTemplatePrefix = @"
                <html>
                <body>
                    <h1>Hello</h1>
                    Some text
                    <script>alert(1)</script>";
            var htmlTemplateSuffix = @"
                </body>
                </html>";
            var htmlTemplate       =
                $@"{htmlTemplatePrefix}
                    <script type='blazor-boot' custom1 custom2=""value"">some text that should be removed</script>
                {htmlTemplateSuffix}";
            var dependencies = new IFileInfo[]
            {
                new TestFileInfo("System.Abc.dll"),
                new TestFileInfo("MyApp.ClassLib.dll"),
            };
            var instance = new IndexHtmlFileProvider(
                htmlTemplate, "MyApp.Entrypoint", "MyNamespace.MyType::MyMethod", dependencies);

            // Act
            var file         = instance.GetFileInfo("/index.html");
            var fileContents = ReadString(file);

            // Assert: Start and end is not modified (including formatting)
            Assert.StartsWith(htmlTemplatePrefix, fileContents);
            Assert.EndsWith(htmlTemplateSuffix, fileContents);

            // Assert: Boot tag is correct
            var scriptTagText = fileContents.Substring(htmlTemplatePrefix.Length, fileContents.Length - htmlTemplatePrefix.Length - htmlTemplateSuffix.Length);
            var parsedHtml    = new HtmlParser().Parse("<html><body>" + scriptTagText + "</body></html>");
            var scriptElem    = parsedHtml.Body.QuerySelector("script");

            Assert.False(scriptElem.HasChildNodes);
            Assert.Equal("/_framework/blazor.js", scriptElem.GetAttribute("src"));
            Assert.Equal("MyApp.Entrypoint.dll", scriptElem.GetAttribute("main"));
            Assert.Equal("MyNamespace.MyType::MyMethod", scriptElem.GetAttribute("entrypoint"));
            Assert.Equal("System.Abc.dll,MyApp.ClassLib.dll", scriptElem.GetAttribute("references"));
            Assert.False(scriptElem.HasAttribute("type"));
            Assert.Equal(string.Empty, scriptElem.Attributes["custom1"].Value);
            Assert.Equal("value", scriptElem.Attributes["custom2"].Value);
        }
Beispiel #5
0
        public void SuppliesHtmlTemplateUnchangedIfNoBootScriptPresent()
        {
            // Arrange
            var htmlTemplate = "<!DOCTYPE html><html><body><h1 style='color:red'>Hello</h1>Some text<script type='irrelevant'>blah</script></body></html>";
            var dependencies = new IFileInfo[]
            {
                new TestFileInfo("System.Abc.dll"),
                new TestFileInfo("MyApp.ClassLib.dll"),
            };
            var instance = new IndexHtmlFileProvider(
                htmlTemplate, "MyApp.Entrypoint", dependencies);

            // Act
            var file = instance.GetFileInfo("/index.html");

            // Assert
            Assert.Equal(htmlTemplate, ReadString(file));
        }
Beispiel #6
0
        public void InsertsScriptTagReferencingAssemblyAndDependencies()
        {
            // Arrange
            var htmlTemplate = "<html><body><h1>Hello</h1>Some text</body></html>";
            var dependencies = new IFileInfo[]
            {
                new TestFileInfo("System.Abc.dll"),
                new TestFileInfo("MyApp.ClassLib.dll"),
            };
            var instance = new IndexHtmlFileProvider(
                htmlTemplate, "MyApp.Entrypoint", dependencies);

            // Act
            var file       = instance.GetFileInfo("/index.html");
            var parsedHtml = new HtmlParser().Parse(ReadString(file));
            var scriptElem = parsedHtml.Body.FirstElementChild;

            // Assert
            Assert.Equal("script", scriptElem.TagName.ToLowerInvariant());
            Assert.False(scriptElem.HasChildNodes);
            Assert.Equal("/_framework/blazor.js", scriptElem.GetAttribute("src"));
            Assert.Equal("MyApp.Entrypoint.dll", scriptElem.GetAttribute("main"));
            Assert.Equal("System.Abc.dll,MyApp.ClassLib.dll", scriptElem.GetAttribute("references"));
        }