Browse Source

Unskip HtmlGeneration tests (#38138)

* Unskip HtmlGeneration tests

* Cleanup image test to no longer use checked in output file
* Fix issue with other HtmlGenerationTests that were attempting to write to disk as part of test execution

Fixes https://github.com/dotnet/aspnetcore/issues/34599
Pranav K 4 years ago
parent
commit
ac7cd02580

+ 83 - 22
src/Mvc/test/Mvc.FunctionalTests/HtmlGenerationTest.cs

@@ -1,20 +1,15 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Net.Http.Headers;
 using System.Reflection;
 using System.Text;
-using System.Threading.Tasks;
+using AngleSharp.Dom;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Mvc.Testing;
 using Microsoft.AspNetCore.Testing;
-using Xunit;
 
 namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
 
@@ -94,7 +89,7 @@ public class HtmlGenerationTest :
         Assert.Equal($"Vrijdag{Environment.NewLine}Month: FirstOne", response, ignoreLineEndingDifferences: true);
     }
 
-    [Theory(Skip = "https://github.com/dotnet/aspnetcore/issues/34599")]
+    [Theory]
     [MemberData(nameof(WebPagesData))]
     public async Task HtmlGenerationWebSite_GeneratesExpectedResults(string action, string antiforgeryPath)
     {
@@ -116,31 +111,97 @@ public class HtmlGenerationTest :
         responseContent = responseContent.Trim();
         if (antiforgeryPath == null)
         {
-            ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
             Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
         }
         else
         {
             var forgeryToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, antiforgeryPath);
-
-            if (ResourceFile.GenerateBaselines)
-            {
-                // Reverse usual substitution and insert a format item into the new file content.
-                responseContent = responseContent.Replace(forgeryToken, "{0}");
-                ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
-            }
-            else
-            {
-                expectedContent = string.Format(CultureInfo.InvariantCulture, expectedContent, forgeryToken);
-                Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
-            }
+            ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent, forgeryToken);
         }
     }
 
