public async Task RunsWhenRequiredAttributesArePresent(
            IDictionary <string, object> attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var context            = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var helper             = new ScriptTagHelper
            {
                HtmlEncoder        = new HtmlEncoder(),
                JavaScriptEncoder  = new JavaScriptStringEncoder(),
                Logger             = logger,
                HostingEnvironment = hostingEnvironment,
                ViewContext        = viewContext,
                Cache = MakeCache(),
            };

            setProperties(helper);

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Null(output.TagName);
            Assert.True(output.IsContentModified);
            Assert.Empty(logger.Logged);
        }
Example #2
0
        public async Task RenderScriptTags_WithFileVersion_AndRequestPathBase()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["src"] = "/bar/js/site.js",
                ["asp-append-version"] = "true"
            });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext("/bar");

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext   = viewContext,
                AppendVersion = true,
                Src           = "/bar/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal(
                "<script src=\"HtmlEncode[[/bar/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>", output.Content.GetContent());
        }
Example #3
0
        public async Task DoesNotRunWhenAllRequiredAttributesAreMissing()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext();
            var viewContext      = MakeViewContext();
            var output           = MakeTagHelperOutput("script");
            var logger           = CreateLogger();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext = viewContext,
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);
        }
Example #4
0
        public void DoesNotRunWhenARequiredAttributeIsMissing(
            TagHelperAttributeList attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext   = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext = viewContext,
            };

            setProperties(helper);

            // Act
            helper.Process(tagHelperContext, output);

            // Assert
            Assert.NotNull(output.TagName);
            Assert.False(output.IsContentModified);
        }
        public void DoesNotRunWhenARequiredAttributeIsMissing(
            IDictionary <string, object> attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext   = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var helper             = new ScriptTagHelper
            {
                Logger             = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext        = viewContext,
                Cache = MakeCache(),
            };

            setProperties(helper);

            // Act
            helper.Process(tagHelperContext, output);

            // Assert
            Assert.NotNull(output.TagName);
            Assert.False(output.IsContentModified);
        }
        public async Task RendersScriptTagsForGlobbedSrcResults()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary <string, object>
            {
                ["src"]             = "/js/site.js",
                ["asp-src-include"] = "**/*.js"
            });
            var output             = MakeTagHelperOutput("script", attributes: new Dictionary <string, object>());
            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var globbingUrlBuilder = new Mock <GlobbingUrlBuilder>();

            globbingUrlBuilder.Setup(g => g.BuildUrlList("/js/site.js", "**/*.js", null))
            .Returns(new[] { "/js/site.js", "/common.js" });
            var helper = new ScriptTagHelper
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                Logger             = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext        = viewContext,
                Src         = "/js/site.js",
                SrcInclude  = "**/*.js",
                HtmlEncoder = new HtmlEncoder(),
                Cache       = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("<script src=\"/js/site.js\"></script><script src=\"/common.js\"></script>", output.Content.GetContent());
        }
Example #7
0
        public async Task RenderScriptTags_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["src"] = "/js/site.js",
                ["asp-append-version"] = "true"
            });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext   = viewContext,
                AppendVersion = true,
                Src           = "/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
        }
        public async Task RenderScriptTags_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary <string, object>
            {
                ["src"] = "/js/site.js",
                ["asp-file-version"] = "true"
            });
            var output = MakeTagHelperOutput("script", attributes: new Dictionary <string, object>());

            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ScriptTagHelper
            {
                Logger             = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext        = viewContext,
                FileVersion        = true,
                HtmlEncoder        = new TestHtmlEncoder(),
                JavaScriptEncoder  = new TestJavaScriptEncoder(),
                Src   = "/js/site.js",
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal(
                "<script src=\"HtmlEncode[[/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>", output.Content.GetContent());
        }
