MockConfigBuilder

Struct MockConfigBuilder 

Source
pub struct MockConfigBuilder { /* private fields */ }
Expand description

Builder for creating mock configurations with fluent API

This builder provides a WireMock-like fluent API for creating mock configurations with comprehensive request matching and response configuration.

§Examples

use mockforge_sdk::admin::MockConfigBuilder;
use serde_json::json;

// Basic mock
let mock = MockConfigBuilder::new("GET", "/api/users")
    .name("Get Users")
    .status(200)
    .body(json!([{"id": 1, "name": "Alice"}]))
    .build();

// Advanced matching with headers and query params
let mock = MockConfigBuilder::new("POST", "/api/users")
    .name("Create User")
    .with_header("Authorization", "Bearer.*")
    .with_query_param("role", "admin")
    .with_body_pattern(r#"{"name":".*"}"#)
    .status(201)
    .body(json!({"id": 123, "created": true}))
    .priority(10)
    .build();

Implementations§

Source§

impl MockConfigBuilder

Source

pub fn new(method: impl Into<String>, path: impl Into<String>) -> Self

Create a new mock configuration builder

§Arguments
  • method - HTTP method (GET, POST, PUT, DELETE, etc.)
  • path - URL path pattern (supports path parameters like /users/{id})
Source

pub fn id(self, id: impl Into<String>) -> Self

Set the mock ID

Source

pub fn name(self, name: impl Into<String>) -> Self

Set the mock name

Source

pub fn body(self, body: Value) -> Self

Set the response body (supports templating with {{variables}})

Source

pub fn status(self, status: u16) -> Self

Set the response status code

Source

pub fn headers(self, headers: HashMap<String, String>) -> Self

Set response headers

Source

pub fn header(self, key: impl Into<String>, value: impl Into<String>) -> Self

Add a single response header

Source

pub fn latency_ms(self, ms: u64) -> Self

Set the latency in milliseconds

Source

pub fn enabled(self, enabled: bool) -> Self

Enable or disable the mock

Source

pub fn with_header( self, name: impl Into<String>, value: impl Into<String>, ) -> Self

Require a specific header to be present and match (supports regex patterns)

§Examples
MockConfigBuilder::new("GET", "/api/users")
    .with_header("Authorization", "Bearer.*")
    .with_header("Content-Type", "application/json")
Source

pub fn with_headers(self, headers: HashMap<String, String>) -> Self

Require multiple headers to be present and match

Source

pub fn with_query_param( self, name: impl Into<String>, value: impl Into<String>, ) -> Self

Require a specific query parameter to be present and match

§Examples
MockConfigBuilder::new("GET", "/api/users")
    .with_query_param("role", "admin")
    .with_query_param("limit", "10")
Source

pub fn with_query_params(self, params: HashMap<String, String>) -> Self

Require multiple query parameters to be present and match

Source

pub fn with_body_pattern(self, pattern: impl Into<String>) -> Self

Require the request body to match a pattern (supports exact match or regex)

§Examples
MockConfigBuilder::new("POST", "/api/users")
    .with_body_pattern(r#"{"name":".*"}"#)  // Regex pattern
    .with_body_pattern("exact string match")  // Exact match
Source

pub fn with_json_path(self, json_path: impl Into<String>) -> Self

Require the request body to match a JSONPath expression

§Examples
MockConfigBuilder::new("POST", "/api/users")
    .with_json_path("$.name")  // Body must have a 'name' field
    .with_json_path("$.age > 18")  // Body must have age > 18
Source

pub fn with_xpath(self, xpath: impl Into<String>) -> Self

Require the request body to match an XPath expression (for XML)

§Examples
MockConfigBuilder::new("POST", "/api/users")
    .with_xpath("/users/user[@id='123']")
Source

pub fn with_custom_matcher(self, expression: impl Into<String>) -> Self

Set a custom matcher expression for advanced matching logic

§Examples
MockConfigBuilder::new("GET", "/api/users")
    .with_custom_matcher("headers.content-type == \"application/json\"")
    .with_custom_matcher("path =~ \"/api/.*\"")
Source

pub fn priority(self, priority: i32) -> Self

Set the priority for this mock (higher priority mocks are matched first)

Default priority is 0. Higher numbers = higher priority.

Source

pub fn scenario(self, scenario: impl Into<String>) -> Self

Set the scenario name for stateful mocking

Scenarios allow you to create stateful mock sequences where the response depends on previous requests.

Source

pub fn when_scenario_state(self, state: impl Into<String>) -> Self

Require a specific scenario state for this mock to be active

This mock will only match if the scenario is in the specified state.

Source

pub fn will_set_scenario_state(self, state: impl Into<String>) -> Self

Set the new scenario state after this mock is matched

After this mock responds, the scenario will transition to this state.

Source

pub fn build(self) -> MockConfig

Build the mock configuration

Auto Trait Implementations§

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<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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> Same for T

Source§

type Output = T

Should always be Self
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
Source§

impl<T> ParallelSend for T