pub struct MockHttpClient { /* private fields */ }Expand description
Mock HTTP client for testing.
Allows configuring predetermined responses for specific requests without making actual HTTP calls.
§Example
let mock = MockHttpClient::new();
mock.add_response(
"POST /v1/chat/completions",
HttpResponse {
status: 200,
body: r#"{"result": "success"}"#.to_string(),
},
);Implementations§
Source§impl MockHttpClient
impl MockHttpClient
Sourcepub fn add_response(&self, key: &str, response: Result<HttpResponse>)
pub fn add_response(&self, key: &str, response: Result<HttpResponse>)
Add a predetermined response for a specific method and path.
The key is formatted as “{method} {path}”. Multiple responses can be added for the same key - they will be returned in FIFO order.
Sourcepub fn add_response_with_trigger(
&self,
key: &str,
response: Result<HttpResponse>,
) -> Sender<()>
pub fn add_response_with_trigger( &self, key: &str, response: Result<HttpResponse>, ) -> Sender<()>
Add a response that will wait for a manual trigger before completing.
Returns a sender that when triggered (by sending () or dropping) will
cause the HTTP request to complete with the given response.
§Example
let trigger = mock.add_response_with_trigger(
"POST /test",
Ok(HttpResponse { status: 200, body: "ok".to_string() })
);
// ... request is now blocked waiting ...
trigger.send(()).unwrap(); // Now it completesSourcepub fn get_calls(&self) -> Vec<MockCall>
pub fn get_calls(&self) -> Vec<MockCall>
Get all calls that have been made to this mock client.
Sourcepub fn clear_calls(&self)
pub fn clear_calls(&self)
Clear all recorded calls.
Sourcepub fn call_count(&self) -> usize
pub fn call_count(&self) -> usize
Get the number of calls made.
Sourcepub fn in_flight_count(&self) -> usize
pub fn in_flight_count(&self) -> usize
Get the number of requests currently in-flight (executing).
This is useful for testing cancellation - if a request is aborted, the in-flight count will decrease.
Trait Implementations§
Source§impl Clone for MockHttpClient
impl Clone for MockHttpClient
Source§fn clone(&self) -> MockHttpClient
fn clone(&self) -> MockHttpClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for MockHttpClient
impl Default for MockHttpClient
Source§impl HttpClient for MockHttpClient
impl HttpClient for MockHttpClient
Source§fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 RequestData,
api_key: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<HttpResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 RequestData,
api_key: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<HttpResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Auto Trait Implementations§
impl Freeze for MockHttpClient
impl !RefUnwindSafe for MockHttpClient
impl Send for MockHttpClient
impl Sync for MockHttpClient
impl Unpin for MockHttpClient
impl UnsafeUnpin for MockHttpClient
impl !UnwindSafe for MockHttpClient
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more