Skip to content

Commit ffcdeda

Browse files
Copilotjaviercn
andcommitted
Fix ContentTypeProvider to respect custom content-type mappings precedence
Co-authored-by: javiercn <[email protected]>
1 parent 230fa2a commit ffcdeda

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/StaticWebAssetsSdk/Tasks/Data/ContentTypeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ private bool TryGetMapping(StaticWebAssetGlobMatcher.MatchContext context, TaskL
459459
var match = _matcher.Match(context);
460460
if (match.IsMatch)
461461
{
462-
if (_builtInMappings.TryGetValue(match.Pattern, out mapping) || _customMappings.TryGetValue(match.Pattern, out mapping))
462+
if (_customMappings.TryGetValue(match.Pattern, out mapping) || _builtInMappings.TryGetValue(match.Pattern, out mapping))
463463
{
464464
log.LogMessage(MessageImportance.Low, $"Matched {relativePath} to {mapping.MimeType} using pattern {match.Pattern}");
465465
return true;

test/Microsoft.NET.Sdk.StaticWebAssets.Tests/StaticWebAssets/ContentTypeProviderTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,57 @@ public void GetContentType_ReturnsTextPlainForCompressedTextFiles(string path)
114114
Assert.Equal("text/plain", contentType.MimeType);
115115
}
116116

117+
[Fact]
118+
public void GetContentType_CustomMappingOverridesBuiltInMapping()
119+
{
120+
// Arrange
121+
var customMapping = new ContentTypeMapping("text/html", "no-store, must-revalidate, no-cache", "*.html", 2);
122+
var provider = new ContentTypeProvider([customMapping]);
123+
124+
// Act
125+
var contentType = provider.ResolveContentTypeMapping(CreateContext("index.html"), _log);
126+
127+
// Assert
128+
Assert.Equal("text/html", contentType.MimeType);
129+
Assert.Equal("no-store, must-revalidate, no-cache", contentType.Cache);
130+
Assert.Equal("*.html", contentType.Pattern);
131+
Assert.Equal(2, contentType.Priority);
132+
}
133+
134+
[Fact]
135+
public void GetContentType_CustomMappingOverridesBuiltInMappingForCompressedFiles()
136+
{
137+
// Arrange
138+
var customMapping = new ContentTypeMapping("text/html", "no-store, must-revalidate, no-cache", "*.html", 2);
139+
var provider = new ContentTypeProvider([customMapping]);
140+
141+
// Act
142+
var contentType = provider.ResolveContentTypeMapping(CreateContext("index.html.gz"), _log);
143+
144+
// Assert
145+
Assert.Equal("text/html", contentType.MimeType);
146+
Assert.Equal("no-store, must-revalidate, no-cache", contentType.Cache);
147+
Assert.Equal("*.html", contentType.Pattern);
148+
Assert.Equal(2, contentType.Priority);
149+
}
150+
151+
[Fact]
152+
public void GetContentType_CustomJavaScriptMappingOverridesBuiltIn()
153+
{
154+
// Arrange
155+
var customMapping = new ContentTypeMapping("text/javascript", "max-age=3600", "*.js", 3);
156+
var provider = new ContentTypeProvider([customMapping]);
157+
158+
// Act
159+
var contentType = provider.ResolveContentTypeMapping(CreateContext("app.js"), _log);
160+
161+
// Assert
162+
Assert.Equal("text/javascript", contentType.MimeType);
163+
Assert.Equal("max-age=3600", contentType.Cache);
164+
Assert.Equal("*.js", contentType.Pattern);
165+
Assert.Equal(3, contentType.Priority);
166+
}
167+
117168
private class TestTaskLoggingHelper : TaskLoggingHelper
118169
{
119170
public TestTaskLoggingHelper() : base(new TestTask())

0 commit comments

Comments
 (0)