< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Utilities.MediaTypeHelper
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/MediaTypeHelper.cs
Tag: Kestrun/Kestrun@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
100%
Covered lines: 46
Uncovered lines: 0
Coverable lines: 46
Total lines: 85
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 01/17/2026 - 04:33:35 Line coverage: 100% (46/46) Branch coverage: 100% (12/12) Total lines: 85 Tag: Kestrun/Kestrun@aca34ea8d284564e2f9f6616dc937668dce926ba 01/17/2026 - 04:33:35 Line coverage: 100% (46/46) Branch coverage: 100% (12/12) Total lines: 85 Tag: Kestrun/Kestrun@aca34ea8d284564e2f9f6616dc937668dce926ba

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Normalize(...)100%44100%
Canonicalize(...)100%88100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/MediaTypeHelper.cs

#LineLine coverage
 1namespace Kestrun.Utilities;
 2
 3internal static class MediaTypeHelper
 4{
 15    private static readonly Dictionary<string, string> ExactMap =
 16        new(StringComparer.OrdinalIgnoreCase)
 17        {
 18            // Canonical types
 19            ["application/json"] = "application/json",
 110            ["application/xml"] = "application/xml",
 111            ["application/yaml"] = "application/yaml",
 112            ["application/cbor"] = "application/cbor",
 113
 114            // Common aliases
 115            ["application/x-yaml"] = "application/yaml",
 116            ["text/yaml"] = "application/yaml",
 117            ["text/xml"] = "application/xml",
 118
 119            // Other supported
 120            ["application/bson"] = "application/bson",
 121            ["text/csv"] = "text/csv",
 122            ["application/x-www-form-urlencoded"] = "application/x-www-form-urlencoded",
 123
 124            // Wildcards (treat as "fine, give me the default")
 125            ["*/*"] = "text/plain",
 126            ["text/*"] = "text/plain",
 127            ["application/*"] = "text/plain",
 128        };
 29
 130    private static readonly (string Suffix, string Canonical)[] StructuredSuffixMap =
 131    [
 132        ("+json", "application/json"),
 133        ("+xml",  "application/xml"),
 134        ("+yaml", "application/yaml"),
 135        ("+yml",  "application/yaml"),
 136        ("+cbor", "application/cbor"),
 137    ];
 38
 39    /// <summary>
 40    /// Normalizes the given content type by trimming whitespace and removing parameters.
 41    /// </summary>
 42    /// <param name="contentType">The content type to normalize.</param>
 43    /// <returns>The normalized media type.</returns>
 44    public static string Normalize(string? contentType)
 45    {
 4346        if (string.IsNullOrWhiteSpace(contentType))
 47        {
 648            return string.Empty;
 49        }
 50
 3751        var semicolon = contentType.IndexOf(';');
 3752        var mediaType = semicolon >= 0 ? contentType[..semicolon] : contentType;
 53
 3754        return mediaType.Trim().ToLowerInvariant();
 55    }
 56
 57    /// <summary>
 58    /// Canonicalizes the given content type to a known media type.
 59    /// </summary>
 60    /// <param name="contentType">The content type to canonicalize.</param>
 61    /// <returns>The canonical media type.</returns>
 62    public static string Canonicalize(string? contentType)
 63    {
 3664        var normalized = Normalize(contentType);
 3665        if (normalized.Length == 0)
 66        {
 367            return string.Empty;
 68        }
 69
 3370        if (ExactMap.TryGetValue(normalized, out var exact))
 71        {
 2572            return exact;
 73        }
 74
 5375        foreach (var (suffix, canonical) in StructuredSuffixMap)
 76        {
 2277            if (normalized.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
 78            {
 779                return canonical;
 80            }
 81        }
 82
 183        return normalized;
 84    }
 85}