< Summary

Information
Class: ProjectTemplate.Web.ErrorHandling.ProblemDetailsRequestClassifier
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/ErrorHandling/ProblemDetailsRequestClassifier.cs
Line coverage
81%
Covered lines: 9
Uncovered lines: 2
Coverable lines: 11
Total lines: 46
Line coverage: 81.8%
Branch coverage
57%
Covered branches: 8
Total branches: 14
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ShouldWriteProblemDetails(...)57.14%151481.81%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/ErrorHandling/ProblemDetailsRequestClassifier.cs

#LineLine coverage
 1using Microsoft.Extensions.Primitives;
 2using Microsoft.Net.Http.Headers;
 3
 4namespace ProjectTemplate.Web.ErrorHandling;
 5
 6/// <summary>
 7/// Provides methods for determining whether a request should be classified for Problem Details response formatting.
 8/// </summary>
 9/// <remarks>This class contains logic to identify requests that are likely to expect Problem Details responses,
 10/// such as API requests or AJAX calls. It is intended for internal use within the application pipeline.</remarks>
 11internal static class ProblemDetailsRequestClassifier
 12{
 13    /// <summary>
 14    /// Determines whether a Problem Details response should be written for the specified HTTP context.
 15    /// </summary>
 16    /// <remarks>A Problem Details response is written if the request targets an API endpoint (path starts
 17    /// with '/api'), is an XMLHttpRequest (AJAX), or explicitly accepts a JSON response. HEAD requests are
 18    /// excluded.</remarks>
 19    /// <param name="httpContext">The HTTP context for the current request. Cannot be null.</param>
 20    /// <returns>true if a Problem Details response should be written for the request; otherwise, false.</returns>
 21    public static bool ShouldWriteProblemDetails(HttpContext httpContext)
 22    {
 22423        ArgumentNullException.ThrowIfNull(httpContext);
 24
 22425        if (HttpMethods.IsHead(httpContext.Request.Method))
 26        {
 027            return false;
 28        }
 29
 22430        if (httpContext.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase))
 31        {
 3832            return true;
 33        }
 34
 18635        if (httpContext.Request.Headers.TryGetValue(HeaderNames.XRequestedWith, out StringValues requestedWith) &&
 18636            string.Equals(requestedWith, "XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
 37        {
 038            return true;
 39        }
 40
 18641        IList<MediaTypeHeaderValue>? acceptHeaders = httpContext.Request.GetTypedHeaders().Accept;
 42
 18643        return acceptHeaders is not null && acceptHeaders.Count != 0 && acceptHeaders.Any(header =>
 18644            header.MediaType.Value?.Contains("json", StringComparison.OrdinalIgnoreCase) == true);
 45    }
 46}