< Summary

Information
Class: ClutterStock.Infrastructure.PostgresUrlParser
Assembly: Infrastructure
File(s): /home/runner/work/ClutterStock/ClutterStock/backend/src/Infrastructure/PostgresUrlParser.cs
Tag: 58_25416222083
Line coverage
12%
Covered lines: 3
Uncovered lines: 22
Coverable lines: 25
Total lines: 61
Line coverage: 12%
Branch coverage
10%
Covered branches: 2
Total branches: 20
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Parse(...)10%2932012%

File(s)

/home/runner/work/ClutterStock/ClutterStock/backend/src/Infrastructure/PostgresUrlParser.cs

#LineLine coverage
 1using System.Collections.Specialized;
 2using System.Web;
 3using Npgsql;
 4
 5namespace ClutterStock.Infrastructure;
 6
 7/// <summary>
 8///     Converts a PostgreSQL URI (<c>postgres[ql]://[user[:password]@]host[:port][/database][?options]</c>)
 9///     to an ADO.NET connection string accepted by Npgsql. Falls back to the input value when it is
 10///     already in native key=value form.
 11/// </summary>
 12public static class PostgresUrlParser
 13{
 14    /// <summary>
 15    ///     Parses <paramref name="value" /> into an Npgsql ADO.NET connection string.
 16    /// </summary>
 17    public static string Parse(string value)
 18    {
 319        if (!value.StartsWith("postgres://", StringComparison.OrdinalIgnoreCase) &&
 320            !value.StartsWith("postgresql://", StringComparison.OrdinalIgnoreCase))
 321            return value;
 22
 023        var uri = new Uri(value);
 024        var builder = new NpgsqlConnectionStringBuilder
 025        {
 026            Host = uri.Host,
 027            Port = uri.IsDefaultPort ? 5432 : uri.Port,
 028        };
 29
 030        var database = uri.AbsolutePath.TrimStart('/');
 031        if (!string.IsNullOrEmpty(database))
 032            builder.Database = Uri.UnescapeDataString(database);
 33
 034        if (!string.IsNullOrEmpty(uri.UserInfo))
 35        {
 036            var colon = uri.UserInfo.IndexOf(':');
 037            if (colon >= 0)
 38            {
 039                builder.Username = Uri.UnescapeDataString(uri.UserInfo[..colon]);
 040                builder.Password = Uri.UnescapeDataString(uri.UserInfo[(colon + 1)..]);
 41            }
 42            else
 43            {
 044                builder.Username = Uri.UnescapeDataString(uri.UserInfo);
 45            }
 46        }
 47
 048        if (!string.IsNullOrEmpty(uri.Query))
 49        {
 050            NameValueCollection query = HttpUtility.ParseQueryString(uri.Query);
 051            foreach (var key in query.AllKeys)
 52            {
 053                if (string.IsNullOrEmpty(key)) continue;
 054                var v = query[key];
 055                if (v is not null) builder[key] = v;
 56            }
 57        }
 58
 059        return builder.ConnectionString;
 60    }
 61}

Methods/Properties

Parse(System.String)