Example #9
0
        public async Task RunsWhenRequiredAttributesArePresent(
            TagHelperAttributeList attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var context            = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext = viewContext,
            };

            setProperties(helper);

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Null(output.TagName);
            Assert.True(output.IsContentModified);
            Assert.Empty(logger.Logged);
        }
        public async Task LogsWhenAllRequiredAttributesAreMissing()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext();
            var viewContext      = MakeViewContext();
            var output           = MakeTagHelperOutput("script");
            var logger           = CreateLogger();

            var helper = new ScriptTagHelper
            {
                Logger      = logger,
                ViewContext = viewContext,
                Cache       = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);

            Assert.Single(logger.Logged);

            Assert.Equal(LogLevel.Verbose, logger.Logged[0].LogLevel);
            Assert.IsAssignableFrom <ILogValues>(logger.Logged[0].State);
            Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
                              ((ILogValues)logger.Logged[0].State).Format());
        }
        public void Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper(
            string src,
            string srcOutput,
            string expectedSrcPrefix)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                new TagHelperAttributeList
            {
                { "type", new HtmlString("text/javascript") },
                { "asp-append-version", true },
            });
            var context          = MakeTagHelperContext(allAttributes);
            var outputAttributes = new TagHelperAttributeList
            {
                { "type", new HtmlString("text/javascript") },
                { "src", srcOutput },
            };
            var output             = MakeTagHelperOutput("script", outputAttributes);
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var urlHelper          = new Mock <IUrlHelper>();

            // Ensure expanded path does not look like an absolute path on Linux, avoiding
            // https://github.com/aspnet/External/issues/21
            urlHelper
            .Setup(urlhelper => urlhelper.Content(It.IsAny <string>()))
            .Returns(new Func <string, string>(url => url.Replace("~/", "virtualRoot/")));
            var urlHelperFactory = new Mock <IUrlHelperFactory>();

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

            var helper = new ScriptTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                new JavaScriptTestEncoder(),
                urlHelperFactory.Object)
            {
                ViewContext   = viewContext,
                AppendVersion = true,
                Src           = src,
            };

            // Act
            helper.Process(context, output);

            // Assert
            Assert.Equal(
                expectedSrcPrefix + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
                (string)output.Attributes["src"].Value,
                StringComparer.Ordinal);
        }
        public void Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper(
            string src,
            string srcOutput,
            string expectedSrcPrefix)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                new TagHelperAttributeList
                {
                    { "type", new HtmlString("text/javascript") },
                    { "asp-append-version", true },
                });
            var context = MakeTagHelperContext(allAttributes);
            var outputAttributes = new TagHelperAttributeList
                {
                    { "type", new HtmlString("text/javascript") },
                    { "src", srcOutput },
                };
            var output = MakeTagHelperOutput("script", outputAttributes);
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var urlHelper = new Mock<IUrlHelper>();

            // Ensure expanded path does not look like an absolute path on Linux, avoiding
            // https://github.com/aspnet/External/issues/21
            urlHelper
                .Setup(urlhelper => urlhelper.Content(It.IsAny<string>()))
                .Returns(new Func<string, string>(url => url.Replace("~/", "virtualRoot/")));
            var urlHelperFactory = new Mock<IUrlHelperFactory>();
            urlHelperFactory
                .Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>()))
                .Returns(urlHelper.Object);

            var helper = new ScriptTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                new JavaScriptTestEncoder(),
                urlHelperFactory.Object)
            {
                ViewContext = viewContext,
                AppendVersion = true,
                Src = src,
            };

            // Act
            helper.Process(context, output);

            // Assert
            Assert.Equal(
                expectedSrcPrefix + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
                (string)output.Attributes["src"].Value,
                StringComparer.Ordinal);
        }
Example #13
0
        public async Task PreservesOrderOfNonSrcAttributes()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["data-extra"]        = "something",
                ["src"]               = "/blank.js",
                ["data-more"]         = "else",
                ["asp-fallback-src"]  = "http://www.example.com/blank.js",
                ["asp-fallback-test"] = "isavailable()",
            });

            var viewContext = MakeViewContext();

            var output = MakeTagHelperOutput("src",
                                             attributes: new TagHelperAttributeList
            {
                ["data-extra"] = "something",
                ["data-more"]  = "else",
            });

            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                logger,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext            = viewContext,
                FallbackSrc            = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("data-extra", output.Attributes[0].Name);
            Assert.Equal("data-more", output.Attributes[1].Name);
            Assert.Equal("src", output.Attributes[2].Name);
            Assert.Empty(logger.Logged);
        }
