Skip to main content

Body

Enum Body 

Source
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).

Fields

§body_type: String
§json: String
§

File

A file body (type: "FILE"), with optional template evaluation.

Fields

§file_path: String
§content_type: Option<String>
§template_type: Option<String>
§

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_PATHjsonPath, REGEXregex, XPATHxpath).

Serialises to { "type": <body_type>, <value_key>: <value> }. Use the Body::json_path / Body::regex constructors for the common cases.

Fields

§body_type: String
§value_key: String
§value: String
§

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

Source

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");
Source

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.

Source

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.

Source

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.*"),
]);
Source

pub fn json_path(expression: impl Into<String>) -> Self

Create a JSON_PATH body matcher.

Serialises to { "type": "JSON_PATH", "jsonPath": <expression> }.

Source

pub fn regex(pattern: impl Into<String>) -> Self

Create a REGEX body matcher.

Serialises to { "type": "REGEX", "regex": <pattern> }.

Source

pub fn xpath(expression: impl Into<String>) -> Self

Create an XPATH body matcher ({ "type": "XPATH", "xpath": <expr> }).

Source

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> }.

Source

pub fn xml(value: impl Into<String>) -> Self

Create an XML body matcher ({ "type": "XML", "xml": <value> }).

Source

pub fn xml_schema(schema: impl Into<String>) -> Self

Create an XML_SCHEMA body matcher ({ "type": "XML_SCHEMA", "xmlSchema": <schema> }).

Source

pub fn json_schema(schema: impl Into<String>) -> Self

Create a JSON_SCHEMA body matcher ({ "type": "JSON_SCHEMA", "jsonSchema": <schema> }).

Source

pub fn parameters(parameters: HashMap<String, Vec<String>>) -> Self

Create a PARAMETERS (form/body parameter) matcher ({ "type": "PARAMETERS", "parameters": { name: [values] } }).

Source

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.

Source

pub fn graphql(query: impl Into<String>) -> Self

Create a GRAPHQL body matcher ({ "type": "GRAPHQL", "query": <query> }).

Source

pub fn wasm(object: Map<String, Value>) -> Self

Create a WASM custom-rule body matcher from a pre-built JSON object.

Captured verbatim, so any current or future WASM matcher fields survive a round-trip.

Source

pub fn object(object: Map<String, Value>) -> Self

Build a Body::Object from a raw JSON object — the escape hatch for any body type not covered by a dedicated constructor.

Trait Implementations§

Source§

impl Clone for Body

Source§

fn clone(&self) -> Body

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Body

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Body

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Body

Source§

fn eq(&self, other: &Body) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Body

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Body

Auto Trait Implementations§

§

impl Freeze for Body

§

impl RefUnwindSafe for Body

§

impl Send for Body

§

impl Sync for Body

§

impl Unpin for Body

§

impl UnsafeUnpin for Body

§

impl UnwindSafe for Body

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more