Add TreatBodyAsTextual() to force textual body decoding#163
Merged
Conversation
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>
Coverage Report for CI Build 29335661863Coverage increased (+0.1%) to 84.925%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
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")); |
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>
This was referenced Jul 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Body-based matchers (
WithBody,WithBodyMatchingRegex,WithBodyMatchingJson, form-field matching) all readRequestInfo.Body, which staysnullwhenever the request's Content-Type isn't recognized as textual byIsBodyLikelyTextual(). 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
PrefetchBodypattern: the public toggle is a simple flag, and all the mechanics are localized inHttpMock.BuildRequestInfo:RequestMockgets an internalForceTextualBodyflag, copied from the builder.HttpMock.BuildRequestInfocomputes whether any currently registered mock opted in, and sets it on the constructedRequestInfovia an internalinit-only property (avoiding aboolconstructor/method parameter per the repo's C# guidelines).RequestInfo.BodydecodesRawBodyas text when eitherIsBodyLikelyTextual()or the forced flag is true.No changes were needed to the individual body-matching methods (
WithBody, etc.) since they already just readRequestInfo.Body.API surface
Only one new public member:
RequestMockBuilder.TreatBodyAsTextual().ApprovedApiverified 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.Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com