-    [Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/34599")]
+    [Fact]
     public async Task HtmlGenerationWebSite_GeneratesExpectedResults_WithImageData()
     {
-        await HtmlGenerationWebSite_GeneratesExpectedResults("Image", antiforgeryPath: null);
+        var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
+
+        // Act
+        // The host is not important as everything runs in memory and tests are isolated from each other.
+        var response = await Client.GetAsync("HtmlGeneration_Home/image");
+
+        // Assert
+        await response.AssertStatusCodeAsync(HttpStatusCode.OK);
+        Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
+        var document = await response.GetHtmlDocumentAsync();
+
+        AssertImgElement(document.GetElementById("1"), new()
+        {
+            ["src"] = "/images/red.png",
+            ["alt"] = "Red block",
+            ["title"] = "<the title>",
+        });
+
+        // <img src = "/images/red.png?v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk" alt = "Red versioned" title = "Red versioned" />
+        AssertImgElement(document.GetElementById("2"), new()
+        {
+            ["src"] = "/images/red.png?v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk",
+            ["alt"] = "Red versioned",
+            ["title"] = "Red versioned",
+        });
+
+        // <img src="/images/red.png" alt="Red explicitly not versioned" title="Red versioned">
+        AssertImgElement(document.GetElementById("3"), new()
+        {
+            ["src"] = "/images/red.png",
+            ["alt"] = "Red explicitly not versioned",
+            ["title"] = "Red versioned",
+        });
+
+        // <img src="http://contoso.com/hello/world" alt="Absolute path versioned">
+        AssertImgElement(document.GetElementById("4"), new()
+        {
+            ["src"] = "http://contoso.com/hello/world",
+            ["alt"] = "Absolute path versioned",
+        });
+
+        // <img src="/images/fake.png" alt="Path to non existing file" />
+        AssertImgElement(document.GetElementById("5"), new()
+        {
+            ["src"] = "/images/fake.png",
+            ["alt"] = "Path to non existing file",
+        });
+
+        // <img src="/images/red.png?abc=def&amp;v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk" alt="Path with query string">
+        AssertImgElement(document.GetElementById("6"), new()
+        {
+            ["src"] = "/images/red.png?abc=def&v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk",
+            ["alt"] = "Path with query string",
+        });
+
+        // <img src="/images/red.png?v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk#abc" alt="Path with query string" />
+        AssertImgElement(document.GetElementById("7"), new()
+        {
+            ["src"] = "/images/red.png?v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk#abc",
+            ["alt"] = "Path with query string",
+        });
+
+        // <img src="/controller/action" alt="Path linking to some action">
+        AssertImgElement(document.GetElementById("8"), new()
+        {
+            ["src"] = "/controller/action",
+            ["alt"] = "Path linking to some action",
+        });
+
+        static void AssertImgElement(IElement imgElement, Dictionary<string, string> expectedAttributes)
+        {
+            Assert.NotNull(imgElement);
+            Assert.Equal("img", imgElement.TagName, ignoreCase: true);
+            Assert.Equal(expectedAttributes.Count + 1, imgElement.Attributes.Length); // + 1 for the id-attribute
+            foreach (var attribute in expectedAttributes)
+            {
+                Assert.Equal(attribute.Value, imgElement.GetAttribute(attribute.Key));
+            }
+        }
     }
 
     [Fact]

+ 6 - 7
src/Mvc/test/Mvc.FunctionalTests/Infrastructure/ResourceFile.cs

@@ -1,16 +1,10 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System;
-using System.Diagnostics;
 using System.Globalization;
-using System.IO;
 using System.Reflection;
 using System.Text;
-using System.Threading.Tasks;
 using Microsoft.AspNetCore.Testing;
-using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources;
-using Xunit;
 
 namespace Microsoft.AspNetCore.Mvc;
 
@@ -211,8 +205,13 @@ public static class ResourceFile
     /// <param name="content">
     /// New content of <paramref name="resourceName"/> in <paramref name="assembly"/>.
     /// </param>
-    public static void UpdateFile(Assembly assembly, string resourceName, string previousContent, string content)
+    private static void UpdateFile(Assembly assembly, string resourceName, string previousContent, string content)
     {
+        if (!GenerateBaselines)
+        {
+            throw new NotSupportedException("Calling UpdateFile is not supported when GenerateBaselines=false");
+        }
+
         // Normalize line endings to '\r\n' for comparison. This removes Environment.NewLine from the equation. Not
         // worth updating files just because we generate baselines on a different system.
         var normalizedPreviousContent = previousContent?.Replace("\r", "").Replace("\n", "\r\n");

+ 0 - 1
src/Mvc/test/Mvc.FunctionalTests/Microsoft.AspNetCore.Mvc.FunctionalTests.csproj

@@ -11,7 +11,6 @@
   <ItemGroup>
     <Compile Include="..\..\Mvc.Formatters.Xml\test\XmlAssert.cs" />
     <EmbeddedResource Include="compiler\resources\**\*" />
-    <None Remove="compiler\resources\TagHelpersWebSite.Home.GlobbingTagHelpers.html" />
     <None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
   </ItemGroup>
 

+ 0 - 35
src/Mvc/test/Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Image.html

@@ -1,35 +0,0 @@
-<!doctype html>
-<html>
-<head>
-    <meta charset="utf-8" />
-    <title>Image</title>
-
-</head>
-<body>
-
-    <h2>Image Tag Helper Test</h2>
-    <!-- Plain image tag -->
-    <img src="/images/red.png" alt="Red block" title="&lt;the title>">
-
-    <!-- Plain image tag with file version -->
-    <img src="/images/red.png?v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk" alt="Red versioned" title="Red versioned" />
-
-    <!-- Plain image tag with file version set to false -->
-    <img src="/images/red.png" alt="Red explicitly not versioned" title="Red versioned">
-
-    <!-- Plain image tag with absolute path and file version -->
-    <img src="http://contoso.com/hello/world" alt="Absolute path versioned">
-
-    <!-- Plain image tag with file version and path to file that does not exist -->
-    <img src="/images/fake.png" alt="Path to non existing file" />
-
-    <!-- Plain image tag with file version and path containing query string -->
-    <img src="/images/red.png?abc=def&amp;v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk" alt="Path with query string">
-
-    <!-- Plain image tag with file version and path containing fragment -->
-    <img src="/images/red.png?v=W2F5D366_nQ2fQqUk3URdgWy2ZekXjHzHJaY5yaiOOk#abc" alt="Path with query string" />
-
-    <!-- Plain image tag with file version and path linking to some action -->
-    <img src="/controller/action" alt="Path linking to some action">
-</body>
-</html>

+ 5 - 0
src/Mvc/test/WebSites/Directory.Build.props

@@ -1,6 +1,11 @@
 <Project>
   <PropertyGroup>
     <IsTestAssetProject>true</IsTestAssetProject>
+    <!--
+      Test websites are referenced by FunctionalTests but aren't consumed as StaticWebAssets.
+      We do not want the path mangling (e.g. _content/MyClassLib) that comes with SWA.
+    -->
+    <StaticWebAssetsEnabled>false</StaticWebAssetsEnabled>
   </PropertyGroup>
   <!-- Skip the parent folder to prevent getting test package references. -->
   <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\..\, Directory.Build.props))\Directory.Build.props" />

