< Summary

Information
Class: AsiBackbone.AspNetCore.Results.AsiBackboneHttpResultMappingOptions
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Results/AsiBackboneHttpResultMappingOptions.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 103
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Results/AsiBackboneHttpResultMappingOptions.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Http;
 2
 3namespace AsiBackbone.AspNetCore.Results;
 4
 5/// <summary>
 6/// Provides host-overridable defaults for mapping AsiBackbone Core outcomes into HTTP responses.
 7/// </summary>
 8public sealed class AsiBackboneHttpResultMappingOptions
 9{
 10    /// <summary>
 11    /// Gets or sets the status code returned for allowed governance decisions and successful operation results.
 12    /// </summary>
 13513    public int SuccessStatusCode { get; set; } = StatusCodes.Status200OK;
 14
 15    /// <summary>
 16    /// Gets or sets the status code returned for warning governance decisions.
 17    /// </summary>
 12718    public int WarningStatusCode { get; set; } = StatusCodes.Status200OK;
 19
 20    /// <summary>
 21    /// Gets or sets the status code returned for denied governance decisions.
 22    /// </summary>
 13323    public int DeniedStatusCode { get; set; } = StatusCodes.Status403Forbidden;
 24
 25    /// <summary>
 26    /// Gets or sets the status code returned for deferred governance decisions.
 27    /// </summary>
 12128    public int DeferredStatusCode { get; set; } = StatusCodes.Status202Accepted;
 29
 30    /// <summary>
 31    /// Gets or sets the status code returned for acknowledgment-required governance decisions.
 32    /// </summary>
 11933    public int AcknowledgmentRequiredStatusCode { get; set; } = StatusCodes.Status428PreconditionRequired;
 34
 35    /// <summary>
 36    /// Gets or sets the status code returned for escalation-recommended governance decisions.
 37    /// </summary>
 11738    public int EscalationRecommendedStatusCode { get; set; } = StatusCodes.Status409Conflict;
 39
 40    /// <summary>
 41    /// Gets or sets the status code returned for failed operation results.
 42    /// </summary>
 11743    public int OperationFailureStatusCode { get; set; } = StatusCodes.Status400BadRequest;
 44
 45    /// <summary>
 46    /// Gets or sets the safe user-facing message used for non-success governance decisions.
 47    /// </summary>
 6048    public string GovernanceDecisionNotAllowedMessage { get; set; } =
 5949        "The governance decision did not allow immediate execution.";
 50
 51    /// <summary>
 52    /// Gets or sets the safe user-facing message used for failed operation results.
 53    /// </summary>
 11354    public string OperationFailureMessage { get; set; } = "The operation did not complete successfully.";
 55
 56    /// <summary>
 57    /// Gets or sets a value indicating whether reason messages may be exposed in HTTP responses.
 58    /// </summary>
 3259    public bool IncludeReasonMessages { get; set; }
 60
 61    /// <summary>
 62    /// Gets or sets a value indicating whether trace identifiers may be exposed in HTTP responses.
 63    /// </summary>
 2264    public bool IncludeTraceId { get; set; }
 65
 66    /// <summary>
 67    /// Gets or sets a value indicating whether policy version and policy hash values may be exposed in HTTP responses.
 68    /// </summary>
 2269    public bool IncludePolicyMetadata { get; set; }
 70
 71    /// <summary>
 72    /// Validates the configured mapping options.
 73    /// </summary>
 74    /// <exception cref="InvalidOperationException">Thrown when a configured status code or message is invalid.</excepti
 75    public void Validate()
 76    {
 6477        ValidateStatusCode(SuccessStatusCode, nameof(SuccessStatusCode));
 6278        ValidateStatusCode(WarningStatusCode, nameof(WarningStatusCode));
 6079        ValidateStatusCode(DeniedStatusCode, nameof(DeniedStatusCode));
 5680        ValidateStatusCode(DeferredStatusCode, nameof(DeferredStatusCode));
 5481        ValidateStatusCode(AcknowledgmentRequiredStatusCode, nameof(AcknowledgmentRequiredStatusCode));
 5282        ValidateStatusCode(EscalationRecommendedStatusCode, nameof(EscalationRecommendedStatusCode));
 5083        ValidateStatusCode(OperationFailureStatusCode, nameof(OperationFailureStatusCode));
 84
 4885        if (string.IsNullOrWhiteSpace(GovernanceDecisionNotAllowedMessage))
 86        {
 287            throw new InvalidOperationException("A safe governance decision response message must be configured.");
 88        }
 89
 4690        if (string.IsNullOrWhiteSpace(OperationFailureMessage))
 91        {
 292            throw new InvalidOperationException("A safe operation failure response message must be configured.");
 93        }
 4494    }
 95
 96    private static void ValidateStatusCode(int statusCode, string propertyName)
 97    {
 39898        if (statusCode is < 100 or > 599)
 99        {
 16100            throw new InvalidOperationException($"{propertyName} must be a valid HTTP status code.");
 101        }
 382102    }
 103}