Exemplo n.º 1
0
        public void RendersImageTag_AddsFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("Alt image text") },
                { "src", "/images/test-image.png" },
                { "asp-append-version", "true" }
            });
            var output = MakeImageTagHelperOutput(attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("Alt image text") },
            });
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ImageTagHelper(hostingEnvironment, MakeCache())
            {
                ViewContext   = viewContext,
                Src           = "/images/test-image.png",
                AppendVersion = true
            };

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

            // Assert
            Assert.True(output.Content.IsEmpty);
            Assert.Equal("img", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var srcAttribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("src"));

            Assert.Equal("/images/test-image.png?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", srcAttribute.Value);
        }
Exemplo n.º 2
0
        public void RendersImageTag_DoesNotAddFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("Alt image text") },
                { "src", "/images/test-image.png" },
                { "asp-file-version", "false" }
            });
            var output = MakeImageTagHelperOutput(attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("Alt image text") },
            });
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ImageTagHelper(hostingEnvironment, MakeCache())
            {
                ViewContext = viewContext,
                Src         = "/images/test-image.png",
                FileVersion = false,
            };

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

            // Assert
            Assert.True(output.Content.IsEmpty);
            Assert.Equal("img", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var srcAttribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("src"));

            Assert.Equal("/images/test-image.png", srcAttribute.Value);
        }
Exemplo n.º 3
0
        public void PreservesOrderOfSourceAttributesWhenRun()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("alt text") },
                { "data-extra", new HtmlString("something") },
                { "title", new HtmlString("Image title") },
                { "src", "testimage.png" },
                { "asp-append-version", "true" }
            });
            var output = MakeImageTagHelperOutput(
                attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("alt text") },
                { "data-extra", new HtmlString("something") },
                { "title", new HtmlString("Image title") },
            });

            var expectedOutput = MakeImageTagHelperOutput(
                attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("alt text") },
                { "data-extra", new HtmlString("something") },
                { "title", new HtmlString("Image title") },
                { "src", "testimage.png?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk" }
            });

            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext        = MakeViewContext();

            var helper = new ImageTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                MakeUrlHelperFactory())
            {
                ViewContext   = viewContext,
                Src           = "testimage.png",
                AppendVersion = true,
            };

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

            // Assert
            Assert.Equal(expectedOutput.TagName, output.TagName);
            Assert.Equal(4, output.Attributes.Count);

            for (int i = 0; i < expectedOutput.Attributes.Count; i++)
            {
                var expectedAtribute = expectedOutput.Attributes[i];
                var actualAttribute  = output.Attributes[i];
                Assert.Equal(expectedAtribute.Name, actualAttribute.Name);
                Assert.Equal(expectedAtribute.Value.ToString(), actualAttribute.Value.ToString());
            }
        }
Exemplo n.º 4
0
        public void Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper(
            string src,
            string srcOutput,
            string expectedSrcPrefix)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                new TagHelperAttributeList
            {
                { "alt", new HtmlString("Testing") },
                { "asp-append-version", true },
            });
            var context          = MakeTagHelperContext(allAttributes);
            var outputAttributes = new TagHelperAttributeList
            {
                { "alt", new HtmlString("Testing") },
                { "src", srcOutput },
            };
            var output = new TagHelperOutput(
                "img",
                outputAttributes,
                getChildContentAsync: (_) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));
            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 ImageTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                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);
        }
Exemplo n.º 5
0
        public void Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper(
            string src,
            string srcOutput,
            string expectedSrcPrefix)
        {
            // Arrange
            var allAttributes = new TagHelperAttributeList(
                new TagHelperAttributeList
                {
                    { "alt", new HtmlString("Testing") },
                    { "asp-append-version", true },
                });
            var context = MakeTagHelperContext(allAttributes);
            var outputAttributes = new TagHelperAttributeList
                {
                    { "alt", new HtmlString("Testing") },
                    { "src", srcOutput },
                };
            var output = new TagHelperOutput(
                "img",
                outputAttributes,
                getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
            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 ImageTagHelper(
                hostingEnvironment,
                MakeCache(),
                new HtmlTestEncoder(),
                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);
        }
Exemplo n.º 6
0
        public void PreservesOrderOfSourceAttributesWhenRun()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    { "alt", new HtmlString("alt text") },
                    { "data-extra", new HtmlString("something") },                    
                    { "title", new HtmlString("Image title") },
                    { "src", "testimage.png" },
                    { "asp-append-version", "true" }
                });
            var output = MakeImageTagHelperOutput(
                attributes: new TagHelperAttributeList
                {
                    { "alt", new HtmlString("alt text") },
                    { "data-extra", new HtmlString("something") },                    
                    { "title", new HtmlString("Image title") },
                });

            var expectedOutput = MakeImageTagHelperOutput(
                attributes: new TagHelperAttributeList
                {
                    { "alt", new HtmlString("alt text") },
                    { "data-extra", new HtmlString("something") },
                    { "title", new HtmlString("Image title") },
                    { "src", "testimage.png?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk" }
                });

            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ImageTagHelper(hostingEnvironment, MakeCache())
            {
                ViewContext = viewContext,
                Src = "testimage.png",
                AppendVersion = true,
            };

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

            // Assert            
            Assert.Equal(expectedOutput.TagName, output.TagName);
            Assert.Equal(4, output.Attributes.Count);
            
            for(int i=0; i < expectedOutput.Attributes.Count; i++)
            {
                var expectedAtribute = expectedOutput.Attributes[i];
                var actualAttribute = output.Attributes[i];
                Assert.Equal(expectedAtribute.Name, actualAttribute.Name);
                Assert.Equal(expectedAtribute.Value.ToString(), actualAttribute.Value.ToString());
            }
        }
Exemplo n.º 7
0
        public void RendersImageTag_AddsFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    { "alt", new HtmlString("Alt image text") },
                    { "src", "/images/test-image.png" },
                    { "asp-append-version", "true" }
                });
            var output = MakeImageTagHelperOutput(attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("Alt image text") },
            });
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ImageTagHelper(hostingEnvironment, MakeCache())
            {
                ViewContext = viewContext,
                Src = "/images/test-image.png",
                AppendVersion = true
            };

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

            // Assert
            Assert.True(output.Content.IsEmpty);
            Assert.Equal("img", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var srcAttribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("src"));
            Assert.Equal("/images/test-image.png?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", srcAttribute.Value);
        }
Exemplo n.º 8
0
        public void RendersImageTag_DoesNotAddFileVersion()
        {
            // Arrange
            var context = MakeTagHelperContext(
                attributes: new TagHelperAttributeList
                {
                    { "alt", new HtmlString("Alt image text") },
                    { "src", "/images/test-image.png" },
                    { "asp-append-version", "false" }
                });
            var output = MakeImageTagHelperOutput(attributes: new TagHelperAttributeList
            {
                { "alt", new HtmlString("Alt image text") },
            });
            var hostingEnvironment = MakeHostingEnvironment();
            var viewContext = MakeViewContext();

            var helper = new ImageTagHelper(hostingEnvironment, MakeCache(), new CommonTestEncoder(), MakeUrlHelper())
            {
                ViewContext = viewContext,
                Src = "/images/test-image.png",
                AppendVersion = false
            };

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

            // Assert
            Assert.True(output.Content.IsEmpty);
            Assert.Equal("img", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var srcAttribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("src"));
            Assert.Equal("/images/test-image.png", srcAttribute.Value);
        }