< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.AddressHeaderCollection
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/AddressHeaderCollection.cs
Line coverage
23%
Covered lines: 19
Uncovered lines: 62
Coverable lines: 81
Total lines: 219
Line coverage: 23.4%
Branch coverage
13%
Covered branches: 8
Total branches: 58
Branch coverage: 13.7%
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%11100%
.ctor(...)0%10100%
AddHeadersTo(...)50%4460%
FindAll(...)0%10100%
FindHeader(...)0%12120%
IsEquivalent(...)40%101056.25%
PopulateHeaderDictionary(...)25%4428.57%
ReadServiceParameters(...)100%110%
ReadServiceParameters(...)0%440%
WriteContentsTo(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/AddressHeaderCollection.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;
 8
 9namespace CoreWCF.Channels
 10{
 11    public sealed class AddressHeaderCollection : System.Collections.ObjectModel.ReadOnlyCollection<AddressHeader>
 12    {
 313        private static readonly AddressHeaderCollection s_emptyHeaderCollection = new AddressHeaderCollection();
 14
 15        public AddressHeaderCollection()
 67216            : base(new List<AddressHeader>())
 17        {
 67218        }
 19
 20        public AddressHeaderCollection(IEnumerable<AddressHeader> addressHeaders)
 021            : base(new List<AddressHeader>(addressHeaders))
 22        {
 23            // avoid allocating an enumerator when possible
 024            if (addressHeaders is IList<AddressHeader> collection)
 25            {
 026                for (int i = 0; i < collection.Count; i++)
 27                {
 028                    if (collection[i] == null)
 29                    {
 030                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 031                            new ArgumentException(SR.MessageHeaderIsNull0));
 32                    }
 33                }
 34            }
 35            else
 36            {
 037                foreach (AddressHeader addressHeader in addressHeaders)
 38                {
 039                    if (addressHeader == null)
 40                    {
 041                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 042                            new ArgumentException(SR.MessageHeaderIsNull0));
 43                    }
 44                }
 45            }
 046        }
 47
 48        private int InternalCount
 49        {
 50            get
 51            {
 2252                if (this == (object)s_emptyHeaderCollection)
 53                {
 054                    return 0;
 55                }
 56
 2257                return Count;
 58            }
 59        }
 60
 61        public void AddHeadersTo(Message message)
 62        {
 1063            if (message == null)
 64            {
 065                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
 66            }
 67
 2068            for (int i = 0; i < InternalCount; i++)
 69            {
 070                message.Headers.Add(this[i].ToMessageHeader());
 71            }
 1072        }
 73
 74        public AddressHeader[] FindAll(string name, string ns)
 75        {
 076            if (name == null)
 77            {
 078                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 79            }
 80
 081            if (ns == null)
 82            {
 083                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns));
 84            }
 85
 086            List<AddressHeader> results = new List<AddressHeader>();
 087            for (int i = 0; i < Count; i++)
 88            {
 089                AddressHeader header = this[i];
 090                if (header.Name == name && header.Namespace == ns)
 91                {
 092                    results.Add(header);
 93                }
 94            }
 95
 096            return results.ToArray();
 97        }
 98
 99        public AddressHeader FindHeader(string name, string ns)
 100        {
 0101            if (name == null)
 102            {
 0103                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 104            }
 105
 0106            if (ns == null)
 107            {
 0108                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns));
 109            }
 110
 0111            AddressHeader matchingHeader = null;
 112
 0113            for (int i = 0; i < Count; i++)
 114            {
 0115                AddressHeader header = this[i];
 0116                if (header.Name == name && header.Namespace == ns)
 117                {
 0118                    if (matchingHeader != null)
 119                    {
 0120                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Mul
 121                    }
 122
 0123                    matchingHeader = header;
 124                }
 125            }
 126
 0127            return matchingHeader;
 128        }
 129
 130        internal bool IsEquivalent(AddressHeaderCollection col)
 131        {
 3132            if (InternalCount != col.InternalCount)
 133            {
 0134                return false;
 135            }
 136
 3137            StringBuilder builder = new StringBuilder();
 3138            Dictionary<string, int> myHeaders = new Dictionary<string, int>();
 3139            PopulateHeaderDictionary(builder, myHeaders);
 140
 3141            Dictionary<string, int> otherHeaders = new Dictionary<string, int>();
 3142            col.PopulateHeaderDictionary(builder, otherHeaders);
 143
 3144            if (myHeaders.Count != otherHeaders.Count)
 145            {
 0146                return false;
 147            }
 148
 6149            foreach (KeyValuePair<string, int> pair in myHeaders)
 150            {
 0151                if (otherHeaders.TryGetValue(pair.Key, out int count))
 152                {
 0153                    if (count != pair.Value)
 154                    {
 0155                        return false;
 156                    }
 157                }
 158                else
 159                {
 0160                    return false;
 161                }
 162            }
 163
 3164            return true;
 0165        }
 166
 167        internal void PopulateHeaderDictionary(StringBuilder builder, Dictionary<string, int> headers)
 168        {
 169            string key;
 12170            for (int i = 0; i < InternalCount; ++i)
 171            {
 0172                builder.Remove(0, builder.Length);
 0173                key = this[i].GetComparableForm(builder);
 0174                if (headers.ContainsKey(key))
 175                {
 0176                    headers[key] = headers[key] + 1;
 177                }
 178                else
 179                {
 0180                    headers.Add(key, 1);
 181                }
 182            }
 6183        }
 184
 185        internal static AddressHeaderCollection ReadServiceParameters(XmlDictionaryReader reader)
 186        {
 0187            return ReadServiceParameters(reader, false);
 188        }
 189
 190        internal static AddressHeaderCollection ReadServiceParameters(XmlDictionaryReader reader, bool isReferenceProper
 191        {
 0192            reader.MoveToContent();
 0193            if (reader.IsEmptyElement)
 194            {
 0195                reader.Skip();
 0196                return null;
 197            }
 198            else
 199            {
 0200                reader.ReadStartElement();
 0201                List<AddressHeader> headerList = new List<AddressHeader>();
 0202                while (reader.IsStartElement())
 203                {
 0204                    headerList.Add(new BufferedAddressHeader(reader, isReferenceProperty));
 205                }
 0206                reader.ReadEndElement();
 0207                return new AddressHeaderCollection(headerList);
 208            }
 209        }
 210
 211        internal void WriteContentsTo(XmlDictionaryWriter writer)
 212        {
 0213            for (int i = 0; i < InternalCount; i++)
 214            {
 0215                this[i].WriteAddressHeader(writer);
 216            }
 0217        }
 218    }
 219}