< Summary

Class:Azure.Messaging.ServiceBus.Core.ConnectionStringParser
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Core\ConnectionStringParser.cs
Covered lines:46
Uncovered lines:3
Coverable lines:49
Total lines:156
Line coverage:93.8% (46 of 49)
Covered branches:30
Total branches:36
Branch coverage:83.3% (30 of 36)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Parse(...)-93.88%83.33%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\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.ServiceBus.Core
 8{
 9    /// <summary>
 10    ///   Allows for parsing Service Bus connection strings.
 11    /// </summary>
 12    ///
 13    internal static class ConnectionStringParser
 14    {
 15        /// <summary>The character used to separate a token and its value in the connection string.</summary>
 16        private const char TokenValueSeparator = '=';
 17
 18        /// <summary>The character used to mark the beginning of a new token/value pair in the connection string.</summa
 19        private const char TokenValuePairDelimiter = ';';
 20
 21        /// <summary>The formatted protocol used by an Service Bus endpoint. </summary>
 22        private const string ServiceBusEndpointScheme = "sb://";
 23
 24        /// <summary>The token that identifies the endpoint address for the Service Bus namespace.</summary>
 25        private const string EndpointToken = "Endpoint";
 26
 27        /// <summary>The token that identifies the name of a specific Service Bus entity under the namespace.</summary>
 28        private const string EntityName = "EntityPath";
 29
 30        /// <summary>The token that identifies the name of a shared access key.</summary>
 31        private const string SharedAccessKeyNameToken = "SharedAccessKeyName";
 32
 33        /// <summary>The token that identifies the value of a shared access key.</summary>
 34        private const string SharedAccessKeyValueToken = "SharedAccessKey";
 35
 36        /// <summary>
 37        ///   Parses the specified Service Bus connection string into its component properties.
 38        /// </summary>
 39        ///
 40        /// <param name="connectionString">The connection string to parse.</param>
 41        ///
 42        /// <returns>The component properties parsed from the connection string.</returns>
 43        ///
 44        /// <seealso cref="ConnectionStringProperties" />
 45        ///
 46        public static ConnectionStringProperties Parse(string connectionString)
 47        {
 10848            Argument.AssertNotNullOrEmpty(connectionString, nameof(connectionString));
 49
 10850            int tokenPositionModifier = (connectionString[0] == TokenValuePairDelimiter) ? 0 : 1;
 10851            int lastPosition = 0;
 10852            int currentPosition = 0;
 53            int valueStart;
 54
 55            string slice;
 56            string token;
 57            string value;
 58
 10859            var parsedValues =
 10860            (
 10861                EndpointToken: default(UriBuilder),
 10862                EntityNameToken: default(string),
 10863                SharedAccessKeyNameToken: default(string),
 10864                SharedAccessKeyValueToken: default(string)
 10865            );
 66
 45867            while (currentPosition != -1)
 68            {
 69                // Slice the string into the next token/value pair.
 70
 35271                currentPosition = connectionString.IndexOf(TokenValuePairDelimiter, lastPosition + 1);
 72
 35273                if (currentPosition >= 0)
 74                {
 24675                    slice = connectionString.Substring(lastPosition, (currentPosition - lastPosition));
 76                }
 77                else
 78                {
 10679                    slice = connectionString.Substring(lastPosition);
 80                }
 81
 82                // Break the token and value apart, if this is a legal pair.
 83
 35284                valueStart = slice.IndexOf(TokenValueSeparator);
 85
 35286                if (valueStart >= 0)
 87                {
 34288                    token = slice.Substring((1 - tokenPositionModifier), (valueStart - 1 + tokenPositionModifier));
 34289                    value = slice.Substring(valueStart + 1);
 90
 91                    // Guard against leading and trailing spaces, only trimming if there is a need.
 92
 34293                    if ((!string.IsNullOrEmpty(token)) && (char.IsWhiteSpace(token[0])) || char.IsWhiteSpace(token[token
 94                    {
 095                        token = token.Trim();
 96                    }
 97
 34298                    if ((!string.IsNullOrEmpty(value)) && (char.IsWhiteSpace(value[0]) || char.IsWhiteSpace(value[value.
 99                    {
 12100                        value = value.Trim();
 101                    }
 102
 103                    // If there was no value for a key, then consider the connection string to
 104                    // be malformed.
 105
 342106                    if (string.IsNullOrEmpty(value))
 107                    {
 0108                        throw new FormatException(Resources.InvalidConnectionString);
 109                    }
 110
 111                    // Compare the token against the known connection string properties and capture the
 112                    // pair if they are a known attribute.
 113
 342114                    if (string.Compare(EndpointToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 115                    {
 106116                        parsedValues.EndpointToken = new UriBuilder(value)
 106117                        {
 106118                            Scheme = ServiceBusEndpointScheme,
 106119                            Port = -1
 106120                        };
 121                    }
 236122                    else if (string.Compare(EntityName, token, StringComparison.OrdinalIgnoreCase) == 0)
 123                    {
 28124                        parsedValues.EntityNameToken = value;
 125                    }
 208126                    else if (string.Compare(SharedAccessKeyNameToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 127                    {
 102128                        parsedValues.SharedAccessKeyNameToken = value;
 129                    }
 106130                    else if (string.Compare(SharedAccessKeyValueToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 131                    {
 102132                        parsedValues.SharedAccessKeyValueToken = value;
 133                    }
 134                }
 10135                else if ((slice.Length != 1) || (slice[0] != TokenValuePairDelimiter))
 136                {
 137                    // This wasn't a legal pair and it is not simply a trailing delimiter; consider
 138                    // the connection string to be malformed.
 139
 0140                    throw new FormatException(Resources.InvalidConnectionString);
 141                }
 142
 350143                tokenPositionModifier = 0;
 350144                lastPosition = currentPosition;
 145            }
 146
 106147            return new ConnectionStringProperties
 106148            (
 106149                parsedValues.EndpointToken?.Uri,
 106150                parsedValues.EntityNameToken,
 106151                parsedValues.SharedAccessKeyNameToken,
 106152                parsedValues.SharedAccessKeyValueToken
 106153            );
 154        }
 155    }
 156}

Methods/Properties

Parse(...)