Example #14
0
        public async Task PreservesOrderOfSourceAttributesWhenRun()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["data-extra"]        = "something",
                ["src"]               = "/blank.js",
                ["data-more"]         = "else",
                ["asp-fallback-src"]  = "http://www.example.com/blank.js",
                ["asp-fallback-test"] = "isavailable()",
            });

            var viewContext = MakeViewContext();

            var output = MakeTagHelperOutput("src",
                                             attributes: new TagHelperAttributeList
            {
                ["data-extra"] = "something",
                ["data-more"]  = "else",
            });

            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                logger,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext            = viewContext,
                FallbackSrc            = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.StartsWith(
                "<script data-extra=\"HtmlEncode[[something]]\" data-more=\"HtmlEncode[[else]]\" src=\"HtmlEncode[[/blank.js]]\"",
                output.Content.GetContent());
            Assert.Empty(logger.Logged);
        }
Example #15
0
        public async Task HandlesMultipleAttributesSameNameCorrectly(
            TagHelperAttributeList outputAttributes,
            string expectedAttributeString)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new TagHelperAttributeList
            {
                ["data-extra"]        = "something",
                ["src"]               = "/blank.js",
                ["asp-fallback-src"]  = "http://www.example.com/blank.js",
                ["asp-fallback-test"] = "isavailable()",
            }));
            var tagHelperContext         = MakeTagHelperContext(allAttributes);
            var viewContext              = MakeViewContext();
            var combinedOutputAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new[]
            {
                new TagHelperAttribute("data-extra", new HtmlString("something"))
            }));
            var output             = MakeTagHelperOutput("script", combinedOutputAttributes);
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext            = viewContext,
                FallbackSrc            = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.StartsWith(
                "<script " + expectedAttributeString + " data-extra=\"something\" " +
                "src=\"HtmlEncode[[/blank.js]]\"",
                output.Content.GetContent());
        }
Example #16
0
        public async Task HandlesMultipleAttributesSameNameCorrectly(
            TagHelperAttributeList outputAttributes,
            string expectedAttributeString)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new TagHelperAttributeList
                    {
                        ["data-extra"] = "something",
                        ["src"] = "/blank.js",
                        ["asp-fallback-src"] = "http://www.example.com/blank.js",
                        ["asp-fallback-test"] = "isavailable()",
                    }));
            var tagHelperContext = MakeTagHelperContext(allAttributes);
            var viewContext = MakeViewContext();
            var combinedOutputAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new[]
                    {
                        new TagHelperAttribute("data-extra", new HtmlString("something"))
                    }));
            var output = MakeTagHelperOutput("script", combinedOutputAttributes);
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext = viewContext,
                FallbackSrc = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.StartsWith(
                "<script " + expectedAttributeString + " data-extra=\"something\" " +
                "src=\"HtmlEncode[[/blank.js]]\"",
                output.Content.GetContent());
        }
Example #17
0
        public async Task LogsWhenARequiredAttributeIsMissing(
            TagHelperAttributeList attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext   = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ScriptTagHelper(
                logger,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
            };

            setProperties(helper);

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);
            Assert.Empty(output.Attributes);
            Assert.True(output.PostElement.IsEmpty);

            Assert.Equal(2, logger.Logged.Count);

            Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
            Assert.IsAssignableFrom <ILogValues>(logger.Logged[0].State);

            var loggerData0 = (ILogValues)logger.Logged[0].State;

            Assert.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
            Assert.IsAssignableFrom <ILogValues>(logger.Logged[1].State);
            Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'" +
                              " with id",
                              ((ILogValues)logger.Logged[1].State).ToString());
        }
        public async Task HandlesMultipleAttributesSameNameCorrectly(TagHelperAttributeList outputAttributes)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new TagHelperAttributeList
            {
                ["data-extra"]        = "something",
                ["src"]               = "/blank.js",
                ["asp-fallback-src"]  = "http://www.example.com/blank.js",
                ["asp-fallback-test"] = "isavailable()",
            }));
            var tagHelperContext         = MakeTagHelperContext(allAttributes);
            var viewContext              = MakeViewContext();
            var combinedOutputAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new[]
            {
                new TagHelperAttribute("data-extra", new HtmlString("something"))
            }));
            var output             = MakeTagHelperOutput("script", combinedOutputAttributes);
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                new JavaScriptTestEncoder(),
                MakeUrlHelperFactory())
            {
                ViewContext            = viewContext,
                FallbackSrc            = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };
            var expectedAttributes = new TagHelperAttributeList(output.Attributes);

            expectedAttributes.Add(new TagHelperAttribute("src", "/blank.js"));

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(expectedAttributes, output.Attributes);
        }
