Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder { /* private fields */ }
Expand description

Builder for configuring and starting a MockServer.

Use method chaining to add fixtures, set bind address, enable auth, then call build() to start the server.

Implementations§

Source§

impl ServerBuilder

Source

pub fn new() -> Self

Creates a new builder with default settings (bind to 127.0.0.1:0, no auth).

Source

pub fn capture_capacity(self, max: usize) -> Self

Cap the captured-request log at max entries. When the log fills, the oldest entry is dropped to make room for each new one (FIFO).

Defaults to unbounded so short-lived #[tokio::test] servers can call get_requests() once and see every request. Long-lived standalone servers should set this to cap memory use — the body field alone can be multi-KB per entry.

capture_capacity(0) disables capture entirely. Nothing is pushed to the log, get_requests() always returns an empty Vec, and request_count() always returns 0. Use this when running under memory pressure and you don’t need the capture API at all — it’s measurably cheaper than the default (unbounded) path because push_captured short-circuits before taking the write lock contents.

Source

pub fn fixture(self, f: Fixture) -> Self

Appends a single fixture to the server’s match list.

Source

pub fn fixtures(self, fixtures: Vec<Fixture>) -> Self

Appends multiple fixtures to the server’s match list.

Source

pub fn fixture_count(&self) -> usize

Returns the number of fixtures currently staged in the builder. Useful for callers (e.g. the CLI) that want to warn when nothing was loaded.

Source

pub fn bind(self, addr: &str) -> Self

Sets the TCP bind address (e.g. "127.0.0.1:8080"). Defaults to "127.0.0.1:0" (random port).

Source

pub fn verbose(self, v: bool) -> Self

Enables or disables verbose logging of matched fixtures and request details.

Source

pub fn with_auth(self, enabled: bool) -> Self

Enable or disable auth enforcement on all LLM endpoints. When enabled, requests without a valid Authorization: Bearer <token> header receive a provider-specific HTTP 401 response.

Source

pub fn with_bearer_token(self, token: &str) -> Self

Register a bearer token with unlimited uses and implicitly enable auth. Requests with Authorization: Bearer <token> are accepted; requests without a valid token receive HTTP 401.

Source

pub fn with_bearer_token_uses(self, token: &str, max_uses: u64) -> Self

Register a bearer token that expires after max_uses requests, and implicitly enable auth. After exhaustion, the token returns HTTP 401. Use this to test token refresh flows deterministically (no real-time clocks).

Source

pub fn with_oauth(self, config: OAuthConfig) -> Self

Enable the embedded OAuth server with custom client configuration.

Source

pub fn with_oauth_defaults(self) -> Self

Enable the embedded OAuth server with default client credentials (client_id: “mock-client”, client_secret: “mock-secret”).

Source

pub fn load_yaml(self, path: &Path) -> Result<Self, Box<dyn Error>>

Loads fixtures from a single YAML file and appends them to the match list.

The path is also recorded as a hot-reload source. When watch is enabled (or the process receives SIGHUP on Unix), this file is re-read and the fixture list is atomically swapped.

Source

pub fn load_yaml_dir(self, dir: &Path) -> Result<Self, Box<dyn Error>>

Loads fixtures from all YAML files in a directory and appends them to the match list.

The directory path is also recorded as a hot-reload source. When watch is enabled (or the process receives SIGHUP on Unix), the directory is re-scanned and the fixture list is atomically swapped.

Source

pub fn watch(self, enabled: bool) -> Self

Enable file-watching hot-reload for sources loaded via load_yaml / load_yaml_dir. Invalid reloads keep the old fixtures serving. Requires the watch feature (on by default). SIGHUP also triggers a reload on Unix regardless of this flag. See docs/cli.md for full behavior.

Source

pub async fn build(self) -> Result<MockServer, Box<dyn Error>>

Validates all fixtures, starts the HTTP server, and returns a running MockServer.

Returns an error if any fixture is invalid or the bind address is unavailable.

Trait Implementations§

Source§

impl Default for ServerBuilder

Source§

fn default() -> Self

Returns the “default value” for a 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: 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,