< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.UriTemplateLiteralPathSegment
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/UriTemplateLiteralPathSegment.cs
Line coverage
59%
Covered lines: 25
Uncovered lines: 17
Coverable lines: 42
Total lines: 145
Line coverage: 59.5%
Branch coverage
40%
Covered branches: 9
Total branches: 22
Branch coverage: 40.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor(...)100%22100%
CreateFromUriTemplate(...)50%6661.53%
CreateFromWireData(...)100%11100%
AsUnescapedString()100%11100%
Bind(...)100%22100%
CompareTo(...)100%110%
Equals(...)50%4475%
GetHashCode()100%11100%
IsEquivalentTo(...)0%440%
IsMatch(...)0%440%
IsNullOrEmpty()100%110%
Lookup(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/UriTemplateLiteralPathSegment.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.Specialized;
 6using System.Text;
 7using CoreWCF.Runtime;
 8
 9namespace CoreWCF
 10{
 11    // thin wrapper around string; use type system to help ensure we
 12    // are doing canonicalization right/consistently
 13    internal class UriTemplateLiteralPathSegment : UriTemplatePathSegment, IComparable<UriTemplateLiteralPathSegment>
 14    {
 15        // segment doesn't store trailing slash
 16        private readonly string _segment;
 117        private static readonly Uri s_dummyUri = new Uri("http://localhost");
 18
 19        private UriTemplateLiteralPathSegment(string segment)
 63020            : base(segment, UriTemplatePartType.Literal, segment.EndsWith("/", StringComparison.Ordinal))
 21        {
 22            Fx.Assert(segment != null, "bad literal segment");
 63023            if (EndsWithSlash)
 24            {
 11925                _segment = segment.Remove(segment.Length - 1);
 26            }
 27            else
 28            {
 51129                _segment = segment;
 30            }
 51131        }
 32
 33        public static new UriTemplateLiteralPathSegment CreateFromUriTemplate(string segment, UriTemplate template)
 34        {
 35            // run it through UriBuilder to escape-if-necessary it
 50036            if (string.Compare(segment, "/", StringComparison.Ordinal) == 0)
 37            {
 38                // running an empty segment through UriBuilder has unexpected/wrong results
 039                return new UriTemplateLiteralPathSegment("/");
 40            }
 41
 50042            if (segment.IndexOf(UriTemplate.WildcardPath, StringComparison.Ordinal) != -1)
 43            {
 044                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(
 045                    SR.Format(SR.UTInvalidWildcardInVariableOrLiteral, template._originalTemplate, UriTemplate.WildcardP
 46            }
 47
 48            // '*' is not usually escaped by the Uri\UriBuilder to %2a, since we forbid passing a
 49            // clear character and the workaroud is to pass the escaped form, we should replace the
 50            // escaped form with the regular one.
 50051            segment = segment.Replace("%2a", "*").Replace("%2A", "*");
 50052            UriBuilder ub = new UriBuilder(s_dummyUri);
 50053            ub.Path = segment;
 50054            string escapedIfNecessarySegment = ub.Uri.AbsolutePath.Substring(1);
 50055            if (escapedIfNecessarySegment == string.Empty)
 56            {
 57                // This path through UriBuilder will sometimes '----' various segments
 58                // such as '../' and './'.  When this happens and the result is an empty
 59                // string, we should just throw and tell the user we don't handle that.
 060                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(segment),
 061                    SR.Format(SR.UTInvalidFormatSegmentOrQueryPart, segment));
 62            }
 63
 50064            return new UriTemplateLiteralPathSegment(escapedIfNecessarySegment);
 65        }
 66
 67        public static UriTemplateLiteralPathSegment CreateFromWireData(string segment)
 68        {
 13069            return new UriTemplateLiteralPathSegment(segment);
 70        }
 71
 72        public string AsUnescapedString()
 73        {
 74            Fx.Assert(_segment != null, "this should only be called by Bind\\Lookup");
 12075            return Uri.UnescapeDataString(_segment);
 76        }
 77
 78        public override void Bind(string[] values, ref int valueIndex, StringBuilder path)
 79        {
 11980            if (EndsWithSlash)
 81            {
 482                path.AppendFormat("{0}/", AsUnescapedString());
 83            }
 84            else
 85            {
 11586                path.Append(AsUnescapedString());
 87            }
 11588        }
 89
 090        public int CompareTo(UriTemplateLiteralPathSegment other) => StringComparer.OrdinalIgnoreCase.Compare(_segment, 
 91
 92        public override bool Equals(object obj)
 93        {
 24894            if (!(obj is UriTemplateLiteralPathSegment lps))
 95            {
 96                Fx.Assert("why would we ever call this?");
 097                return false;
 98            }
 99            else
 100            {
 248101                return ((EndsWithSlash == lps.EndsWithSlash) &&
 248102                    StringComparer.OrdinalIgnoreCase.Equals(_segment, lps._segment));
 103            }
 104        }
 105
 789106        public override int GetHashCode() => StringComparer.OrdinalIgnoreCase.GetHashCode(_segment);
 107
 108        public override bool IsEquivalentTo(UriTemplatePathSegment other, bool ignoreTrailingSlash)
 109        {
 0110            if (other == null)
 111            {
 112                Fx.Assert("why would we ever call this?");
 0113                return false;
 114            }
 115
 0116            if (other.Nature != UriTemplatePartType.Literal)
 117            {
 0118                return false;
 119            }
 120
 0121            UriTemplateLiteralPathSegment otherAsLiteral = other as UriTemplateLiteralPathSegment;
 122            Fx.Assert(otherAsLiteral != null, "The nature requires that this will be OK");
 123
 0124            return IsMatch(otherAsLiteral, ignoreTrailingSlash);
 125        }
 126
 127        public override bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash)
 128        {
 0129            if (!ignoreTrailingSlash && (segment.EndsWithSlash != this.EndsWithSlash))
 130            {
 0131                return false;
 132            }
 133
 0134            return CompareTo(segment) == 0;
 135        }
 136
 0137        public bool IsNullOrEmpty() => string.IsNullOrEmpty(_segment);
 138
 139        public override void Lookup(string segment, NameValueCollection boundParameters)
 140        {
 141            Fx.Assert(StringComparer.OrdinalIgnoreCase.Compare(AsUnescapedString(), segment) == 0,
 142                "How can that be? Lookup is expected to be called after IsMatch");
 36143        }
 144    }
 145}