Example #19
0
        public async Task RenderScriptTags_GlobbedSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["src"]                = "/js/site.js",
                ["asp-src-include"]    = "*.js",
                ["asp-append-version"] = "true"
            });
            var output             = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());
            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var globbingUrlBuilder = new Mock <GlobbingUrlBuilder>();

            globbingUrlBuilder.Setup(g => g.BuildUrlList(null, "*.js", null))
            .Returns(new[] { "/common.js" });

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                ViewContext        = viewContext,
                SrcInclude         = "*.js",
                AppendVersion      = true,
                Src = "/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
            Assert.Equal("<script src=\"HtmlEncode[[/common.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                         "</script>", output.PostElement.GetContent());
        }
Example #20
0
        public async Task RenderScriptTags_FallbackSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["src"] = "/js/site.js",
                ["asp-fallback-src-include"] = "fallback.js",
                ["asp-fallback-test"]        = "isavailable()",
                ["asp-append-version"]       = "true"
            });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext            = viewContext,
                FallbackSrc            = "fallback.js",
                FallbackTestExpression = "isavailable()",
                AppendVersion          = true,
                Src = "/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
            Assert.Equal(Environment.NewLine + "<script>(isavailable()||document.write(\"<script " +
                         "src=\\\"JavaScriptStringEncode[[fallback.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\\\">" +
                         "<\\/script>\"));</script>", output.PostElement.GetContent());
        }
Example #21
0
        public async Task RendersScriptTagsForGlobbedSrcResults_UsesProvidedEncoder()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                ["src"]             = "/js/site.js",
                ["asp-src-include"] = "**/*.js"
            });
            var output             = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());
            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var globbingUrlBuilder = new Mock <GlobbingUrlBuilder>();

            globbingUrlBuilder.Setup(g => g.BuildUrlList(null, "**/*.js", null))
            .Returns(new[] { "/common.js" });

            var helper = new ScriptTagHelper(
                logger.Object,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                ViewContext        = viewContext,
                Src        = "/js/site.js",
                SrcInclude = "**/*.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js", output.Attributes["src"].Value);
            Assert.Equal("<script src=\"HtmlEncode[[/common.js]]\"></script>", output.PostElement.GetContent());
        }
        public async Task RenderScriptTags_GlobbedSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary <string, object>
            {
                ["src"]              = "/js/site.js",
                ["asp-src-include"]  = "*.js",
                ["asp-file-version"] = "true"
            });
            var output             = MakeTagHelperOutput("script", attributes: new Dictionary <string, object>());
            var logger             = new Mock <ILogger <ScriptTagHelper> >();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var globbingUrlBuilder = new Mock <GlobbingUrlBuilder>();

            globbingUrlBuilder.Setup(g => g.BuildUrlList("/js/site.js", "*.js", null))
            .Returns(new[] { "/js/site.js", "/common.js" });
            var helper = new ScriptTagHelper
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                Logger             = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext        = viewContext,
                SrcInclude         = "*.js",
                FileVersion        = true,
                HtmlEncoder        = new TestHtmlEncoder(),
                JavaScriptEncoder  = new TestJavaScriptEncoder(),
                Src   = "/js/site.js",
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("<script src=\"HtmlEncode[[/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                         "</script><script src=\"HtmlEncode[[/common.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                         "</script>", output.Content.GetContent());
        }
        public async Task LogsWhenARequiredAttributeIsMissing(
            IDictionary <string, object> attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext   = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var helper             = new ScriptTagHelper
            {
                Logger             = logger,
                HostingEnvironment = hostingEnvironment,
                ViewContext        = viewContext,
                Cache = MakeCache(),
            };

            setProperties(helper);

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);

            Assert.Equal(2, logger.Logged.Count);

            Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
            Assert.IsAssignableFrom <PartialModeMatchLoggerStructure>(logger.Logged[0].State);

            var loggerData0 = (PartialModeMatchLoggerStructure)logger.Logged[0].State;

            Assert.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
            Assert.IsAssignableFrom <ILogValues>(logger.Logged[1].State);
            Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
                              ((ILogValues)logger.Logged[1].State).Format());
        }
