< Summary - AsyncResponse (Release / net8.0+net10.0 / unit+integration)

Information
Class: AsyncResponse.Transports.RabbitMQ.RabbitMqCorrelationIdExtractor
Assembly: AsyncResponse.Transports.RabbitMQ
File(s): /_/src/Transports/AsyncResponse.Transports.RabbitMQ/RabbitMqCorrelationIdExtractor.cs
Line coverage
95%
Covered lines: 20
Uncovered lines: 1
Coverable lines: 21
Total lines: 47
Line coverage: 95.2%
Branch coverage
95%
Covered branches: 21
Total branches: 22
Branch coverage: 95.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Extract(...)100%1212100%
TryConvertHeader(...)90%101091.66%

File(s)

/_/src/Transports/AsyncResponse.Transports.RabbitMQ/RabbitMqCorrelationIdExtractor.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text;
 3
 4namespace AsyncResponse.Transports.RabbitMQ;
 5
 6/// <summary>
 7/// Extracts the AsyncResponse correlation id from an inbound response message: first from the
 8/// broker's own CorrelationId, then the configured header, then from the JSON body via the
 9/// configured paths (walked by the shared <see cref="CorrelationIdJsonPaths"/>).
 10/// </summary>
 11internal static class RabbitMqCorrelationIdExtractor
 12{
 13    /// <summary>Extracts the correlation id from the supplied message.</summary>
 14    public static string? Extract(
 15        RabbitMqDelivery delivery,
 16        string messageJson,
 17        RabbitMqAsyncResponseOptions options)
 18    {
 4219        if (!string.IsNullOrWhiteSpace(delivery.BasicProperties.CorrelationId))
 220            return delivery.BasicProperties.CorrelationId;
 21
 4022        if (!string.IsNullOrWhiteSpace(options.CorrelationIdHeader)
 4023            && delivery.BasicProperties.Headers is not null
 4024            && delivery.BasicProperties.Headers.TryGetValue(options.CorrelationIdHeader, out var header)
 4025            && TryConvertHeader(header) is { } headerValue
 4026            && !string.IsNullOrWhiteSpace(headerValue))
 27        {
 1428            return headerValue;
 29        }
 30
 2631        return CorrelationIdJsonPaths.Extract(messageJson, options.CorrelationIdJsonPaths);
 32    }
 33
 34    private static string? TryConvertHeader(object? header)
 1635        => header switch
 1636        {
 237            null => null,
 238            string s => s,
 239            byte[] bytes => Encoding.UTF8.GetString(bytes),
 240            ReadOnlyMemory<byte> memory => Encoding.UTF8.GetString(memory.Span),
 1641            // AMQP headers legally carry numeric/timestamp values, and the id must render the same
 1642            // on every consumer: a locale-formatted "1,5" here never matches the "1.5" the waiter
 1643            // registered under, so the wait runs to timeout on hosts with another CurrentCulture.
 844            IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
 045            _ => header.ToString()
 1646        };
 47}