< Summary

Line coverage
89%
Covered lines: 25
Uncovered lines: 3
Coverable lines: 28
Total lines: 59
Line coverage: 89.2%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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%
ToString(...)100%11100%
TryParseValue(...)75%8885%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ByteArrayHeaderParser.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.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6
 7namespace System.Net.Http.Headers
 8{
 9    // Don't derive from BaseHeaderParser since parsing the Base64 string is delegated to Convert.FromBase64String()
 10    // which will remove leading, trailing, and whitespace in the middle of the string.
 11    internal sealed class ByteArrayHeaderParser : HttpHeaderParser
 12    {
 113        internal static readonly ByteArrayHeaderParser Parser = new ByteArrayHeaderParser();
 14
 15        private ByteArrayHeaderParser()
 116            : base(false)
 117        {
 118        }
 19
 20        public override string ToString(object value)
 621        {
 622            Debug.Assert(value is byte[]);
 23
 624            return Convert.ToBase64String((byte[])value);
 625        }
 26
 27        public override bool TryParseValue([NotNullWhen(true)] string? value, object? storeValue, ref int index, [NotNul
 1428        {
 1429            parsedValue = null;
 30
 31            // Some headers support empty/null values. This one doesn't.
 1432            if (string.IsNullOrEmpty(value) || (index == value.Length))
 433            {
 434                return false;
 35            }
 36
 1037            string base64String = value;
 1038            if (index > 0)
 039            {
 040                base64String = value.Substring(index);
 041            }
 42
 43            // Try convert the string (we assume it's a valid Base64 string) to byte[].
 44            try
 1045            {
 1046                parsedValue = Convert.FromBase64String(base64String);
 447                index = value.Length;
 448                return true;
 49            }
 650            catch (FormatException e)
 651            {
 652                if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, SR.Format(SR.net_http_parser_invalid_base
 653            }
 54
 655            return false;
 1456        }
 57    }
 58}
 59