Example #24
0
        public async Task RunsWhenRequiredAttributesArePresent_NoSrc(
            TagHelperAttributeList attributes,
            Action <ScriptTagHelper> setProperties)
        {
            // Arrange
            var context            = MakeTagHelperContext(attributes);
            var output             = MakeTagHelperOutput("script");
            var logger             = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();
            var globbingUrlBuilder = new Mock <GlobbingUrlBuilder>();

            globbingUrlBuilder.Setup(g => g.BuildUrlList(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(new[] { "/common.js" });

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext        = viewContext,
                GlobbingUrlBuilder = globbingUrlBuilder.Object
            };

            setProperties(helper);

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Null(output.TagName);
            Assert.True(output.IsContentModified);
            Assert.True(output.PostElement.IsModified);
            Assert.Empty(logger.Logged);
        }
Example #25
0
        public async Task RunsWhenRequiredAttributesArePresent(
            TagHelperAttributeList attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var context = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext = viewContext,
            };
            setProperties(helper);

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Null(output.TagName);
            Assert.True(output.IsContentModified);
            Assert.Empty(logger.Logged);
        }
Example #26
0
        public async Task RenderScriptTags_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    ["src"] = "/js/site.js",
                    ["asp-append-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder())
            {
                ViewContext = viewContext,
                AppendVersion = true,
                Src = "/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal(
                "<script src=\"HtmlEncode[[/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>", output.Content.GetContent());
        }
