< Summary

Information
Class: AsiBackbone.AspNetCore.Correlation.GovernanceHttpRequestCorrelation
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Correlation/GovernanceHttpRequestCorrelation.cs
Line coverage
100%
Covered lines: 48
Uncovered lines: 0
Coverable lines: 48
Total lines: 170
Line coverage: 100%
Branch coverage
100%
Covered branches: 40
Total branches: 40
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%11100%
get_HasMetadata()100%11100%
ToEvaluationContext(...)100%22100%
NormalizeSuppliedMetadata(...)100%88100%
MergeMetadata(...)100%1414100%
NormalizeOptional(...)100%22100%
NormalizeMetadata(...)100%88100%
AddIfValid(...)100%66100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Correlation/GovernanceHttpRequestCorrelation.cs

#LineLine coverage
 1using System.Collections.ObjectModel;
 2using AsiBackbone.Core.Constraints;
 3
 4namespace AsiBackbone.AspNetCore.Correlation;
 5
 6/// <summary>
 7/// Represents framework-neutral request correlation data resolved from the current ASP.NET Core HTTP request.
 8/// </summary>
 9/// <remarks>
 10/// Initializes a new instance of the <see cref="GovernanceHttpRequestCorrelation" /> class.
 11/// </remarks>
 12/// <param name="correlationId">The resolved correlation identifier, when available.</param>
 13/// <param name="traceId">The resolved trace identifier, when available.</param>
 14/// <param name="metadata">Safe request metadata resolved from the host.</param>
 8915public sealed class GovernanceHttpRequestCorrelation(
 8916    string? correlationId = null,
 8917    string? traceId = null,
 8918    IReadOnlyDictionary<string, string>? metadata = null)
 19{
 120    private static readonly IReadOnlyDictionary<string, string> EmptyMetadata =
 121        new ReadOnlyDictionary<string, string>(
 122            new Dictionary<string, string>(StringComparer.Ordinal));
 23
 24    /// <summary>
 25    /// Gets the request correlation identifier, when supplied by the host or propagated request headers.
 26    /// </summary>
 27    public string? CorrelationId { get; } = NormalizeOptional(correlationId);
 28
 29    /// <summary>
 30    /// Gets the request trace identifier, when supplied by the host or current activity.
 31    /// </summary>
 32    public string? TraceId { get; } = NormalizeOptional(traceId);
 33
 34    /// <summary>
 35    /// Gets safe request metadata supplied by the ASP.NET Core adapter.
 36    /// </summary>
 37    public IReadOnlyDictionary<string, string> Metadata { get; } = NormalizeMetadata(metadata);
 38
 39    /// <summary>
 40    /// Gets a value indicating whether safe request metadata is available.
 41    /// </summary>
 142    public bool HasMetadata => Metadata.Count > 0;
 43
 44    /// <summary>
 45    /// Creates a framework-neutral constraint evaluation context from the resolved request correlation data.
 46    /// </summary>
 47    /// <param name="policyVersion">Optional policy version.</param>
 48    /// <param name="policyHash">Optional policy hash.</param>
 49    /// <param name="metadata">Optional host-provided metadata to merge with safe request metadata.</param>
 50    /// <param name="mergeRequestMetadata">
 51    /// When <see langword="true" /> (the default), <see cref="Metadata" /> is merged underneath
 52    /// <paramref name="metadata" />. Pass <see langword="false" /> when <paramref name="metadata" /> already contains
 53    /// request metadata and is authoritative — for example after sanitization, where re-merging would reintroduce
 54    /// entries the sanitizer removed. <paramref name="metadata" /> is still key-normalized either way.
 55    /// </param>
 56    /// <returns>A framework-neutral constraint evaluation context.</returns>
 57    /// <remarks>
 58    /// Merging layers <paramref name="metadata" /> over <see cref="Metadata" /> by key, so a caller that rewrites a
 59    /// request-derived value overrides it, but a caller that <em>removes</em> one does not: with nothing to override
 60    /// the raw entry, the merge puts it back. Sanitized metadata must therefore be passed with
 61    /// <paramref name="mergeRequestMetadata" /> set to <see langword="false" />.
 62    /// </remarks>
 63    public GovernanceEvaluationContext ToEvaluationContext(
 64        string? policyVersion = null,
 65        string? policyHash = null,
 66        IReadOnlyDictionary<string, string>? metadata = null,
 67        bool mergeRequestMetadata = true)
 68    {
 5369        return new GovernanceEvaluationContext(
 5370            CorrelationId,
 5371            policyVersion,
 5372            policyHash,
 5373            mergeRequestMetadata ? MergeMetadata(metadata) : NormalizeSuppliedMetadata(metadata),
 5374            TraceId);
 75    }
 76
 77    /// <summary>
 78    /// Normalizes supplied metadata keys and values without merging request metadata underneath them.
 79    /// </summary>
 80    /// <param name="metadata">The metadata to normalize.</param>
 81    /// <returns>A normalized metadata dictionary containing only the supplied entries.</returns>
 82    private static IReadOnlyDictionary<string, string> NormalizeSuppliedMetadata(
 83        IReadOnlyDictionary<string, string>? metadata)
 84    {
 5085        if (metadata is null || metadata.Count == 0)
 86        {
 287            return EmptyMetadata;
 88        }
 89
 4890        Dictionary<string, string> normalized = new(StringComparer.Ordinal);
 91
 58892        foreach (KeyValuePair<string, string> item in metadata)
 93        {
 24694            AddIfValid(normalized, item.Key, item.Value);
 95        }
 96
 4897        return normalized.Count == 0
 4898            ? EmptyMetadata
 4899            : new ReadOnlyDictionary<string, string>(normalized);
 100    }
 101
 102    /// <summary>
 103    /// Merges safe request metadata with host-provided metadata.
 104    /// </summary>
 105    /// <param name="metadata">Optional host-provided metadata.</param>
 106    /// <returns>A normalized metadata dictionary.</returns>
 107    public IReadOnlyDictionary<string, string> MergeMetadata(IReadOnlyDictionary<string, string>? metadata)
 108    {
 62109        if ((metadata is null || metadata.Count == 0) && Metadata.Count == 0)
 110        {
 11111            return EmptyMetadata;
 112        }
 113
 51114        Dictionary<string, string> merged = new(StringComparer.Ordinal);
 115
 228116        foreach (KeyValuePair<string, string> item in Metadata)
 117        {
 63118            AddIfValid(merged, item.Key, item.Value);
 119        }
 120
 51121        if (metadata is not null)
 122        {
 494123            foreach (KeyValuePair<string, string> item in metadata)
 124            {
 197125                AddIfValid(merged, item.Key, item.Value);
 126            }
 127        }
 128
 51129        return merged.Count == 0
 51130            ? EmptyMetadata
 51131            : new ReadOnlyDictionary<string, string>(merged);
 132    }
 133
 134    private static string? NormalizeOptional(string? value)
 135    {
 178136        return string.IsNullOrWhiteSpace(value)
 178137            ? null
 178138            : value.Trim();
 139    }
 140
 141    private static IReadOnlyDictionary<string, string> NormalizeMetadata(
 142        IReadOnlyDictionary<string, string>? metadata)
 143    {
 89144        if (metadata is null || metadata.Count == 0)
 145        {
 12146            return EmptyMetadata;
 147        }
 148
 77149        Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal);
 150
 352151        foreach (KeyValuePair<string, string> item in metadata)
 152        {
 99153            AddIfValid(normalizedMetadata, item.Key, item.Value);
 154        }
 155
 77156        return normalizedMetadata.Count == 0
 77157            ? EmptyMetadata
 77158            : new ReadOnlyDictionary<string, string>(normalizedMetadata);
 159    }
 160
 161    private static void AddIfValid(Dictionary<string, string> metadata, string key, string? value)
 162    {
 605163        if (string.IsNullOrWhiteSpace(key))
 164        {
 3165            return;
 166        }
 167
 602168        metadata[key.Trim()] = value?.Trim() ?? string.Empty;
 602169    }
 170}