< Summary

Information
Class: dotnet_etcd.ConnectionStringParser
Assembly: dotnet-etcd
File(s): /home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/ConnectionStringParser.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ParseConnectionString(...)95%2020100%

File(s)

/home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/ConnectionStringParser.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Linq;
 5using Grpc.Core;
 6
 7namespace dotnet_etcd;
 8
 9/// <summary>
 10///     Handles parsing of connection strings for etcd client
 11/// </summary>
 12public class ConnectionStringParser
 13{
 14    private const string InsecurePrefix = "http://";
 15    private const string SecurePrefix = "https://";
 16    private const string StaticHostsPrefix = "static://";
 17    private const string DnsPrefix = "dns://";
 18    private const string AlternateDnsPrefix = "discovery-srv://";
 19
 20    /// <summary>
 21    ///     Parses a connection string and returns the appropriate URI and connection type
 22    /// </summary>
 23    /// <param name="connectionString">The connection string to parse</param>
 24    /// <param name="port">The port to use if not specified in the connection string</param>
 25    /// <param name="credentials">The credentials to use for the connection</param>
 26    /// <returns>A tuple containing the parsed URI and whether it's a DNS connection</returns>
 27    public (Uri[] Uris, bool IsDnsConnection) ParseConnectionString(string connectionString, int port, ChannelCredential
 11728    {
 11729        if (string.IsNullOrWhiteSpace(connectionString))
 230        {
 231            throw new ArgumentNullException(nameof(connectionString));
 32        }
 33
 11534        if (connectionString.StartsWith(AlternateDnsPrefix, StringComparison.InvariantCultureIgnoreCase))
 135        {
 136            connectionString = connectionString.Substring(AlternateDnsPrefix.Length);
 137            connectionString = DnsPrefix + connectionString;
 138        }
 39
 11540        if (connectionString.StartsWith(DnsPrefix, StringComparison.InvariantCultureIgnoreCase))
 341        {
 342            return (new[] { new Uri(connectionString) }, true);
 43        }
 44
 11245        string[] hosts = connectionString.Split(',');
 11246        List<Uri> nodes = new();
 47
 56848        foreach (string host in hosts)
 11649        {
 11650            string processedHost = host.Trim();
 51
 52            // Only append port if no port is specified and it's not a full URL
 11653            if (!processedHost.Contains(':') &&
 11654                !processedHost.StartsWith(InsecurePrefix, StringComparison.InvariantCultureIgnoreCase) &&
 11655                !processedHost.StartsWith(SecurePrefix, StringComparison.InvariantCultureIgnoreCase))
 956            {
 957                processedHost += $":{Convert.ToString(port, CultureInfo.InvariantCulture)}";
 958            }
 59
 11660            if (!(processedHost.StartsWith(InsecurePrefix, StringComparison.InvariantCultureIgnoreCase) ||
 11661                  processedHost.StartsWith(SecurePrefix, StringComparison.InvariantCultureIgnoreCase)))
 8462            {
 8463                processedHost = credentials == ChannelCredentials.Insecure
 8464                    ? $"{InsecurePrefix}{processedHost}"
 8465                    : $"{SecurePrefix}{processedHost}";
 8466            }
 67
 11668            nodes.Add(new Uri(processedHost));
 11669        }
 70
 11271        return (nodes.ToArray(), false);
 11572    }
 73}