Skip to content

Fix ContentTypeProvider to respect custom content-type mappings precedence #50153

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/StaticWebAssetsSdk/Tasks/Data/ContentTypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private bool TryGetMapping(StaticWebAssetGlobMatcher.MatchContext context, TaskL
var match = _matcher.Match(context);
if (match.IsMatch)
{
if (_builtInMappings.TryGetValue(match.Pattern, out mapping) || _customMappings.TryGetValue(match.Pattern, out mapping))
if (_customMappings.TryGetValue(match.Pattern, out mapping) || _builtInMappings.TryGetValue(match.Pattern, out mapping))
{
log.LogMessage(MessageImportance.Low, $"Matched {relativePath} to {mapping.MimeType} using pattern {match.Pattern}");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,57 @@ public void GetContentType_ReturnsTextPlainForCompressedTextFiles(string path)
Assert.Equal("text/plain", contentType.MimeType);
}

[Fact]
public void GetContentType_CustomMappingOverridesBuiltInMapping()
{
// Arrange
var customMapping = new ContentTypeMapping("text/html", "no-store, must-revalidate, no-cache", "*.html", 2);
var provider = new ContentTypeProvider([customMapping]);

// Act
var contentType = provider.ResolveContentTypeMapping(CreateContext("index.html"), _log);

// Assert
Assert.Equal("text/html", contentType.MimeType);
Assert.Equal("no-store, must-revalidate, no-cache", contentType.Cache);
Assert.Equal("*.html", contentType.Pattern);
Assert.Equal(2, contentType.Priority);
}

[Fact]
public void GetContentType_CustomMappingOverridesBuiltInMappingForCompressedFiles()
{
// Arrange
var customMapping = new ContentTypeMapping("text/html", "no-store, must-revalidate, no-cache", "*.html", 2);
var provider = new ContentTypeProvider([customMapping]);

// Act
var contentType = provider.ResolveContentTypeMapping(CreateContext("index.html.gz"), _log);

// Assert
Assert.Equal("text/html", contentType.MimeType);
Assert.Equal("no-store, must-revalidate, no-cache", contentType.Cache);
Assert.Equal("*.html", contentType.Pattern);
Assert.Equal(2, contentType.Priority);
}

[Fact]
public void GetContentType_CustomJavaScriptMappingOverridesBuiltIn()
{
// Arrange
var customMapping = new ContentTypeMapping("text/javascript", "max-age=3600", "*.js", 3);
var provider = new ContentTypeProvider([customMapping]);

// Act
var contentType = provider.ResolveContentTypeMapping(CreateContext("app.js"), _log);

// Assert
Assert.Equal("text/javascript", contentType.MimeType);
Assert.Equal("max-age=3600", contentType.Cache);
Assert.Equal("*.js", contentType.Pattern);
Assert.Equal(3, contentType.Priority);
}

private class TestTaskLoggingHelper : TaskLoggingHelper
{
public TestTaskLoggingHelper() : base(new TestTask())
Expand Down