pub struct FakeHandler { /* private fields */ }test-util only.Expand description
Simulates HTTP responses for testing without making actual network requests.
The FakeHandler lets you easily mock HTTP client behavior by providing predefined
responses or custom response logic. Available with the test-util feature enabled.
§Examples
Return a fixed status code:
use http::StatusCode;
use http_extensions::FakeHandler;
// All requests to this client will return 404 Not Found
let handler = FakeHandler::from(StatusCode::NOT_FOUND);Return a sequence of responses:
use http::StatusCode;
use http_extensions::FakeHandler;
let handler = FakeHandler::from(vec![
StatusCode::OK,
StatusCode::BAD_REQUEST,
StatusCode::INTERNAL_SERVER_ERROR,
]);
// First request → 200 OK
// Second request → 400 Bad Request
// Third request → 500 Internal Server Error
// Fourth request → Error (all responses consumed)Create dynamic responses based on the request:
use http::StatusCode;
use http_extensions::{FakeHandler, HttpResponseBuilder};
let handler = FakeHandler::from_fn(|request| {
if request.uri().path() == "/api/users" {
HttpResponseBuilder::new_fake()
.status(StatusCode::OK)
.text(r#"{"users": []}"#)
.build()
} else {
HttpResponseBuilder::new_fake()
.status(StatusCode::NOT_FOUND)
.text("Resource not found")
.build()
}
});§Working with HttpResponseBuilder
Use HttpResponseBuilder::new_fake to generate tailored test responses
with a fluent API. This is especially useful with FakeHandler::from_fn
to create responses that react to request data:
Implementations§
Source§impl FakeHandler
impl FakeHandler
Sourcepub fn from_fn<H>(handler: H) -> Self
pub fn from_fn<H>(handler: H) -> Self
Creates a handler from a synchronous request handler function.
Takes a function that processes requests synchronously and wraps it into an async handler internally.
§Examples
use http::StatusCode;
use http_extensions::{FakeHandler, HttpResponseBuilder};
let handler = FakeHandler::from_fn(|request| {
HttpResponseBuilder::new_fake()
.status(StatusCode::OK)
.text("Hello World")
.build()
});Sourcepub fn from_error_fn(
error: impl Fn(HttpRequest) -> HttpError + Send + Sync + 'static,
) -> Self
pub fn from_error_fn( error: impl Fn(HttpRequest) -> HttpError + Send + Sync + 'static, ) -> Self
Creates a handler that always returns HTTP error.
§Examples
use http_extensions::{FakeHandler, HttpError, HttpRequest};
let handler = FakeHandler::from_error_fn(|_request: HttpRequest| {
HttpError::validation("simulated error")
});Sourcepub fn from_async_fn<H, F>(handler: H) -> Selfwhere
H: Fn(HttpRequest) -> F + 'static + Send + Sync,
F: Future<Output = Result<HttpResponse>> + Send + 'static,
pub fn from_async_fn<H, F>(handler: H) -> Selfwhere
H: Fn(HttpRequest) -> F + 'static + Send + Sync,
F: Future<Output = Result<HttpResponse>> + Send + 'static,
Creates a handler from an asynchronous function.
Useful for complex async scenarios like simulating network delays or other asynchronous behavior in your tests.
§Examples
use http::StatusCode;
use http_extensions::{FakeHandler, HttpResponseBuilder};
let handler = FakeHandler::from_async_fn(|request| async move {
// Simulate network delay
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
HttpResponseBuilder::new_fake()
.status(StatusCode::OK)
.text("Response after delay")
.build()
});Sourcepub fn from_status_codes<T>(codes: T) -> Selfwhere
T: IntoIterator<Item = StatusCode>,
pub fn from_status_codes<T>(codes: T) -> Selfwhere
T: IntoIterator<Item = StatusCode>,
Creates a handler that returns a sequence of status codes.
Returns empty-body responses with the given status codes in order. When the sequence is exhausted, further requests will error.
§Examples
use http::StatusCode;
use http_extensions::FakeHandler;
let handler = FakeHandler::from_status_codes([
StatusCode::OK,
StatusCode::BAD_REQUEST,
StatusCode::INTERNAL_SERVER_ERROR,
]);§Errors
After all responses are consumed, further requests will return a validation error.
Sourcepub fn from_responses<T>(responses: T) -> Selfwhere
T: IntoIterator<Item = HttpResponse>,
pub fn from_responses<T>(responses: T) -> Selfwhere
T: IntoIterator<Item = HttpResponse>,
Creates a handler that returns predefined HTTP responses in sequence.
Takes full HttpResponse objects and returns them in order.
When the sequence is exhausted, further requests return an error.
§Examples
use http::StatusCode;
use http_extensions::{FakeHandler, HttpResponseBuilder};
let responses = vec![
HttpResponseBuilder::new_fake()
.status(StatusCode::OK)
.text("Success response")
.build()
.unwrap(),
HttpResponseBuilder::new_fake()
.status(StatusCode::NOT_FOUND)
.text("Not found")
.build()
.unwrap(),
];
let handler = FakeHandler::from_responses(responses);§Errors
After all responses are consumed, requests will fail with “all responses used by fake handler are already consumed”.
Sourcepub fn never_completes() -> Self
pub fn never_completes() -> Self
Creates a handler that never completes requests.
Useful for testing timeout handling in your code.
Trait Implementations§
Source§impl AsRef<HttpBodyBuilder> for FakeHandler
impl AsRef<HttpBodyBuilder> for FakeHandler
Source§fn as_ref(&self) -> &HttpBodyBuilder
fn as_ref(&self) -> &HttpBodyBuilder
Source§impl Clone for FakeHandler
impl Clone for FakeHandler
Source§fn clone(&self) -> FakeHandler
fn clone(&self) -> FakeHandler
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for FakeHandler
impl Debug for FakeHandler
Source§impl Default for FakeHandler
Creates a default FakeHandler that returns 200 OK responses.
impl Default for FakeHandler
Creates a default FakeHandler that returns 200 OK responses.
Source§impl From<Response<HttpBody>> for FakeHandler
Create a FakeHandler from a single HTTP response.
impl From<Response<HttpBody>> for FakeHandler
Create a FakeHandler from a single HTTP response.
The response body must be buffered to be reused for multiple requests. If the body is not buffered, an error will be returned when the handler tries to reuse it.
Source§fn from(value: HttpResponse) -> Self
fn from(value: HttpResponse) -> Self
Source§impl From<StatusCode> for FakeHandler
Create a FakeHandler from a single status code.
impl From<StatusCode> for FakeHandler
Create a FakeHandler from a single status code.
Source§fn from(value: StatusCode) -> Self
fn from(value: StatusCode) -> Self
Source§impl From<Vec<Response<HttpBody>>> for FakeHandler
Create a FakeHandler from a vector of HTTP responses.
impl From<Vec<Response<HttpBody>>> for FakeHandler
Create a FakeHandler from a vector of HTTP responses.
Source§fn from(value: Vec<HttpResponse>) -> Self
fn from(value: Vec<HttpResponse>) -> Self
Source§impl From<Vec<StatusCode>> for FakeHandler
Create a FakeHandler from a vector of status codes.
impl From<Vec<StatusCode>> for FakeHandler
Create a FakeHandler from a vector of status codes.