< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Web.Utility
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Web/Utility.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 66
Coverable lines: 66
Total lines: 212
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 62
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
IsXmlContent(...)0%440%
IsJsonContent(...)0%440%
CombineUri(...)0%12120%
QuoteAwareStringSplit(...)0%220%
QuoteAwareSubString(...)0%28280%
GetContentType(...)0%220%
GetContentTypeOrNull(...)0%880%
IEnumerableToCommaSeparatedString(...)100%110%
AddRange(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Web/Utility.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Net.Mime;
 7using System.Text;
 8using CoreWCF.Runtime;
 9
 10namespace CoreWCF.Web
 11{
 12    internal static class Utility
 13    {
 14        public const string ApplicationXml = "application/xml";
 15        public const string TextXml = "text/xml";
 16        public const string ApplicationJson = "application/json";
 17        public const string TextJson = "text/json";
 18        public const string GET = "GET";
 19
 20        public static bool IsXmlContent(this string contentType)
 21        {
 022            if (contentType == null)
 23            {
 024                return true;
 25            }
 26
 027            string contentTypeProcessed = contentType.Trim();
 28
 029            return contentTypeProcessed.StartsWith(ApplicationXml, StringComparison.OrdinalIgnoreCase)
 030                || contentTypeProcessed.StartsWith(TextXml, StringComparison.OrdinalIgnoreCase);
 31        }
 32
 33        public static bool IsJsonContent(this string contentType)
 34        {
 035            if (contentType == null)
 36            {
 037                return true;
 38            }
 39
 040            string contentTypeProcessed = contentType.Trim();
 41
 042            return contentTypeProcessed.StartsWith(ApplicationJson, StringComparison.OrdinalIgnoreCase)
 043                || contentTypeProcessed.StartsWith(TextJson, StringComparison.OrdinalIgnoreCase);
 44        }
 45
 46        public static string CombineUri(string former, string latter)
 47        {
 48            // Appending the latter string to the form string,
 49            // while making sure there is a single slash char seperating the latter and the former.
 50            // This method behaves differently than new Uri(baseUri, relativeUri)
 51            // as CombineUri simply appends, whereas new Uri() actually replaces the last segment
 52            // of the its base path with the relative uri.
 53
 054            var builder = new StringBuilder();
 055            if (former.Length > 0 && latter.Length > 0)
 56            {
 057                if (former[former.Length - 1] == '/' && latter[0] == '/')
 58                {
 059                    builder.Append(former, 0, former.Length - 1);
 060                    builder.Append(latter);
 061                    return builder.ToString();
 62                }
 63
 064                if (former[former.Length - 1] != '/' && latter[0] != '/')
 65                {
 066                    builder.Append(former);
 067                    builder.Append('/');
 068                    builder.Append(latter);
 069                    return builder.ToString();
 70                }
 71            }
 72
 073            return former + latter;
 74        }
 75
 76        public static List<string> QuoteAwareStringSplit(string str)
 77        {
 078            List<string> subStrings = new List<string>();
 079            int offset = 0;
 080            while (true)
 81            {
 082                string subString = QuoteAwareSubString(str, ref offset);
 083                if (subString == null)
 84                {
 85                    break;
 86                }
 087                subStrings.Add(subString);
 88            }
 89
 090            return subStrings;
 91        }
 92
 93        // This method extracts substrings from a string starting at the offset
 94        // and up until the next comma in the string.  The sub string extraction is
 95        // quote aware such that commas inside quoted-strings are ignored.  On return,
 96        // offset points to the next char beyond the comma of the substring returned
 97        // and may point beyond the length of the header.
 98        public static string QuoteAwareSubString(string str, ref int offset)
 99        {
 100            // this method will filter out empty-string and white-space-only items in
 101            // the header.  For example "x,,y" and "x, ,y" would result in just "x" and "y"
 102            // substrings being returned.
 103
 0104            if (string.IsNullOrEmpty(str) || offset >= str.Length)
 105            {
 0106                return null;
 107            }
 108
 0109            int startIndex = (offset > 0) ? offset : 0;
 110
 111            // trim whitespace and commas from the begining of the item
 0112            while (char.IsWhiteSpace(str[startIndex]) || str[startIndex] == ',')
 113            {
 0114                startIndex++;
 0115                if (startIndex >= str.Length)
 116                {
 0117                    return null;
 118                }
 119            }
 120
 0121            int endIndex = startIndex;
 0122            bool insideQuotes = false;
 123
 0124            while (endIndex < str.Length)
 125            {
 0126                if (str[endIndex] == '\"' &&
 0127                   (!insideQuotes || endIndex == 0 || str[endIndex - 1] != '\\'))
 128                {
 0129                    insideQuotes = !insideQuotes;
 130                }
 0131                else if (str[endIndex] == ',' && !insideQuotes)
 132                {
 133                    break;
 134                }
 0135                endIndex++;
 136            }
 0137            offset = endIndex + 1;
 138
 139            // trim whitespace from the end of the item; the substring is guaranteed to
 140            // have at least one non-whitespace character
 0141            while (char.IsWhiteSpace(str[endIndex - 1]))
 142            {
 0143                endIndex--;
 144            }
 145
 0146            return str.Substring(startIndex, endIndex - startIndex);
 147        }
 148
 149        public static ContentType GetContentType(string contentType)
 150        {
 0151            string contentTypeTrimmed = contentType.Trim();
 0152            if (!string.IsNullOrEmpty(contentTypeTrimmed))
 153            {
 0154                return GetContentTypeOrNull(contentTypeTrimmed);
 155            }
 156
 0157            return null;
 158        }
 159
 160        public static ContentType GetContentTypeOrNull(string contentType)
 161        {
 162            try
 163            {
 164                Fx.Assert(contentType == contentType.Trim(), "The ContentType input argument should already be trimmed."
 165                Fx.Assert(!string.IsNullOrEmpty(contentType), "The ContentType input argument should not be null or empt
 166
 0167                ContentType contentTypeToReturn = new ContentType(contentType);
 168
 169                // Need to check for "*/<Something-other-than-*>" because the ContentType constructor doesn't catch this
 0170                string[] typeAndSubType = contentTypeToReturn.MediaType.Split('/');
 171                Fx.Assert(typeAndSubType.Length == 2, "The creation of the ContentType would have failed if there wasn't
 0172                if (typeAndSubType[0][0] == '*' && typeAndSubType[0].Length == 1 &&
 0173                    !(typeAndSubType[1][0] == '*' && typeAndSubType[1].Length == 1))
 174                {
 175                    //
 176
 177
 178
 179                    // throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new FormatException(
 180                    // SR2.GetString(SR2.InvalidContentType, contentType)));
 0181                    return null;
 182                }
 0183                return contentTypeToReturn;
 184            }
 0185            catch (FormatException)
 186            {
 187                // Return null to indicate that the content type creation failed
 188                //System.ServiceModel.DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
 0189            }
 190
 0191            return null;
 0192        }
 193
 194        public static string IEnumerableToCommaSeparatedString(IEnumerable<string> items)
 195        {
 196            Fx.Assert(items != null, "The 'items' argument should never be null.");
 197
 0198            return string.Join(", ", items);
 199        }
 200
 201        public static void AddRange<T>(ICollection<T> list, IEnumerable<T> itemsToAdd)
 202        {
 203            Fx.Assert(list != null, "The 'list' argument should never be null.");
 204            Fx.Assert(itemsToAdd != null, "The 'itemsToAdd' argument should never be null.");
 205
 0206            foreach (T item in itemsToAdd)
 207            {
 0208                list.Add(item);
 209            }
 0210        }
 211    }
 212}