Example #27
0
        public async Task LogsWhenAllRequiredAttributesAreMissing()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext();
            var viewContext = MakeViewContext();
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();

            var helper = new ScriptTagHelper(
                logger,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);

            Assert.Single(logger.Logged);

            Assert.Equal(LogLevel.Verbose, logger.Logged[0].LogLevel);
            Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);
            Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'",
                ((ILogValues)logger.Logged[0].State).ToString());
        }
        public async Task RunsWhenRequiredAttributesArePresent(
            IDictionary<string, object> attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var context = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var helper = new ScriptTagHelper
            {
                HtmlEncoder = new HtmlEncoder(),
                JavaScriptEncoder = new JavaScriptStringEncoder(),
                Logger = logger,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                Cache = MakeCache(),
            };
            setProperties(helper);

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Null(output.TagName);
            Assert.True(output.IsContentModified);
            Assert.Empty(logger.Logged);
        }
        public async Task RenderScriptTags_GlobbedSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary<string, object>
                {
                    ["src"] = "/js/site.js",
                    ["asp-src-include"] = "*.js",
                    ["asp-file-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new Dictionary<string, object>());
            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var globbingUrlBuilder = new Mock<GlobbingUrlBuilder>();
            globbingUrlBuilder.Setup(g => g.BuildUrlList("/js/site.js", "*.js", null))
                .Returns(new[] { "/js/site.js", "/common.js" });
            var helper = new ScriptTagHelper
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                Logger = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                SrcInclude = "*.js",
                FileVersion = true,
                HtmlEncoder = new TestHtmlEncoder(),
                JavaScriptEncoder = new TestJavaScriptEncoder(),
                Src = "/js/site.js",
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("<script src=\"HtmlEncode[[/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script><script src=\"HtmlEncode[[/common.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>", output.Content.GetContent());
        }
        public async Task RenderScriptTags_FallbackSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary<string, object>
                {
                    ["src"] = "/js/site.js",
                    ["asp-fallback-src-include"] = "fallback.js",
                    ["asp-fallback-test"] = "isavailable()",
                    ["asp-file-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new Dictionary<string, object>());

            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ScriptTagHelper
            {
                Logger = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                FallbackSrc = "fallback.js",
                FallbackTestExpression = "isavailable()",
                FileVersion = true,
                HtmlEncoder = new TestHtmlEncoder(),
                JavaScriptEncoder = new TestJavaScriptEncoder(),
                Src = "/js/site.js",
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal(
                "<script src=\"HtmlEncode[[/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>\r\n<script>(isavailable()||document.write(\"<script src=\\\"JavaScriptEncode[[fallback.js" +
                "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\\\"><\\/script>\"));</script>",
                output.Content.GetContent());
        }
Example #31
0
        public async Task RunsWhenRequiredAttributesArePresent_NoSrc(
            TagHelperAttributeList attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var context = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var globbingUrlBuilder = new Mock<GlobbingUrlBuilder>();
            globbingUrlBuilder.Setup(g => g.BuildUrlList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                .Returns(new[] { "/common.js" });

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
                GlobbingUrlBuilder = globbingUrlBuilder.Object
            };
            setProperties(helper);

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Null(output.TagName);
            Assert.True(output.IsContentModified);
            Assert.True(output.PostElement.IsModified);
            Assert.Empty(logger.Logged);
        }
Example #32
0
        public async Task RenderScriptTags_FallbackSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    ["src"] = "/js/site.js",
                    ["asp-fallback-src-include"] = "fallback.js",
                    ["asp-fallback-test"] = "isavailable()",
                    ["asp-append-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
                FallbackSrc = "fallback.js",
                FallbackTestExpression = "isavailable()",
                AppendVersion = true,
                Src = "/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
            Assert.Equal(Environment.NewLine + "<script>(isavailable()||document.write(\"<script " +
                "src=\\\"JavaScriptStringEncode[[fallback.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\\\">" +
                "<\\/script>\"));</script>", output.PostElement.GetContent());
        }
Example #33
0
        public async Task RendersScriptTagsForGlobbedSrcResults_UsesProvidedEncoder()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    ["src"] = "/js/site.js",
                    ["asp-src-include"] = "**/*.js"
                });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());
            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var globbingUrlBuilder = new Mock<GlobbingUrlBuilder>();
            globbingUrlBuilder.Setup(g => g.BuildUrlList(null, "**/*.js", null))
                .Returns(new[] { "/common.js" });

            var helper = new ScriptTagHelper(
                logger.Object,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                ViewContext = viewContext,
                Src = "/js/site.js",
                SrcInclude = "**/*.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js", output.Attributes["src"].Value);
            Assert.Equal("<script src=\"HtmlEncode[[/common.js]]\"></script>", output.PostElement.GetContent());
        }
        public async Task HandlesMultipleAttributesSameNameCorrectly(TagHelperAttributeList outputAttributes)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new TagHelperAttributeList
                    {
                        ["data-extra"] = "something",
                        ["src"] = "/blank.js",
                        ["asp-fallback-src"] = "http://www.example.com/blank.js",
                        ["asp-fallback-test"] = "isavailable()",
                    }));
            var tagHelperContext = MakeTagHelperContext(allAttributes);
            var viewContext = MakeViewContext();
            var combinedOutputAttributes = new TagHelperAttributeList(
                outputAttributes.Concat(
                    new[]
                    {
                        new TagHelperAttribute("data-extra", new HtmlString("something"))
                    }));
            var output = MakeTagHelperOutput("script", combinedOutputAttributes);
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                new JavaScriptTestEncoder(),
                MakeUrlHelperFactory())
            {
                ViewContext = viewContext,
                FallbackSrc = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };
            var expectedAttributes = new TagHelperAttributeList(output.Attributes);
            expectedAttributes.Add(new TagHelperAttribute("src", "/blank.js"));

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(expectedAttributes, output.Attributes);
        }
        public void DoesNotRunWhenARequiredAttributeIsMissing(
            IDictionary<string, object> attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var helper = new ScriptTagHelper
            {
                Logger = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                Cache = MakeCache(),
            };
            setProperties(helper);

            // Act
            helper.Process(tagHelperContext, output);

            // Assert
            Assert.NotNull(output.TagName);
            Assert.False(output.IsContentModified);
        }
