Skip to content

Commit

Permalink
fix: allows setting charset with MIME mapping (#142)
Browse files Browse the repository at this point in the history
* Edit regex of mime command line option for accept charset
* Add unit test for charset
  • Loading branch information
pknam authored Jun 25, 2023
1 parent ace5b81 commit b25bb58
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/dotnet-serve/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public virtual bool UseTls
public virtual string CertificatePassword { get; internal set; }

[Option("-m|--mime <MAPPING>", CommandOptionType.MultipleValue, Description = "Add a mapping from file extension to MIME type. Empty MIME removes a mapping.\nExpected format is <EXT>=<MIME>.")]
[RegularExpression(@"^([^=]+)=([^=]*)$", ErrorMessage = "MIME mappings must have the form: ext=mime/type")]
[RegularExpression(@"^([^=]+)=(.*)$", ErrorMessage = "MIME mappings must have the form: ext=mime/type")]
public string[] MimeMappings { get; internal set; }

// Internal, experimental flag. If you found this, it may break in the future.
Expand Down
6 changes: 4 additions & 2 deletions test/dotnet-serve.Tests/MimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public MimeTests(ITestOutputHelper output)
[InlineData("/file.js", "contents_of_file.js\n", "application/my-js", "js=application/my-js")]
// Remove a default mapping.
[InlineData("/file.js", "contents_of_file.js\n", null, "js=")]
// Add a mapping with charset.
[InlineData("/file.log", "contents of file.log\n", "text/plain; charset=utf-8", ".log=text/plain; charset=utf-8")]
public async Task ItAppliesMimeMappings(string file, string expectedContents, string expectedMime, params string[] mimeMap)
{
var path = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Mime");
Expand All @@ -38,9 +40,9 @@ public async Task ItAppliesMimeMappings(string file, string expectedContents, st
}
else
{
Assert.Equal(resp.Content.Headers.ContentType.MediaType, expectedMime);
Assert.Equal(expectedMime, resp.Content.Headers.ContentType.ToString());
}
var respTxt = await resp.Content.ReadAsStringAsync();
Assert.Equal(respTxt, expectedContents);
Assert.Equal(expectedContents, respTxt);
}
}
1 change: 1 addition & 0 deletions test/dotnet-serve.Tests/TestAssets/Mime/file.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contents of file.log

0 comments on commit b25bb58

Please sign in to comment.