You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add TreatBodyAsTextual() to force textual body decoding (#163)
* Add TreatBodyAsTextual() to force textual body decoding
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>
* Document TreatBodyAsTextual() in SKILL.md and advanced docs
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: website/docs/advanced.md
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -114,6 +114,18 @@ mock.ForPost()
114
114
.RespondsWithStatus(HttpStatusCode.NoContent);
115
115
```
116
116
117
+
### Forcing a Body to be Treated as Textual
118
+
119
+
Body matchers (`WithBody`, `WithBodyMatchingJson`, `WithBodyMatchingRegex`, and form-field matching) only work when the request's Content-Type is recognized as textual (`text/*`, `multipart/*`, `application/json`, `application/xml`, and similar). If your request uses a Content-Type Mockly doesn't recognize as textual — but the body is actually text — opt in with `TreatBodyAsTextual()`:
120
+
121
+
```csharp
122
+
mock.ForPost()
123
+
.WithPath("/api/test")
124
+
.WithBody("*something*")
125
+
.TreatBodyAsTextual()
126
+
.RespondsWithStatus(HttpStatusCode.NoContent);
127
+
```
128
+
117
129
## Request Body Prefetching
118
130
119
131
By default, Mockly prefetches the request body for matchers. You can disable this to defer reading content inside your predicate:
0 commit comments