Skip to content

Add RespondsWithProblemDetails#128

Merged
dennisdoomen merged 1 commit into
mainfrom
dennisdoomen/add-responds-with-problem-details
May 30, 2026
Merged

Add RespondsWithProblemDetails#128
dennisdoomen merged 1 commit into
mainfrom
dennisdoomen/add-responds-with-problem-details

Conversation

@dennisdoomen

Copy link
Copy Markdown
Owner

Closes #122

Summary

Adds a first-class responder that returns an RFC 7807 application/problem+json payload, the standard error format for ASP.NET Core and many modern APIs. This lets users write concise negative-path tests without hand-building the JSON.

The ProblemDetails shape is emitted directly through the existing JSON serialization path (reusing the configurable JsonSerializerOptions), so no Microsoft.AspNetCore.* dependency is introduced. The response Content-Type is set to application/problem+json. When not supplied, title defaults to the status code's reason phrase and status is derived from the status code. type, detail, and instance are only emitted when provided, and any extensions are serialized as additional top-level members.

New API

RequestMockResponseBuilder RespondsWithProblemDetails(
    HttpStatusCode statusCode,
    string? title = null,
    string? detail = null,
    string? type = null,
    string? instance = null,
    IDictionary<string, object?>? extensions = null);

Usage:

mock.ForGet().WithPath("/api/users/999")
    .RespondsWithProblemDetails(HttpStatusCode.NotFound, title: "User not found");

Tests

Added a WhenRespondingWithProblemDetails spec group covering the application/problem+json content-type, the status/title defaults, supplied title/detail/type/instance, serialized extension members, and interaction with Times(n). Full suite is green on both net8.0 and net472.

Notes

  • Additive and backward-compatible; no consumer breaking changes.
  • Public API surface updated via AcceptApiChanges.ps1; the regenerated *.verified.txt files are included.
  • Two CSharpGuidelines rules (AV1561 parameter count, AV1553 null-default optional parameters) are suppressed locally with justification, since the method intentionally mirrors the RFC 7807 members for an ergonomic single-call API.

@dennisdoomen dennisdoomen added the enhancement New feature or request label May 30, 2026
mock.AllMocksInvoked.Should().BeTrue();
}
}
#nullable restore
@github-actions

github-actions Bot commented May 30, 2026

Copy link
Copy Markdown

Test Results

  3 files  ± 0    3 suites  ±0   7s ⏱️ -1s
123 tests + 5  123 ✅ + 5  0 💤 ±0  0 ❌ ±0 
243 runs  +10  243 ✅ +10  0 💤 ±0  0 ❌ ±0 

Results for commit c2bad1e. ± Comparison against base commit a64a77b.

♻️ This comment has been updated with latest results.

@coveralls

coveralls commented May 30, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 26683760573

Coverage increased (+0.6%) to 85.906%

Details

  • Coverage increased (+0.6%) from the base build.
  • Patch coverage: 49 of 49 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 1082
Covered Lines: 983
Line Coverage: 90.85%
Relevant Branches: 337
Covered Branches: 236
Branch Coverage: 70.03%
Branches in Coverage %: Yes
Coverage Strength: 328.91 hits per line

💛 - Coveralls

Adds an RFC 7807 application/problem+json responder to RequestMockBuilder that emits the ProblemDetails shape via the existing JSON serialization path, defaulting title from the status reason phrase and status from the status code.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dennisdoomen dennisdoomen force-pushed the dennisdoomen/add-responds-with-problem-details branch from 0afbaa2 to c2bad1e Compare May 30, 2026 12:25
@dennisdoomen dennisdoomen marked this pull request as ready for review May 30, 2026 12:26
@dennisdoomen dennisdoomen changed the title Add RespondsWithProblemDetails (closes #122) Add RespondsWithProblemDetails May 30, 2026
@dennisdoomen dennisdoomen merged commit d10fc5c into main May 30, 2026
10 checks passed
@dennisdoomen dennisdoomen deleted the dennisdoomen/add-responds-with-problem-details branch May 30, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add RespondsWithProblemDetails (RFC 7807 application/problem+json)

3 participants