Skip to content

Add TreatBodyAsTextual() to force textual body decoding#163

Merged
dennisdoomen merged 2 commits into
mainfrom
feature/treat-body-as-textual
Jul 14, 2026
Merged

Add TreatBodyAsTextual() to force textual body decoding#163
dennisdoomen merged 2 commits into
mainfrom
feature/treat-body-as-textual

Conversation

@dennisdoomen

Copy link
Copy Markdown
Owner

Problem

Body-based matchers (WithBody, WithBodyMatchingRegex, WithBodyMatchingJson, form-field matching) all read RequestInfo.Body, which stays null whenever the request's Content-Type isn't recognized as textual by IsBodyLikelyTextual(). There was previously no way for a caller to opt in when they know the body is actually text but it uses an unrecognized or binary-looking media type (e.g. application/octet-stream, a custom/vendor type, etc.).

Fix

New fluent method RequestMockBuilder.TreatBodyAsTextual() lets a mock opt into decoding the body as text regardless of Content-Type.

The implementation mirrors the existing PrefetchBody pattern: the public toggle is a simple flag, and all the mechanics are localized in HttpMock.BuildRequestInfo:

  • RequestMock gets an internal ForceTextualBody flag, copied from the builder.
  • HttpMock.BuildRequestInfo computes whether any currently registered mock opted in, and sets it on the constructed RequestInfo via an internal init-only property (avoiding a bool constructor/method parameter per the repo's C# guidelines).
  • RequestInfo.Body decodes RawBody as text when either IsBodyLikelyTextual() or the forced flag is true.

No changes were needed to the individual body-matching methods (WithBody, etc.) since they already just read RequestInfo.Body.

API surface

Only one new public member: RequestMockBuilder.TreatBodyAsTextual(). ApprovedApi verified files updated accordingly.

Testing

  • Cannot_match_body_with_an_unrecognized_content_type_by_default — confirms current behavior (no match without the override).
  • Can_force_a_body_with_an_unrecognized_content_type_to_be_treated_as_textual — confirms .TreatBodyAsTextual() makes the match succeed.
  • Full test suite: 197/196 passed (net8.0 / net472); API verification tests pass.

Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com

dennisdoomen and others added 2 commits July 14, 2026 15:09
Body-based matchers (WithBody, WithBodyMatchingRegex, WithBodyMatchingJson,
form-field matching) only match against RequestInfo.Body, which stays null
when the request's Content-Type isn't recognized as textual. There's no way
for callers to opt in when they know the body is actually text but uses an
unrecognized or binary-looking media type.

RequestMockBuilder.TreatBodyAsTextual() lets a mock opt into decoding the
body as text regardless of Content-Type. Mirrors the existing PrefetchBody
pattern: the public toggle is a simple flag, and the mechanics are
localized in HttpMock.BuildRequestInfo, which now forces RequestInfo to
decode the body as text when any registered mock has opted in. No changes
were needed to the individual body-matching methods, since they already
read RequestInfo.Body.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@coveralls

coveralls commented Jul 14, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 29335661863

Coverage increased (+0.1%) to 84.925%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 16 of 16 lines across 4 files 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: 1515
Covered Lines: 1355
Line Coverage: 89.44%
Relevant Branches: 495
Covered Branches: 352
Branch Coverage: 71.11%
Branches in Coverage %: Yes
Coverage Strength: 331.93 hits per line

💛 - Coveralls

@dennisdoomen dennisdoomen added the enhancement New feature or request label Jul 14, 2026
@dennisdoomen
dennisdoomen enabled auto-merge (squash) July 14, 2026 13:18

var client = mock.GetClient();

var content = new ByteArrayContent(Encoding.UTF8.GetBytes("a body with something in it"));

var client = mock.GetClient();

var content = new ByteArrayContent(Encoding.UTF8.GetBytes("a body with something in it"));
@github-actions

Copy link
Copy Markdown

Test Results

  3 files  ±0    3 suites  ±0   7s ⏱️ ±0s
199 tests +2  199 ✅ +2  0 💤 ±0  0 ❌ ±0 
395 runs  +4  395 ✅ +4  0 💤 ±0  0 ❌ ±0 

Results for commit 154b9eb. ± Comparison against base commit 4fe054f.

@dennisdoomen
dennisdoomen merged commit 89ac9d9 into main Jul 14, 2026
10 checks passed
@dennisdoomen
dennisdoomen deleted the feature/treat-body-as-textual branch July 14, 2026 13:22
dennisdoomen added a commit that referenced this pull request Jul 14, 2026
Addresses GitHub Advanced Security / InspectCode findings on PR #163.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

3 participants