< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.UriTemplateClientFormatter
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/UriTemplateClientFormatter.cs
Line coverage
56%
Covered lines: 33
Uncovered lines: 25
Coverable lines: 58
Total lines: 128
Line coverage: 56.8%
Branch coverage
60%
Covered branches: 18
Total branches: 30
Branch coverage: 60%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
DeserializeReply(...)100%110%
SerializeRequest(...)100%110%
GetUTStringOrDefault(...)50%6666.66%
Populate(...)75%202074.35%
MakeDefaultGetUTString(...)0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/UriTemplateClientFormatter.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.Text;
 7using System.Xml;
 8using CoreWCF.Channels;
 9using CoreWCF.Description;
 10
 11namespace CoreWCF.Dispatcher
 12{
 13    internal class UriTemplateClientFormatter : IClientMessageFormatter
 14    {
 015        public object DeserializeReply(Message message, object[] parameters) => throw new PlatformNotSupportedException(
 16
 017        public Message SerializeRequest(MessageVersion messageVersion, object[] parameters) => throw new PlatformNotSupp
 18
 19        internal static string GetUTStringOrDefault(OperationDescription operationDescription)
 20        {
 48921            string utString = WebHttpBehavior.GetWebUriTemplate(operationDescription);
 48922            if (utString == null && WebHttpBehavior.GetWebMethod(operationDescription) == WebHttpBehavior.GET)
 23            {
 024                utString = MakeDefaultGetUTString(operationDescription);
 25            }
 26
 48927            if (utString == null)
 28            {
 029                utString = operationDescription.Name; // note: not + "/*", see 8988 and 9653
 30            }
 31
 48932            return utString;
 33        }
 34
 35        internal static void Populate(out Dictionary<int, string> pathMapping,
 36            out Dictionary<int, KeyValuePair<string, Type>> queryMapping,
 37            out int totalNumUTVars,
 38            out UriTemplate uriTemplate,
 39            OperationDescription operationDescription,
 40            QueryStringConverter qsc,
 41            string contractName)
 42        {
 32643            pathMapping = new Dictionary<int, string>();
 32644            queryMapping = new Dictionary<int, KeyValuePair<string, Type>>();
 32645            string utString = GetUTStringOrDefault(operationDescription);
 32646            uriTemplate = new UriTemplate(utString);
 32647            List<string> neededPathVars = new List<string>(uriTemplate.PathSegmentVariableNames);
 32648            List<string> neededQueryVars = new List<string>(uriTemplate.QueryValueVariableNames);
 32649            Dictionary<string, byte> alreadyGotVars = new Dictionary<string, byte>(StringComparer.OrdinalIgnoreCase);
 32650            totalNumUTVars = neededPathVars.Count + neededQueryVars.Count;
 91251            for (int i = 0; i < operationDescription.Messages[0].Body.Parts.Count; ++i)
 52            {
 13053                MessagePartDescription mpd = operationDescription.Messages[0].Body.Parts[i];
 13054                string parameterName = XmlConvert.DecodeName(mpd.Name);
 13055                if (alreadyGotVars.ContainsKey(parameterName))
 56                {
 057                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 058                        SR.Format(SR.UriTemplateVarCaseDistinction, XmlConvert.DecodeName(operationDescription.Name), co
 59                }
 60
 13061                List<string> neededPathCopy = new List<string>(neededPathVars);
 45262                foreach (string pathVar in neededPathCopy)
 63                {
 9664                    if (string.Compare(parameterName, pathVar, StringComparison.OrdinalIgnoreCase) == 0)
 65                    {
 8066                        if (mpd.Type != typeof(string))
 67                        {
 068                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 069                                SR.Format(SR.UriTemplatePathVarMustBeString, XmlConvert.DecodeName(operationDescription.
 70                        }
 8071                        pathMapping.Add(i, parameterName);
 8072                        alreadyGotVars.Add(parameterName, 0);
 8073                        neededPathVars.Remove(pathVar);
 74                    }
 75                }
 76
 13077                List<string> neededQueryCopy = new List<string>(neededQueryVars);
 29278                foreach (string queryVar in neededQueryCopy)
 79                {
 1680                    if (string.Compare(parameterName, queryVar, StringComparison.OrdinalIgnoreCase) == 0)
 81                    {
 1682                        if (!qsc.CanConvert(mpd.Type))
 83                        {
 084                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 085                                SR.Format(SR.UriTemplateQueryVarMustBeConvertible, XmlConvert.DecodeName(operationDescri
 86                        }
 1687                        queryMapping.Add(i, new KeyValuePair<string, Type>(parameterName, mpd.Type));
 1688                        alreadyGotVars.Add(parameterName, 0);
 1689                        neededQueryVars.Remove(queryVar);
 90                    }
 91                }
 92            }
 93
 32694            if (neededPathVars.Count != 0)
 95            {
 096                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(
 097                    SR.Format(SR.UriTemplateMissingVar, XmlConvert.DecodeName(operationDescription.Name), contractName, 
 98            }
 99
 326100            if (neededQueryVars.Count != 0)
 101            {
 0102                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format
 0103                    (SR.Format(SR.UriTemplateMissingVar, XmlConvert.DecodeName(operationDescription.Name), contractName,
 104            }
 326105        }
 106
 107        private static string MakeDefaultGetUTString(OperationDescription od)
 108        {
 0109            StringBuilder sb = new StringBuilder(XmlConvert.DecodeName(od.Name));
 110            //sb.Append("/*"); // note: not + "/*", see 8988 and 9653
 0111            if (!WebHttpBehavior.IsUntypedMessage(od.Messages[0]))
 112            {
 0113                sb.Append("?");
 0114                foreach (MessagePartDescription mpd in od.Messages[0].Body.Parts)
 115                {
 0116                    string parameterName = XmlConvert.DecodeName(mpd.Name);
 0117                    sb.Append(parameterName);
 0118                    sb.Append("={");
 0119                    sb.Append(parameterName);
 0120                    sb.Append("}&");
 121                }
 0122                sb.Remove(sb.Length - 1, 1);
 123            }
 124
 0125            return sb.ToString();
 126        }
 127    }
 128}