8000 Allow to fingerprint Blazor.js by maraf · Pull Request #46988 · dotnet/sdk · GitHub
[go: up one dir, main page]

Skip to content

Allow to fingerprint Blazor.js #46988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 3, 2025
Prev Previous commit
Next Next commit
Fix testasset used by other tests
  • Loading branch information
maraf committed Feb 21, 2025
commit c481361251f2ff7239d82f689b62e901b017ca96
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ public void Build_FingerprintsContent_WhenEnabled()
AssertBuildAssets(manifest1, outputPath, intermediateOutputPath);
}

public static TheoryData<string, string> WriteImportMapToHtmlData => new TheoryData<string, string>
public static TheoryData<string, string, string> WriteImportMapToHtmlData => new TheoryData<string, string, string>
{
{ "VanillaWasm", "main.js" },
{ "BlazorWasmMinimal", "_framework/blazor.webassembly.js" }
{ "VanillaWasm", "main.js", null },
{ "BlazorWasmMinimal", "_framework/blazor.webassembly.js", "_framework/blazor.webassembly#[.{fingerprint}].js" }
};

[Theory]
[MemberData(nameof(WriteImportMapToHtmlData))]
public void Build_WriteImportMapToHtml(string testAsset, string scriptPath)
public void Build_WriteImportMapToHtml(string testAsset, string scriptPath, string scriptPathWithFingerprintPattern)
{
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
ReplaceStringInIndexHtml(ProjectDirectory, scriptPath, scriptPathWithFingerprintPattern);

var build = CreateBuildCommand(ProjectDirectory);
ExecuteCommand(build, "-p:WriteImportMapToHtml=true").Should().Pass();
Expand All @@ -70,20 +71,35 @@ public void Build_WriteImportMapToHtml(string testAsset, string scriptPath)

[Theory]
[MemberData(nameof(WriteImportMapToHtmlData))]
public void Publish_WriteImportMapToHtml(string testAsset, string scriptPath)
public void Publish_WriteImportMapToHtml(string testAsset, string scriptPath, string scriptPathWithFingerprintPattern)
{
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
ReplaceStringInIndexHtml(ProjectDirectory, scriptPath, scriptPathWithFingerprintPattern);

var projectName = Path.GetFileNameWithoutExtension(Directory.EnumerateFiles(ProjectDirectory.TestRoot, "*.csproj").Single());

var publish = CreatePublishCommand(ProjectDirectory);
ExecuteCommand(publish, "-p:WriteImportMapToHtml=true").Should().Pass();

var outputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
var indexHtmlPath = Path.Combine(outputPath, "wwwroot", "index.html");
var indexHtmlOutputPath = Path.Combine(outputPath, "wwwroot", "index.html");
var endpointsManifestPath = Path.Combine(outputPath, $"{projectName}.staticwebassets.endpoints.json");

AssertImportMapInHtml(indexHtmlPath, endpointsManifestPath, scriptPath);
AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath);
}

private void ReplaceStringInIndexHtml(TestAsset testAsset, string scriptPath, string scriptPathWithFingerprintPattern)
{
if (scriptPathWithFingerprintPattern != null)
{
var indexHtmlPath = Path.Combine(testAsset.TestRoot, "wwwroot", "index.html");
var indexHtmlContent = File.ReadAllText(indexHtmlPath);
var newIndexHtmlContent = indexHtmlContent.Replace(scriptPath, scriptPathWithFingerprintPattern);
if (indexHtmlContent == newIndexHtmlContent)
throw new Exception($"Script replacement '{scriptPath}' for '{scriptPathWithFingerprintPattern}' didn't produce any change in '{indexHtmlPath}'");

File.WriteAllText(indexHtmlPath, newIndexHtmlContent);
}
}

private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webassembly#[.{fingerprint}].js"></script>
<script src="_framework/blazor.webassembly.js"></script>
</body>

</html>
Loading
0