< Summary

Information
Class: System.Net.Http.Headers.NameValueWithParametersHeaderValue
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\NameValueWithParametersHeaderValue.cs
Line coverage
49%
Covered lines: 43
Uncovered lines: 44
Coverable lines: 87
Total lines: 154
Line coverage: 49.4%
Branch coverage
60%
Covered branches: 12
Total branches: 20
Branch coverage: 60%
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%110%
.ctor(...)100%110%
.ctor()100%11100%
.ctor(...)100%110%
Equals(...)0%440%
GetHashCode()100%110%
ToString()100%11100%
Parse(...)100%110%
TryParse(...)0%220%
GetNameValueWithParametersLength(...)83.33%121293.33%
CreateNameValue()100%11100%
System.ICloneable.Clone()100%110%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\NameValueWithParametersHeaderValue.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.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.IO;
 8using System.Text;
 9
 10namespace System.Net.Http.Headers
 11{
 12    // According to the RFC, in places where a "parameter" is required, the value is mandatory
 13    // (e.g. Media-Type, Accept). However, we don't introduce a dedicated type for this.
 14    public class NameValueWithParametersHeaderValue : NameValueHeaderValue, ICloneable
 15    {
 116        private static readonly Func<NameValueHeaderValue> s_nameValueCreator = CreateNameValue;
 17
 18        private UnvalidatedObjectCollection<NameValueHeaderValue>? _parameters;
 19
 20        public ICollection<NameValueHeaderValue> Parameters =>
 2888721            _parameters ??= new UnvalidatedObjectCollection<NameValueHeaderValue>();
 22
 23        public NameValueWithParametersHeaderValue(string name)
 024            : base(name)
 025        {
 026        }
 27
 28        public NameValueWithParametersHeaderValue(string name, string? value)
 029            : base(name, value)
 030        {
 031        }
 32
 16359333        internal NameValueWithParametersHeaderValue()
 16359334        {
 16359335        }
 36
 37        protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
 038            : base(source)
 039        {
 040            _parameters = source._parameters.Clone();
 041        }
 42
 43        public override bool Equals([NotNullWhen(true)] object? obj)
 044        {
 045            bool result = base.Equals(obj);
 46
 047            if (result)
 048            {
 049                NameValueWithParametersHeaderValue? other = obj as NameValueWithParametersHeaderValue;
 50
 051                if (other == null)
 052                {
 053                    return false;
 54                }
 055                return HeaderUtilities.AreEqualCollections(_parameters, other._parameters);
 56            }
 57
 058            return false;
 059        }
 60
 61        public override int GetHashCode()
 062        {
 063            return base.GetHashCode() ^ NameValueHeaderValue.GetHashCode(_parameters);
 064        }
 65
 66        public override string ToString()
 27209567        {
 27209568            string baseString = base.ToString();
 27209569            StringBuilder sb = StringBuilderCache.Acquire();
 27209570            sb.Append(baseString);
 27209571            NameValueHeaderValue.ToString(_parameters, ';', true, sb);
 27209572            return StringBuilderCache.GetStringAndRelease(sb);
 27209573        }
 74
 75        public static new NameValueWithParametersHeaderValue Parse(string input)
 076        {
 077            int index = 0;
 078            return (NameValueWithParametersHeaderValue)GenericHeaderParser.SingleValueNameValueWithParametersParser
 079                .ParseValue(input, null, ref index);
 080        }
 81
 82        public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out NameValueWithParametersHe
 083        {
 084            int index = 0;
 085            parsedValue = null;
 86
 087            if (GenericHeaderParser.SingleValueNameValueWithParametersParser.TryParseValue(input,
 088                null, ref index, out object? output))
 089            {
 090                parsedValue = (NameValueWithParametersHeaderValue)output!;
 091                return true;
 92            }
 093            return false;
 094        }
 95
 96        internal static int GetNameValueWithParametersLength(string? input, int startIndex, out object? parsedValue)
 16369797        {
 16369798            Debug.Assert(input != null);
 16369799            Debug.Assert(startIndex >= 0);
 100
 163697101            parsedValue = null;
 102
 163697103            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
 0104            {
 0105                return 0;
 106            }
 107
 163697108            int nameValueLength = NameValueHeaderValue.GetNameValueLength(input, startIndex,
 163697109                s_nameValueCreator, out NameValueHeaderValue? nameValue);
 110
 163697111            if (nameValueLength == 0)
 104112            {
 104113                return 0;
 114            }
 115
 163593116            int current = startIndex + nameValueLength;
 163593117            current += HttpRuleParser.GetWhitespaceLength(input, current);
 163593118            NameValueWithParametersHeaderValue? nameValueWithParameters =
 163593119                nameValue as NameValueWithParametersHeaderValue;
 163593120            Debug.Assert(nameValueWithParameters != null);
 121
 122            // So far we have a valid name/value pair. Check if we have also parameters for the name/value pair. If
 123            // yes, parse parameters. E.g. something like "name=value; param1=value1; param2=value2".
 163593124            if ((current < input.Length) && (input[current] == ';'))
 28887125            {
 28887126                current++; // skip delimiter.
 28887127                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
 28887128                    (UnvalidatedObjectCollection<NameValueHeaderValue>)nameValueWithParameters.Parameters);
 129
 28887130                if (parameterLength == 0)
 26131                {
 26132                    return 0;
 133                }
 134
 28861135                parsedValue = nameValueWithParameters;
 28861136                return current + parameterLength - startIndex;
 137            }
 138
 139            // We have a name/value pair without parameters.
 134706140            parsedValue = nameValueWithParameters;
 134706141            return current - startIndex;
 163697142        }
 143
 144        private static NameValueHeaderValue CreateNameValue()
 163593145        {
 163593146            return new NameValueWithParametersHeaderValue();
 163593147        }
 148
 149        object ICloneable.Clone()
 0150        {
 0151            return new NameValueWithParametersHeaderValue(this);
 0152        }
 153    }
 154}