Example #36
0
        public async Task LogsWhenARequiredAttributeIsMissing(
            TagHelperAttributeList attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ScriptTagHelper(
                logger,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
            };
            setProperties(helper);

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);
            Assert.Empty(output.Attributes);
            Assert.True(output.PostElement.IsEmpty);

            Assert.Equal(2, logger.Logged.Count);

            Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
            Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);

            var loggerData0 = (ILogValues)logger.Logged[0].State;

            Assert.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
            Assert.IsAssignableFrom<ILogValues>(logger.Logged[1].State);
            Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'" +
                " with id",
                ((ILogValues)logger.Logged[1].State).ToString());
        }
Example #37
0
        public void DoesNotRunWhenARequiredAttributeIsMissing(
            TagHelperAttributeList attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
            };
            setProperties(helper);

            // Act
            helper.Process(tagHelperContext, output);

            // Assert
            Assert.NotNull(output.TagName);
            Assert.False(output.IsContentModified);
            Assert.Empty(output.Attributes);
            Assert.True(output.PostElement.IsEmpty);
        }
        public async Task LogsWhenARequiredAttributeIsMissing(
            IDictionary<string, object> attributes,
            Action<ScriptTagHelper> setProperties)
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(attributes);
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var helper = new ScriptTagHelper
            {
                Logger = logger,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                Cache = MakeCache(),
            };
            setProperties(helper);

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);

            Assert.Equal(2, logger.Logged.Count);

            Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
            Assert.IsAssignableFrom<PartialModeMatchLoggerStructure>(logger.Logged[0].State);

            var loggerData0 = (PartialModeMatchLoggerStructure)logger.Logged[0].State;

            Assert.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
            Assert.IsAssignableFrom<ILogValues>(logger.Logged[1].State);
            Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
                ((ILogValues)logger.Logged[1].State).Format());
        }
Example #39
0
        public async Task DoesNotRunWhenAllRequiredAttributesAreMissing()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext();
            var viewContext = MakeViewContext();
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();

            var helper = new ScriptTagHelper(
                CreateLogger(),
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);
            Assert.Empty(output.Attributes);
            Assert.True(output.PostElement.IsEmpty);
        }
        public async Task DoesNotRunWhenAllRequiredAttributesAreMissing()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext();
            var viewContext = MakeViewContext();
            var output = MakeTagHelperOutput("script");
            var logger = CreateLogger();

            var helper = new ScriptTagHelper
            {
                Logger = logger,
                ViewContext = viewContext,
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.False(output.IsContentModified);
        }
