pub struct DynamicStub {
pub method: String,
pub path: String,
pub status: Arc<RwLock<u16>>,
pub headers: Arc<RwLock<HashMap<String, String>>>,
pub response_fn: DynamicResponseFn,
pub latency_ms: Option<u64>,
}Expand description
Dynamic stub with runtime response generation
Fields§
§method: StringHTTP method
path: StringPath pattern
status: Arc<RwLock<u16>>HTTP status code (can be dynamic)
headers: Arc<RwLock<HashMap<String, String>>>Response headers (can be dynamic)
response_fn: DynamicResponseFnDynamic response function
latency_ms: Option<u64>Optional latency in milliseconds
Implementations§
Source§impl DynamicStub
impl DynamicStub
Sourcepub fn new<F>(
method: impl Into<String>,
path: impl Into<String>,
response_fn: F,
) -> Self
pub fn new<F>( method: impl Into<String>, path: impl Into<String>, response_fn: F, ) -> Self
Create a new dynamic stub
Sourcepub async fn set_status(&self, status: u16)
pub async fn set_status(&self, status: u16)
Set the HTTP status code
Sourcepub async fn get_status(&self) -> u16
pub async fn get_status(&self) -> u16
Get the current status code
Sourcepub async fn add_header(&self, key: String, value: String)
pub async fn add_header(&self, key: String, value: String)
Add a response header
Sourcepub async fn remove_header(&self, key: &str)
pub async fn remove_header(&self, key: &str)
Remove a response header
Sourcepub async fn get_headers(&self) -> HashMap<String, String>
pub async fn get_headers(&self) -> HashMap<String, String>
Get all headers (returns a clone)
For more efficient read-only access, consider using with_headers() instead.
Sourcepub async fn with_headers<F, R>(&self, f: F) -> R
pub async fn with_headers<F, R>(&self, f: F) -> R
Access headers without cloning via a callback
This is more efficient than get_headers() when you only need to
read header values without modifying them.
§Examples
let stub = DynamicStub::new("GET", "/test", |_| json!({}));
stub.add_header("X-Custom".to_string(), "value".to_string()).await;
// Efficient read-only access
let has_custom = stub.with_headers(|headers| {
headers.contains_key("X-Custom")
}).await;Sourcepub fn generate_response(&self, ctx: &RequestContext) -> Value
pub fn generate_response(&self, ctx: &RequestContext) -> Value
Generate a response for a given request context
Sourcepub fn with_latency(self, ms: u64) -> Self
pub fn with_latency(self, ms: u64) -> Self
Set latency
Auto Trait Implementations§
impl Freeze for DynamicStub
impl !RefUnwindSafe for DynamicStub
impl Send for DynamicStub
impl Sync for DynamicStub
impl Unpin for DynamicStub
impl !UnwindSafe for DynamicStub
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more