< 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
55%
Covered branches: 11
Total branches: 20
Branch coverage: 55%
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 =>
 123621            _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
 475033        internal NameValueWithParametersHeaderValue()
 475034        {
 475035        }
 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()
 453067        {
 453068            string baseString = base.ToString();
 453069            StringBuilder sb = StringBuilderCache.Acquire();
 453070            sb.Append(baseString);
 453071            NameValueHeaderValue.ToString(_parameters, ';', true, sb);
 453072            return StringBuilderCache.GetStringAndRelease(sb);
 453073        }
 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)
 483897        {
 483898            Debug.Assert(input != null);
 483899            Debug.Assert(startIndex >= 0);
 100
 4838101            parsedValue = null;
 102
 4838103            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
 0104            {
 0105                return 0;
 106            }
 107
 4838108            int nameValueLength = NameValueHeaderValue.GetNameValueLength(input, startIndex,
 4838109                s_nameValueCreator, out NameValueHeaderValue? nameValue);
 110
 4838111            if (nameValueLength == 0)
 88112            {
 88113                return 0;
 114            }
 115
 4750116            int current = startIndex + nameValueLength;
 4750117            current += HttpRuleParser.GetWhitespaceLength(input, current);
 4750118            NameValueWithParametersHeaderValue? nameValueWithParameters =
 4750119                nameValue as NameValueWithParametersHeaderValue;
 4750120            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".
 4750124            if ((current < input.Length) && (input[current] == ';'))
 1236125            {
 1236126                current++; // skip delimiter.
 1236127                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
 1236128                    (UnvalidatedObjectCollection<NameValueHeaderValue>)nameValueWithParameters.Parameters);
 129
 1236130                if (parameterLength == 0)
 58131                {
 58132                    return 0;
 133                }
 134
 1178135                parsedValue = nameValueWithParameters;
 1178136                return current + parameterLength - startIndex;
 137            }
 138
 139            // We have a name/value pair without parameters.
 3514140            parsedValue = nameValueWithParameters;
 3514141            return current - startIndex;
 4838142        }
 143
 144        private static NameValueHeaderValue CreateNameValue()
 4750145        {
 4750146            return new NameValueWithParametersHeaderValue();
 4750147        }
 148
 149        object ICloneable.Clone()
 0150        {
 0151            return new NameValueWithParametersHeaderValue(this);
 0152        }
 153    }
 154}