Example #41
0
        public async Task PreservesOrderOfNonSrcAttributes()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    ["data-extra"] = "something",
                    ["src"] = "/blank.js",
                    ["data-more"] = "else",
                    ["asp-fallback-src"] = "http://www.example.com/blank.js",
                    ["asp-fallback-test"] = "isavailable()",
                });

            var viewContext = MakeViewContext();

            var output = MakeTagHelperOutput("src",
                attributes: new TagHelperAttributeList
                {
                    ["data-extra"] = "something",
                    ["data-more"] = "else",
                });

            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper(
                logger,
                hostingEnvironment,
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
                FallbackSrc = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("data-extra", output.Attributes[0].Name);
            Assert.Equal("src", output.Attributes[1].Name);
            Assert.Equal("data-more", output.Attributes[2].Name);
            Assert.Empty(logger.Logged);
        }
        public async Task PreservesOrderOfSourceAttributesWhenRun()
        {
            // Arrange
            var tagHelperContext = MakeTagHelperContext(
                attributes: new Dictionary<string, object>
                {
                    ["data-extra"] = "something",
                    ["src"] = "/blank.js",
                    ["data-more"] = "else",
                    ["asp-fallback-src"] = "http://www.example.com/blank.js",
                    ["asp-fallback-test"] = "isavailable()",
                });

            var viewContext = MakeViewContext();

            var output = MakeTagHelperOutput("src",
                attributes: new Dictionary<string, object>
                {
                    ["data-extra"] = "something",
                    ["data-more"] = "else",
                });

            var logger = CreateLogger();
            var hostingEnvironment = MakeHostingEnvironment();

            var helper = new ScriptTagHelper
            {
                HtmlEncoder = new HtmlEncoder(),
                JavaScriptEncoder = new JavaScriptStringEncoder(),
                Logger = logger,
                ViewContext = viewContext,
                HostingEnvironment = hostingEnvironment,
                FallbackSrc = "~/blank.js",
                FallbackTestExpression = "http://www.example.com/blank.js",
                Src = "/blank.js",
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.StartsWith(
                "<script data-extra=\"something\" data-more=\"else\" src=\"/blank.js\"", output.Content.GetContent());
            Assert.Empty(logger.Logged);
        }
Example #43
0
        public async Task RenderScriptTags_WithFileVersion_AndRequestPathBase()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    ["src"] = "/bar/js/site.js",
                    ["asp-append-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext("/bar");

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                ViewContext = viewContext,
                AppendVersion = true,
                Src = "/bar/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/bar/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
        }
        public async Task RendersScriptTagsForGlobbedSrcResults_UsesProvidedEncoder()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary<string, object>
                {
                    ["src"] = "/js/site.js",
                    ["asp-src-include"] = "**/*.js"
                });
            var output = MakeTagHelperOutput("script", attributes: new Dictionary<string, object>());
            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var globbingUrlBuilder = new Mock<GlobbingUrlBuilder>();
            globbingUrlBuilder.Setup(g => g.BuildUrlList("/js/site.js", "**/*.js", null))
                .Returns(new[] { "/js/site.js", "/common.js" });
            var helper = new ScriptTagHelper
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                Logger = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                Src = "/js/site.js",
                SrcInclude = "**/*.js",
                HtmlEncoder = new TestHtmlEncoder(),
                JavaScriptEncoder = new TestJavaScriptEncoder(),
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("<script src=\"HtmlEncode[[/js/site.js]]\"></script>" +
                "<script src=\"HtmlEncode[[/common.js]]\"></script>", output.Content.GetContent());
        }
Example #45
0
        public async Task RenderScriptTags_GlobbedSrc_WithFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    ["src"] = "/js/site.js",
                    ["asp-src-include"] = "*.js",
                    ["asp-append-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());
            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();
            var globbingUrlBuilder = new Mock<GlobbingUrlBuilder>();
            globbingUrlBuilder.Setup(g => g.BuildUrlList(null, "*.js", null))
                .Returns(new[] { "/common.js" });

            var helper = new ScriptTagHelper(
                logger.Object,
                MakeHostingEnvironment(),
                MakeCache(),
                new CommonTestEncoder(),
                new CommonTestEncoder(),
                MakeUrlHelper())
            {
                GlobbingUrlBuilder = globbingUrlBuilder.Object,
                ViewContext = viewContext,
                SrcInclude = "*.js",
                AppendVersion = true,
                Src = "/js/site.js",
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("script", output.TagName);
            Assert.Equal("/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
            Assert.Equal("<script src=\"HtmlEncode[[/common.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>", output.PostElement.GetContent());
        }
        public async Task RenderScriptTags_WithFileVersion_AndRequestPathBase()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new Dictionary<string, object>
                {
                    ["src"] = "/bar/js/site.js",
                    ["asp-file-version"] = "true"
                });
            var output = MakeTagHelperOutput("script", attributes: new Dictionary<string, object>());

            var logger = new Mock<ILogger<ScriptTagHelper>>();
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext("/bar");

            var helper = new ScriptTagHelper
            {
                Logger = logger.Object,
                HostingEnvironment = hostingEnvironment,
                ViewContext = viewContext,
                FileVersion = true,
                HtmlEncoder = new TestHtmlEncoder(),
                JavaScriptEncoder = new TestJavaScriptEncoder(),
                Src = "/bar/js/site.js",
                Cache = MakeCache(),
            };

            // Act
            await helper.ProcessAsync(context, output);

            // Assert
            Assert.Equal(
                "<script src=\"HtmlEncode[[/bar/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
                "</script>", output.Content.GetContent());
        }