|
5 | 5 |
|
6 | 6 | using Microsoft.AspNetCore.StaticWebAssets.Tasks;
|
7 | 7 | using System.Text.Json;
|
| 8 | +using System.IO.Compression; |
8 | 9 |
|
9 | 10 | namespace Microsoft.NET.Sdk.Razor.Tests;
|
10 | 11 |
|
@@ -88,7 +89,7 @@ public void Publish_OverrideHtmlAssetPlaceholders(string testAsset, string scrip
|
88 | 89 | var indexHtmlOutputPath = Path.Combine(outputPath, "wwwroot", "index.html");
|
89 | 90 | var endpointsManifestPath = Path.Combine(outputPath, $"{projectName}.staticwebassets.endpoints.json");
|
90 | 91 |
|
91 |
| - AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath, expectFingerprintOnScript: expectFingerprintOnScript, expectPreloadElement: testAsset == "VanillaWasm"); |
| 92 | + AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath, expectFingerprintOnScript: expectFingerprintOnScript, expectPreloadElement: testAsset == "VanillaWasm", assertHtmlCompressed: true); |
92 | 93 | }
|
93 | 94 |
|
94 | 95 | private void FingerprintUserJavascriptAssets(bool fingerprintUserJavascriptAssets)
|
@@ -125,38 +126,81 @@ private void ReplaceStringInIndexHtml(TestAsset testAsset, string sourceValue, s
|
125 | 126 | }
|
126 | 127 | }
|
127 | 128 |
|
128 |
| - private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath, bool expectFingerprintOnScript = true, bool expectPreloadElement = false) |
| 129 | + private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath, bool expectFingerprintOnScript = true, bool expectPreloadElement = false, bool assertHtmlCompressed = false) |
129 | 130 | {
|
130 |
| - var indexHtmlContent = File.ReadAllText(indexHtmlPath); |
131 |
| - var endpoints = JsonSerializer.Deserialize<StaticWebAssetEndpointsManifest>(File.ReadAllText(endpointsManifestPath)); |
132 | 131 |
|
| 132 | + var endpoints = JsonSerializer.Deserialize<StaticWebAssetEndpointsManifest>(File.ReadAllText(endpointsManifestPath)); |
133 | 133 | var fingerprintedScriptPath = GetFingerprintedPath(scriptPath);
|
134 |
| - if (expectFingerprintOnScript) |
| 134 | + |
| 135 | + var indexHtmlContent = File.ReadAllText(indexHtmlPath); |
| 136 | + AssertHtmlContent(indexHtmlContent); |
| 137 | + |
| 138 | + if (assertHtmlCompressed) |
135 | 139 | {
|
136 |
| - Assert.DoesNotContain($"src=\"{scriptPath}\"", indexHtmlContent); |
137 |
| - Assert.Contains($"src=\"{fingerprintedScriptPath}\"", indexHtmlContent); |
| 140 | + var indexHtmlGzipContent = DecompressGzipFile(indexHtmlPath + ".gz"); |
| 141 | + AssertHtmlContent(indexHtmlGzipContent); |
| 142 | + |
| 143 | + var indexHtmlBrotliContent = DecompressBrotliFile(indexHtmlPath + ".br"); |
| 144 | + AssertHtmlContent(indexHtmlBrotliContent); |
138 | 145 | }
|
139 |
| - else |
| 146 | + |
| 147 | + void AssertHtmlContent(string content) |
140 | 148 | {
|
141 |
| - Assert.Contains(scriptPath, indexHtmlContent); |
| 149 | + if (expectFingerprintOnScript) |
| 150 | + { |
| 151 | + Assert.DoesNotContain($"src=\"{scriptPath}\"", content); |
| 152 | + Assert.Contains($"src=\"{fingerprintedScriptPath}\"", content); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + Assert.Contains(scriptPath, content); |
| 157 | + |
| 158 | + if (scriptPath != fingerprintedScriptPath) |
| 159 | + { |
| 160 | + Assert.DoesNotContain(fingerprintedScriptPath, content); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + Assert.Contains(GetFingerprintedPath("_framework/dotnet.js"), content); |
| 165 | + Assert.Contains(GetFingerprintedPath("_framework/dotnet.native.js"), content); |
| 166 | + Assert.Contains(GetFingerprintedPath("_framework/dotnet.runtime.js"), content); |
142 | 167 |
|
143 |
| - if (scriptPath != fingerprintedScriptPath) |
| 168 | + if (expectPreloadElement) |
144 | 169 | {
|
145 |
| - Assert.DoesNotContain(fingerprintedScriptPath, indexHtmlContent); |
| 170 | + Assert.DoesNotContain("<link rel=\"preload\"", content); |
| 171 | + Assert.Contains($"<link href=\"{fingerprintedScriptPath}\" rel=\"preload\" as=\"script\" fetchpriority=\"high\" crossorigin=\"anonymous\"", content); |
146 | 172 | }
|
147 | 173 | }
|
148 | 174 |
|
149 |
| - Assert.Contains(GetFingerprintedPath("_framework/dotnet.js"), indexHtmlContent); |
150 |
| - Assert.Contains(GetFingerprintedPath("_framework/dotnet.native.js"), indexHtmlContent); |
151 |
| - Assert.Contains(GetFingerprintedPath("_framework/dotnet.runtime.js"), indexHtmlContent); |
| 175 | + string GetFingerprintedPath(string route) |
| 176 | + => endpoints.Endpoints.FirstOrDefault(e => e.Route == route && e.Selectors.Length == 0)?.AssetFile ?? throw new Exception($"Missing endpoint for file '{route}' in '{endpointsManifestPath}'"); |
152 | 177 |
|
153 |
| - if (expectPreloadElement) |
| 178 | + string DecompressGzipFile(string path) |
154 | 179 | {
|
155 |
| - Assert.DoesNotContain("<link rel=\"preload\">", indexHtmlContent); |
156 |
| - Assert.Contains($"<link href=\"{fingerprintedScriptPath}\" rel=\"preload\" as=\"script\" fetchpriority=\"high\" crossorigin=\"anonymous\"", indexHtmlContent); |
| 180 | + if (File.Exists(path)) |
| 181 | + { |
| 182 | + using var fileStream = File.OpenRead(path); |
| 183 | + using var compressedStream = new GZipStream(fileStream, CompressionMode.Decompress); |
| 184 | + using var reader = new StreamReader(compressedStream); |
| 185 | + return reader.ReadToEnd(); |
| 186 | + } |
| 187 | + |
| 188 | + Assert.Fail($"File '{path}' does not exist."); |
| 189 | + return null; |
157 | 190 | }
|
158 | 191 |
|
159 |
| - string GetFingerprintedPath(string route) |
160 |
| - => endpoints.Endpoints.FirstOrDefault(e => e.Route == route && e.Selectors.Length == 0)?.AssetFile ?? throw new Exception($"Missing endpoint for file '{route}' in '{endpointsManifestPath}'"); |
| 192 | + string DecompressBrotliFile(string path) |
| 193 | + { |
| 194 | + if (File.Exists(path)) |
| 195 | + { |
| 196 | + using var fileStream = File.OpenRead(path); |
| 197 | + using var compressedStream = new BrotliStream(fileStream, CompressionMode.Decompress); |
| 198 | + using var reader = new StreamReader(compressedStream); |
| 199 | + return reader.ReadToEnd(); |
| 200 | + } |
| 201 | + |
| 202 | + Assert.Fail($"File '{path}' does not exist."); |
| 203 | + return null; |
| 204 | + } |
161 | 205 | }
|
162 | 206 | }
|
0 commit comments