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
impl ServerBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder with default settings (bind to 127.0.0.1:0, no auth).
Sourcepub fn capture_capacity(self, max: usize) -> Self
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.
Sourcepub fn fixtures(self, fixtures: Vec<Fixture>) -> Self
pub fn fixtures(self, fixtures: Vec<Fixture>) -> Self
Appends multiple fixtures to the server’s match list.
Sourcepub fn fixture_count(&self) -> usize
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.
Sourcepub fn bind(self, addr: &str) -> Self
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).
Sourcepub fn verbose(self, v: bool) -> Self
pub fn verbose(self, v: bool) -> Self
Enables or disables verbose logging of matched fixtures and request details.
Sourcepub fn with_auth(self, enabled: bool) -> Self
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.
Sourcepub fn with_bearer_token(self, token: &str) -> Self
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.
Sourcepub fn with_bearer_token_uses(self, token: &str, max_uses: u64) -> Self
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).
Sourcepub fn with_oauth(self, config: OAuthConfig) -> Self
pub fn with_oauth(self, config: OAuthConfig) -> Self
Enable the embedded OAuth server with custom client configuration.
Sourcepub fn with_oauth_defaults(self) -> Self
pub fn with_oauth_defaults(self) -> Self
Enable the embedded OAuth server with default client credentials (client_id: “mock-client”, client_secret: “mock-secret”).
Sourcepub fn load_yaml(self, path: &Path) -> Result<Self, Box<dyn Error>>
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.
Sourcepub fn load_yaml_dir(self, dir: &Path) -> Result<Self, Box<dyn Error>>
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.
Sourcepub fn watch(self, enabled: bool) -> Self
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.
Sourcepub async fn build(self) -> Result<MockServer, Box<dyn Error>>
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.