Package org.mockserver.matchers
Interface BodySource
public interface BodySource
Minimal abstraction over the pieces of an HTTP message that the shared body-dispatch logic in
BodyMatching needs to match an actual message body against a template body matcher.
Both HttpRequest (request matching, via
HttpRequestPropertiesMatcher) and HttpResponse (response
matching, via HttpResponseMatcher) are adapted to this interface so a single
dispatch implementation drives both — giving the response body matcher the same JSON/XML/form
conversion, optional-body short-circuit, multipart decoding and binary original/compressed
dual-match the request body matcher already has.
The methods mirror the existing message accessors exactly:
getBodyAsString()— the decoded body as a string, ornullwhen absent.getBodyAsRawBytes()— the decoded raw body bytes (nevernull; empty array when absent).getOriginalBody()— the on-the-wire (e.g. still-compressed) bytes when the message arrived compressed and was decompressed, otherwisenull.getFirstHeader(String)— the first value of a header (theContent-Typeheader carries the multipart boundary and selects the JSON/XML/form conversion);""when absent.getOrComputeConvertedBody(Supplier)— memoises the expensive XML/form-to-JSON conversion per message so the parse runs once per message rather than once per candidate matcher. A message without a cache simply computes on every call.
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]getFirstHeader(String name) getOrComputeConvertedBody(Supplier<String> supplier) Returns the memoised result of the (potentially expensive) XML/form-to-JSON conversion, computing and caching it on first call when the underlying message supports caching.byte[]default HttpRequestThe originatingHttpRequestwhen this source wraps a request, otherwisenull.
-
Method Details
-
getBodyAsString
String getBodyAsString() -
getBodyAsRawBytes
byte[] getBodyAsRawBytes() -
getOriginalBody
byte[] getOriginalBody() -
getFirstHeader
-
getOrComputeConvertedBody
Returns the memoised result of the (potentially expensive) XML/form-to-JSON conversion, computing and caching it on first call when the underlying message supports caching. Messages without a cache (e.g. responses) invoke the supplier on every call — the supplier is a pure function of the immutable body so the result is identical either way. -
requestForLogging
The originatingHttpRequestwhen this source wraps a request, otherwisenull. Used solely so a JSON-conversion failure log on the request path reports the actual request exactly as before; the response path has no request to report and returnsnull.
-