pub enum Body {
Plain(String),
Typed {
body_type: String,
json: String,
},
File {
file_path: String,
content_type: Option<String>,
template_type: Option<String>,
},
AllOf(Vec<Body>),
Matcher {
body_type: String,
value_key: String,
value: String,
},
Object(Map<String, Value>),
}Expand description
Request/response body — either a plain string, a typed object, or a file reference.
Variants§
Plain(String)
A plain string body.
Typed
A typed body (e.g., JSON).
File
A file body (type: "FILE"), with optional template evaluation.
AllOf(Vec<Body>)
An ALL_OF composite body matcher — every nested body matcher must match.
Serialises to { "type": "ALL_OF", "bodyAllOf": [ <body>, ... ] },
recursing through the normal Body serialisation for each sub-body.
Matcher
A single-value typed body matcher whose value lives under a named key
(e.g. JSON_PATH → jsonPath, REGEX → regex, XPATH → xpath).
Serialises to { "type": <body_type>, <value_key>: <value> }. Use the
Body::json_path / Body::regex constructors for the common cases.
Object(Map<String, Value>)
Any typed body object captured verbatim as a JSON object — the
forward-compatible catch-all for body matcher/value types that do not
have a dedicated variant (STRING/subString, XML, XML_SCHEMA,
JSON_SCHEMA, PARAMETERS, BINARY, GRAPHQL, MULTIPART, WASM,
JSON_RPC, FUZZY, …). Serialises the map back exactly, so every body
shape round-trips without silent field loss.
Implementations§
Source§impl Body
impl Body
Sourcepub fn file(file_path: impl Into<String>) -> Self
pub fn file(file_path: impl Into<String>) -> Self
Create a FILE body referencing a path on the server filesystem.
§Example
use mockserver_client::Body;
let body = Body::file("/data/response.json")
.with_content_type("application/json")
.with_template_type("VELOCITY");Sourcepub fn with_content_type(self, content_type: impl Into<String>) -> Self
pub fn with_content_type(self, content_type: impl Into<String>) -> Self
Set the content type on a FILE body. No-op on other variants.
Sourcepub fn with_template_type(self, template_type: impl Into<String>) -> Self
pub fn with_template_type(self, template_type: impl Into<String>) -> Self
Set the template type (e.g., “VELOCITY”, “MUSTACHE”) on a FILE body. No-op on other variants.
Sourcepub fn all_of(bodies: Vec<Body>) -> Self
pub fn all_of(bodies: Vec<Body>) -> Self
Create an ALL_OF composite body matcher — every nested body matcher
must match for the request body to match.
§Example
use mockserver_client::Body;
let body = Body::all_of(vec![
Body::json_path("$.name"),
Body::regex(".*active.*"),
]);Sourcepub fn json_path(expression: impl Into<String>) -> Self
pub fn json_path(expression: impl Into<String>) -> Self
Create a JSON_PATH body matcher.
Serialises to { "type": "JSON_PATH", "jsonPath": <expression> }.
Sourcepub fn regex(pattern: impl Into<String>) -> Self
pub fn regex(pattern: impl Into<String>) -> Self
Create a REGEX body matcher.
Serialises to { "type": "REGEX", "regex": <pattern> }.
Sourcepub fn xpath(expression: impl Into<String>) -> Self
pub fn xpath(expression: impl Into<String>) -> Self
Create an XPATH body matcher ({ "type": "XPATH", "xpath": <expr> }).
Sourcepub fn string(value: impl Into<String>, sub_string: bool) -> Self
pub fn string(value: impl Into<String>, sub_string: bool) -> Self
Create a STRING body matcher. When sub_string is true the value need
only be a substring of the request body.
Serialises to { "type": "STRING", "string": <value>, "subString": <b> }.
Sourcepub fn xml(value: impl Into<String>) -> Self
pub fn xml(value: impl Into<String>) -> Self
Create an XML body matcher ({ "type": "XML", "xml": <value> }).
Sourcepub fn xml_schema(schema: impl Into<String>) -> Self
pub fn xml_schema(schema: impl Into<String>) -> Self
Create an XML_SCHEMA body matcher
({ "type": "XML_SCHEMA", "xmlSchema": <schema> }).
Sourcepub fn json_schema(schema: impl Into<String>) -> Self
pub fn json_schema(schema: impl Into<String>) -> Self
Create a JSON_SCHEMA body matcher
({ "type": "JSON_SCHEMA", "jsonSchema": <schema> }).
Sourcepub fn parameters(parameters: HashMap<String, Vec<String>>) -> Self
pub fn parameters(parameters: HashMap<String, Vec<String>>) -> Self
Create a PARAMETERS (form/body parameter) matcher
({ "type": "PARAMETERS", "parameters": { name: [values] } }).
Sourcepub fn binary(data: impl AsRef<[u8]>, content_type: Option<String>) -> Self
pub fn binary(data: impl AsRef<[u8]>, content_type: Option<String>) -> Self
Create a BINARY body matcher/value from raw bytes (base64-encoded on
the wire as base64Bytes), with an optional content type.
Sourcepub fn graphql(query: impl Into<String>) -> Self
pub fn graphql(query: impl Into<String>) -> Self
Create a GRAPHQL body matcher ({ "type": "GRAPHQL", "query": <query> }).