Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 0bab92c

Browse files
committed
Improve MimeTypes.GetExtension()
1 parent a17d6ca commit 0bab92c

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
namespace ServiceStack
1414
{
15-
public static class HttpUtils
15+
public static partial class HttpUtils
16+
{
17+
18+
}
19+
20+
public static partial class HttpUtils
1621
{
1722
public static string UserAgent = "ServiceStack.Text";
1823

@@ -781,10 +786,8 @@ public static Stream SendStreamToUrl(this string url, string method = null,
781786

782787
if (requestBody != null)
783788
{
784-
using (var req = PclExport.Instance.GetRequestStream(webReq))
785-
{
786-
requestBody.CopyTo(req);
787-
}
789+
using var req = PclExport.Instance.GetRequestStream(webReq);
790+
requestBody.CopyTo(req);
788791
}
789792

790793
var webRes = PclExport.Instance.GetResponse(webReq);
@@ -1431,8 +1434,8 @@ public static string GetExtension(string mimeType)
14311434
}
14321435

14331436
var parts = mimeType.Split('/');
1434-
if (parts.Length == 1) return "." + parts[0];
1435-
if (parts.Length == 2) return "." + parts[1];
1437+
if (parts.Length == 1) return "." + parts[0].LeftPart('+').LeftPart(';');
1438+
if (parts.Length == 2) return "." + parts[1].LeftPart('+').LeftPart(';');
14361439

14371440
throw new NotSupportedException("Unknown mimeType: " + mimeType);
14381441
}

tests/ServiceStack.Text.Tests/HttpUtilsTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,14 @@ public void Can_SetHashParam()
6464
Assert.That("http://example.com#s=rf/f=1".SetHashParam("f", "2"), Is.EqualTo("http://example.com#s=rf/f=2"));
6565
Assert.That("http://example.com#ab=0".SetHashParam("a", "1"), Is.EqualTo("http://example.com#ab=0/a=1"));
6666
}
67+
68+
[Test]
69+
public void Can_get_MimeType_file_extension()
70+
{
71+
Assert.That(MimeTypes.GetExtension(MimeTypes.Html), Is.EqualTo(".html"));
72+
Assert.That(MimeTypes.GetExtension(MimeTypes.HtmlUtf8), Is.EqualTo(".html"));
73+
Assert.That(MimeTypes.GetExtension(MimeTypes.ImagePng), Is.EqualTo(".png"));
74+
Assert.That(MimeTypes.GetExtension(MimeTypes.ImageSvg), Is.EqualTo(".svg"));
75+
}
6776
}
6877
}

0 commit comments

Comments
 (0)