+ 9 - 9
src/Mvc/test/WebSites/HtmlGenerationWebSite/Views/HtmlGeneration_Home/Image.cshtml

@@ -11,27 +11,27 @@
 
     <h2>Image Tag Helper Test</h2>
     <!-- Plain image tag -->
-    <img src="~/images/red.png" alt="Red block" title="&lt;the title>">
+    <img src="~/images/red.png" alt="Red block" title="&lt;the title>" id="1">
 
     <!-- Plain image tag with file version -->
-    <img src="~/images/red.png" alt="Red versioned" title="Red versioned" asp-append-version="true" />
+    <img src="~/images/red.png" alt="Red versioned" title="Red versioned" asp-append-version="true" id="2"/>
 
     <!-- Plain image tag with file version set to false -->
-    <img src="~/images/red.png" alt="Red explicitly not versioned" title="Red versioned" asp-append-version="false">
+    <img src="~/images/red.png" alt="Red explicitly not versioned" title="Red versioned" asp-append-version="false" id="3">
 
     <!-- Plain image tag with absolute path and file version -->
-    <img src="http://contoso.com/hello/world" alt="Absolute path versioned" asp-append-version="true">
+    <img src="http://contoso.com/hello/world" alt="Absolute path versioned" asp-append-version="true" id="4">
 
     <!-- Plain image tag with file version and path to file that does not exist -->
-    <img src="~/images/fake.png" alt="Path to non existing file" asp-append-version="true" />
+    <img src="~/images/fake.png" alt="Path to non existing file" asp-append-version="true" id="5"/>
 
     <!-- Plain image tag with file version and path containing query string -->
-    <img src="~/images/red.png?abc=def" alt="Path with query string" asp-append-version="true">
+    <img src="~/images/red.png?abc=def" alt="Path with query string" asp-append-version="true" id="6">
 
     <!-- Plain image tag with file version and path containing fragment -->
-    <img src="~/images/red.png#abc" alt="Path with query string" asp-append-version="true" />
+    <img src="~/images/red.png#abc" alt="Path with query string" asp-append-version="true" id="7"/>
 
     <!-- Plain image tag with file version and path linking to some action -->
-    <img src="/controller/action" alt="Path linking to some action" asp-append-version="true">
+    <img src="/controller/action" alt="Path linking to some action" asp-append-version="true" id="8">
 </body>
-</html>
+</html>