Skip to main content

MockServer

Struct MockServer 

Source
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

Source

pub fn start() -> Self

Starts a new mock server on a random available port.

The server begins listening immediately and runs in a background thread.

§Example
let server = MockServer::start();
println!("Server listening on {}", server.addr());
Source

pub fn start_with_options(options: MockServerOptions) -> Self

Starts a mock server with custom options.

Source

pub fn addr(&self) -> SocketAddr

Returns the socket address the server is listening on.

Source

pub fn url(&self) -> String

Returns the base URL for the server (e.g., “http://127.0.0.1:12345”).

Source

pub fn url_for(&self, path: &str) -> String

Returns a URL for the given path.

Source

pub fn mock_response(&self, path: impl Into<String>, response: MockResponse)

Configures a canned response for a specific path.

Use * at the end of the path for prefix matching.

§Example
server.mock_response("/api/users", MockResponse::ok().json(&users));
server.mock_response("/api/*", MockResponse::with_status(404));
Source

pub fn set_default_response(&self, response: MockResponse)

Sets the default response for unmatched paths.

Source

pub fn requests(&self) -> Vec<RecordedRequest>

Returns all recorded requests.

Source

pub fn request_count(&self) -> usize

Returns the number of recorded requests.

Source

pub fn requests_for(&self, path: &str) -> Vec<RecordedRequest>

Returns requests matching the given path.

Source

pub fn last_request(&self) -> Option<RecordedRequest>

Returns the last recorded request.

Source

pub fn clear_requests(&self)

Clears all recorded requests.

Source

pub fn clear_responses(&self)

Clears all configured responses.

Source

pub fn reset(&self)

Resets the server (clears requests and responses).

Source

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.

Source

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.

Source

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.

Source

pub fn assert_request_count(&self, expected: usize)

Asserts the total number of requests received.

§Panics

Panics if the count doesn’t match.

Trait Implementations§

Source§

impl Drop for MockServer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T