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
impl MockConfigBuilder
Sourcepub fn new(method: impl Into<String>, path: impl Into<String>) -> Self
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})
Sourcepub fn body(self, body: Value) -> Self
pub fn body(self, body: Value) -> Self
Set the response body (supports templating with {{variables}})
Sourcepub fn header(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn header(self, key: impl Into<String>, value: impl Into<String>) -> Self
Add a single response header
Sourcepub fn latency_ms(self, ms: u64) -> Self
pub fn latency_ms(self, ms: u64) -> Self
Set the latency in milliseconds
Sourcepub fn with_header(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> Self
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")Sourcepub fn with_headers(self, headers: HashMap<String, String>) -> Self
pub fn with_headers(self, headers: HashMap<String, String>) -> Self
Require multiple headers to be present and match
Sourcepub fn with_query_param(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> Self
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")Sourcepub fn with_query_params(self, params: HashMap<String, String>) -> Self
pub fn with_query_params(self, params: HashMap<String, String>) -> Self
Require multiple query parameters to be present and match
Sourcepub fn with_body_pattern(self, pattern: impl Into<String>) -> Self
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 matchSourcepub fn with_json_path(self, json_path: impl Into<String>) -> Self
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 > 18Sourcepub fn with_xpath(self, xpath: impl Into<String>) -> Self
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']")Sourcepub fn with_custom_matcher(self, expression: impl Into<String>) -> Self
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/.*\"")Sourcepub fn priority(self, priority: i32) -> Self
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.
Sourcepub fn scenario(self, scenario: impl Into<String>) -> Self
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.
Sourcepub fn when_scenario_state(self, state: impl Into<String>) -> Self
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.
Sourcepub fn will_set_scenario_state(self, state: impl Into<String>) -> Self
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.
Sourcepub fn build(self) -> MockConfig
pub fn build(self) -> MockConfig
Build the mock configuration