pub struct Server { /* private fields */ }
Expand description
One instance of the mock server.
Mockito uses a server pool to manage running servers. Once the pool reaches capacity, new requests will have to wait for a free server. The size of the server pool is set to 100.
Most of the times, you should initialize new servers with Server::new
, which fetches
the next available instance from the pool:
let mut server = mockito::Server::new();
If for any reason you’d like to bypass the server pool, you can use Server::new_with_port
:
let mut server = mockito::Server::new_with_port(0);
Implementations§
source§impl Server
impl Server
sourcepub fn new() -> ServerGuard
pub fn new() -> ServerGuard
Fetches a new mock server from the server pool.
This method will panic on failure.
If for any reason you’d like to bypass the server pool, you can use Server::new_with_port
:
sourcepub async fn new_async() -> ServerGuard
pub async fn new_async() -> ServerGuard
Same as Server::new
but async.
sourcepub fn new_with_port(port: u16) -> Server
pub fn new_with_port(port: u16) -> Server
Starts a new server on a given port. If the port is set to 0
, a random available
port will be assigned. Note that this call bypasses the server pool.
This method will panic on failure.
sourcepub async fn new_with_port_async(port: u16) -> Server
pub async fn new_with_port_async(port: u16) -> Server
Same as Server::new_with_port
but async.
sourcepub fn mock<P: Into<Matcher>>(&mut self, method: &str, path: P) -> Mock
pub fn mock<P: Into<Matcher>>(&mut self, method: &str, path: P) -> Mock
Initializes a mock with the given HTTP method
and path
.
The mock is enabled on the server only after calling the Mock::create
method.
Example
let mut s = mockito::Server::new();
let _m1 = s.mock("GET", "/");
let _m2 = s.mock("POST", "/users");
let _m3 = s.mock("DELETE", "/users?id=1");
sourcepub fn host_with_port(&self) -> String
pub fn host_with_port(&self) -> String
The host and port of the mock server.
Can be used with std::net::TcpStream
.
sourcepub async fn reset_async(&mut self)
pub async fn reset_async(&mut self)
Same as Server::reset
but async.