pub struct MockServer { /* private fields */ }Expand description
A mock HTTP server for integration testing.
MockServer spawns an actual TCP server on a random port, allowing you to
test HTTP client code against a real server. It records all incoming requests
and allows you to configure canned responses.
§Features
- Real TCP server: Listens on an actual port for real HTTP connections
- Request recording: Records all requests for later assertions
- Canned responses: Configure responses for specific paths
- Clean shutdown: Server shuts down when dropped
§Example
use fastapi_core::testing::{MockServer, MockResponse};
// Start a mock server
let server = MockServer::start();
// Configure a response
server.mock_response("/api/users", MockResponse::ok().json(&vec!["Alice", "Bob"]));
// Make requests with your HTTP client
let url = format!("http://{}/api/users", server.addr());
// ... make request ...
// Assert requests were made
let requests = server.requests();
assert_eq!(requests.len(), 1);
assert_eq!(requests[0].path, "/api/users");Implementations§
Source§impl MockServer
impl MockServer
Sourcepub fn start_with_options(options: MockServerOptions) -> Self
pub fn start_with_options(options: MockServerOptions) -> Self
Starts a mock server with custom options.
Sourcepub fn addr(&self) -> SocketAddr
pub fn addr(&self) -> SocketAddr
Returns the socket address the server is listening on.
Sourcepub fn url(&self) -> String
pub fn url(&self) -> String
Returns the base URL for the server (e.g., “http://127.0.0.1:12345”).
Sourcepub fn mock_response(&self, path: impl Into<String>, response: MockResponse)
pub fn mock_response(&self, path: impl Into<String>, response: MockResponse)
Sourcepub fn set_default_response(&self, response: MockResponse)
pub fn set_default_response(&self, response: MockResponse)
Sets the default response for unmatched paths.
Sourcepub fn requests(&self) -> Vec<RecordedRequest>
pub fn requests(&self) -> Vec<RecordedRequest>
Returns all recorded requests.
Sourcepub fn request_count(&self) -> usize
pub fn request_count(&self) -> usize
Returns the number of recorded requests.
Sourcepub fn requests_for(&self, path: &str) -> Vec<RecordedRequest>
pub fn requests_for(&self, path: &str) -> Vec<RecordedRequest>
Returns requests matching the given path.
Sourcepub fn last_request(&self) -> Option<RecordedRequest>
pub fn last_request(&self) -> Option<RecordedRequest>
Returns the last recorded request.
Sourcepub fn clear_requests(&self)
pub fn clear_requests(&self)
Clears all recorded requests.
Sourcepub fn clear_responses(&self)
pub fn clear_responses(&self)
Clears all configured responses.
Sourcepub fn wait_for_requests(&self, count: usize, timeout: Duration) -> bool
pub fn wait_for_requests(&self, count: usize, timeout: Duration) -> bool
Waits for a specific number of requests, with timeout.
Returns true if the expected number of requests were received,
false if the timeout was reached.
Sourcepub fn assert_received(&self, path: &str)
pub fn assert_received(&self, path: &str)
Asserts that a request was made to the given path.
§Panics
Panics if no request was made to the path.
Sourcepub fn assert_not_received(&self, path: &str)
pub fn assert_not_received(&self, path: &str)
Asserts that no request was made to the given path.
§Panics
Panics if a request was made to the path.