< 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(...)100%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
 13828    {
 13829        if (string.IsNullOrWhiteSpace(connectionString))
 230        {
 231            throw new ArgumentNullException(nameof(connectionString));
 32        }
 33
 13634        if (connectionString.StartsWith(AlternateDnsPrefix, StringComparison.InvariantCultureIgnoreCase))
 135        {
 136            connectionString = connectionString.Substring(AlternateDnsPrefix.Length);
 137            connectionString = DnsPrefix + connectionString;
 138        }
 39
 13640        if (connectionString.StartsWith(DnsPrefix, StringComparison.InvariantCultureIgnoreCase))
 341        {
 342            return (new[] { new Uri(connectionString) }, true);
 43        }
 44
 13345        string[] hosts = connectionString.Split(',');
 13346        List<Uri> nodes = new();
 47
 67348        foreach (string host in hosts)
 13749        {
 13750            string processedHost = host.Trim();
 51
 52            // Only append port if no port is specified and it's not a full URL
 13753            if (!processedHost.Contains(':') &&
 13754                !processedHost.StartsWith(InsecurePrefix, StringComparison.InvariantCultureIgnoreCase) &&
 13755                !processedHost.StartsWith(SecurePrefix, StringComparison.InvariantCultureIgnoreCase))
 956            {
 957                processedHost += $":{Convert.ToString(port, CultureInfo.InvariantCulture)}";
 958            }
 59
 13760            if (!(processedHost.StartsWith(InsecurePrefix, StringComparison.InvariantCultureIgnoreCase) ||
 13761                  processedHost.StartsWith(SecurePrefix, StringComparison.InvariantCultureIgnoreCase)))
 8862            {
 8863                processedHost = credentials == ChannelCredentials.Insecure
 8864                    ? $"{InsecurePrefix}{processedHost}"
 8865                    : $"{SecurePrefix}{processedHost}";
 8866            }
 67
 13768            nodes.Add(new Uri(processedHost));
 13769        }
 70
 13371        return (nodes.ToArray(), false);
 13672    }
 73}