< Summary

Class:Azure.Messaging.EventHubs.Core.ConnectionStringParser
Assembly:Azure.Messaging.EventHubs
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Core\ConnectionStringParser.cs
Covered lines:48
Uncovered lines:5
Coverable lines:53
Total lines:165
Line coverage:90.5% (48 of 53)
Covered branches:30
Total branches:40
Branch coverage:75% (30 of 40)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
Parse(...)-90.38%75%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Core\ConnectionStringParser.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using Azure.Core;
 6
 7namespace Azure.Messaging.EventHubs.Core
 8{
 9    /// <summary>
 10    ///   Allows for parsing Event Hubs connection strings.
 11    /// </summary>
 12    ///
 13    internal static class ConnectionStringParser
 14    {
 15        /// <summary>The token that identifies the endpoint address for the Event Hubs namespace.</summary>
 16        private const string EndpointToken = "Endpoint";
 17
 18        /// <summary>The token that identifies the name of a specific Event Hub under the namespace.</summary>
 19        private const string EventHubNameToken = "EntityPath";
 20
 21        /// <summary>The token that identifies the name of a shared access key.</summary>
 22        private const string SharedAccessKeyNameToken = "SharedAccessKeyName";
 23
 24        /// <summary>The token that identifies the value of a shared access key.</summary>
 25        private const string SharedAccessKeyValueToken = "SharedAccessKey";
 26
 27        /// <summary>The character used to separate a token and its value in the connection string.</summary>
 28        private const char TokenValueSeparator = '=';
 29
 30        /// <summary>The character used to mark the beginning of a new token/value pair in the connection string.</summa
 31        private const char TokenValuePairDelimiter = ';';
 32
 33        /// <summary>The name of the protocol used by an Event Hubs endpoint. </summary>
 34        private const string EventHubsEndpointSchemeName = "sb";
 35
 36        /// <summary>The formatted protocol used by an Event Hubs endpoint. </summary>
 237        private static readonly string EventHubsEndpointScheme = $"{ EventHubsEndpointSchemeName }{ Uri.SchemeDelimiter 
 38
 39        /// <summary>
 40        ///   Parses the specified Event Hubs connection string into its component properties.
 41        /// </summary>
 42        ///
 43        /// <param name="connectionString">The connection string to parse.</param>
 44        ///
 45        /// <returns>The component properties parsed from the connection string.</returns>
 46        ///
 47        /// <seealso cref="ConnectionStringProperties" />
 48        ///
 49        public static ConnectionStringProperties Parse(string connectionString)
 50        {
 21851            Argument.AssertNotNullOrEmpty(connectionString, nameof(connectionString));
 52
 21853            int tokenPositionModifier = (connectionString[0] == TokenValuePairDelimiter) ? 0 : 1;
 21854            int lastPosition = 0;
 21855            int currentPosition = 0;
 56            int valueStart;
 57
 58            string slice;
 59            string token;
 60            string value;
 61
 21862            var parsedValues =
 21863            (
 21864                EndpointToken: default(UriBuilder),
 21865                EventHubNameToken: default(string),
 21866                SharedAccessKeyNameToken: default(string),
 21867                SharedAccessKeyValueToken: default(string)
 21868            );
 69
 102470            while (currentPosition != -1)
 71            {
 72                // Slice the string into the next token/value pair.
 73
 80674                currentPosition = connectionString.IndexOf(TokenValuePairDelimiter, lastPosition + 1);
 75
 80676                if (currentPosition >= 0)
 77                {
 58878                    slice = connectionString.Substring(lastPosition, (currentPosition - lastPosition));
 79                }
 80                else
 81                {
 21882                    slice = connectionString.Substring(lastPosition);
 83                }
 84
 85                // Break the token and value apart, if this is a legal pair.
 86
 80687                valueStart = slice.IndexOf(TokenValueSeparator);
 88
 80689                if (valueStart >= 0)
 90                {
 79491                    token = slice.Substring((1 - tokenPositionModifier), (valueStart - 1 + tokenPositionModifier));
 79492                    value = slice.Substring(valueStart + 1);
 93
 94                    // Guard against leading and trailing spaces, only trimming if there is a need.
 95
 79496                    if ((!string.IsNullOrEmpty(token)) && (char.IsWhiteSpace(token[0])) || char.IsWhiteSpace(token[token
 97                    {
 098                        token = token.Trim();
 99                    }
 100
 794101                    if ((!string.IsNullOrEmpty(value)) && (char.IsWhiteSpace(value[0]) || char.IsWhiteSpace(value[value.
 102                    {
 0103                        value = value.Trim();
 104                    }
 105
 106                    // If there was no value for a key, then consider the connection string to
 107                    // be malformed.
 108
 794109                    if (string.IsNullOrEmpty(value))
 110                    {
 0111                        throw new FormatException(Resources.InvalidConnectionString);
 112                    }
 113
 114                    // Compare the token against the known connection string properties and capture the
 115                    // pair if they are a known attribute.
 116
 794117                    if (string.Compare(EndpointToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 118                    {
 194119                        parsedValues.EndpointToken = new UriBuilder(value)
 194120                        {
 194121                            Scheme = EventHubsEndpointScheme,
 194122                            Port = -1
 194123                        };
 124
 194125                        if ((string.Compare(parsedValues.EndpointToken.Scheme, EventHubsEndpointSchemeName, StringCompar
 194126                            || (Uri.CheckHostName(parsedValues.EndpointToken.Host) == UriHostNameType.Unknown))
 127                        {
 0128                            throw new FormatException(Resources.InvalidConnectionString);
 129                        }
 130                    }
 600131                    else if (string.Compare(EventHubNameToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 132                    {
 164133                        parsedValues.EventHubNameToken = value;
 134                    }
 436135                    else if (string.Compare(SharedAccessKeyNameToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 136                    {
 210137                        parsedValues.SharedAccessKeyNameToken = value;
 138                    }
 226139                    else if (string.Compare(SharedAccessKeyValueToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 140                    {
 210141                        parsedValues.SharedAccessKeyValueToken = value;
 142                    }
 143                }
 12144                else if ((slice.Length != 1) || (slice[0] != TokenValuePairDelimiter))
 145                {
 146                    // This wasn't a legal pair and it is not simply a trailing delimiter; consider
 147                    // the connection string to be malformed.
 148
 0149                    throw new FormatException(Resources.InvalidConnectionString);
 150                }
 151
 806152                tokenPositionModifier = 0;
 806153                lastPosition = currentPosition;
 154            }
 155
 218156            return new ConnectionStringProperties
 218157            (
 218158                parsedValues.EndpointToken?.Uri,
 218159                parsedValues.EventHubNameToken,
 218160                parsedValues.SharedAccessKeyNameToken,
 218161                parsedValues.SharedAccessKeyValueToken
 218162            );
 163        }
 164    }
 165}

Methods/Properties

.cctor()
Parse(...)