< 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
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
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
 2228    {
 2229        if (string.IsNullOrWhiteSpace(connectionString))
 230        {
 231            throw new ArgumentNullException(nameof(connectionString));
 32        }
 33
 2034        if (connectionString.StartsWith(AlternateDnsPrefix, StringComparison.InvariantCultureIgnoreCase))
 135        {
 136            connectionString = connectionString.Substring(AlternateDnsPrefix.Length);
 137            connectionString = DnsPrefix + connectionString;
 138        }
 39
 2040        if (connectionString.StartsWith(DnsPrefix, StringComparison.InvariantCultureIgnoreCase))
 341        {
 342            return (new[] { new Uri(connectionString) }, true);
 43        }
 44
 1745        string[] hosts = connectionString.Split(',');
 1746        List<Uri> nodes = new();
 47
 9148        foreach (string host in hosts)
 2049        {
 2050            string processedHost = host.Trim();
 51
 52            // Only append port if no port is specified and it's not a full URL
 2053            if (!processedHost.Contains(':') &&
 2054                !processedHost.StartsWith(InsecurePrefix, StringComparison.InvariantCultureIgnoreCase) &&
 2055                !processedHost.StartsWith(SecurePrefix, StringComparison.InvariantCultureIgnoreCase))
 956            {
 957                processedHost += $":{Convert.ToString(port, CultureInfo.InvariantCulture)}";
 958            }
 59
 2060            if (!(processedHost.StartsWith(InsecurePrefix, StringComparison.InvariantCultureIgnoreCase) ||
 2061                  processedHost.StartsWith(SecurePrefix, StringComparison.InvariantCultureIgnoreCase)))
 1862            {
 1863                processedHost = credentials == ChannelCredentials.Insecure
 1864                    ? $"{InsecurePrefix}{processedHost}"
 1865                    : $"{SecurePrefix}{processedHost}";
 1866            }
 67
 2068            nodes.Add(new Uri(processedHost));
 2069        }
 70
 1771        return (nodes.ToArray(), false);
 2072    }
 73}