< Summary

Class:Azure.Core.Pipeline.ContentTypeUtilities
Assembly:Azure.Data.Tables
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\ContentTypeUtilities.cs
Covered lines:15
Uncovered lines:4
Coverable lines:19
Total lines:58
Line coverage:78.9% (15 of 19)
Covered branches:15
Total branches:18
Branch coverage:83.3% (15 of 18)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
TryGetTextEncoding(...)-78.95%83.33%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\ContentTypeUtilities.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable disable
 5
 6using System;
 7using System.Text;
 8
 9namespace Azure.Core.Pipeline
 10{
 11    internal static class ContentTypeUtilities
 12    {
 13        public static bool TryGetTextEncoding(string contentType, out Encoding encoding)
 14        {
 15            const string charsetMarker = "; charset=";
 16            const string utf8Charset = "utf-8";
 17            const string textContentTypePrefix = "text/";
 18            const string jsonSuffix = "json";
 19            const string appJsonPrefix = "application/json";
 20            const string xmlSuffix = "xml";
 21            const string urlEncodedSuffix = "-urlencoded";
 22
 23            // Default is technically US-ASCII, but will default to UTF-8 which is a superset.
 24            const string appFormUrlEncoded = "application/x-www-form-urlencoded";
 25
 6426            if (contentType == null)
 27            {
 028                encoding = null;
 029                return false;
 30            }
 31
 6432            var charsetIndex = contentType.IndexOf(charsetMarker, StringComparison.OrdinalIgnoreCase);
 6433            if (charsetIndex != -1)
 34            {
 4435                ReadOnlySpan<char> charset = contentType.AsSpan().Slice(charsetIndex + charsetMarker.Length);
 4436                if (charset.StartsWith(utf8Charset.AsSpan(), StringComparison.OrdinalIgnoreCase))
 37                {
 4438                    encoding = Encoding.UTF8;
 4439                    return true;
 40                }
 41            }
 42
 2043            if (contentType.StartsWith(textContentTypePrefix, StringComparison.OrdinalIgnoreCase) ||
 2044                contentType.EndsWith(jsonSuffix, StringComparison.OrdinalIgnoreCase) ||
 2045                contentType.EndsWith(xmlSuffix, StringComparison.OrdinalIgnoreCase) ||
 2046                contentType.EndsWith(urlEncodedSuffix, StringComparison.OrdinalIgnoreCase) ||
 2047                contentType.StartsWith(appJsonPrefix, StringComparison.OrdinalIgnoreCase) ||
 2048                contentType.StartsWith(appFormUrlEncoded, StringComparison.OrdinalIgnoreCase))
 49            {
 2050                encoding = Encoding.UTF8;
 2051                return true;
 52            }
 53
 054            encoding = null;
 055            return false;
 56        }
 57    }
 58}

Methods/Properties

TryGetTextEncoding(...)