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 50.
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 you’d like to bypass the server pool or configure the server in a different way
(by setting a custom host and port or enabling auto-asserts), you can use Server::new_with_opts:
let opts = mockito::ServerOpts { port: 0, ..Default::default() };
let server_with_port = mockito::Server::new_with_opts(opts);
let opts = mockito::ServerOpts { host: "0.0.0.0", ..Default::default() };
let server_with_host = mockito::Server::new_with_opts(opts);
let opts = mockito::ServerOpts { assert_on_drop: true, ..Default::default() };
let server_with_auto_assert = mockito::Server::new_with_opts(opts);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
👎Deprecated since 1.3.0: Use Server::new_with_opts instead
pub fn new_with_port(port: u16) -> Server
Server::new_with_opts insteadDEPRECATED: Use Server::new_with_opts instead.
Sourcepub fn new_with_opts(opts: ServerOpts) -> Server
pub fn new_with_opts(opts: ServerOpts) -> Server
Starts a new server with the given options. Note that this call bypasses the server pool.
This method will panic on failure.
Sourcepub async fn new_with_port_async(port: u16) -> Server
👎Deprecated since 1.3.0: Use Server::new_with_opts_async instead
pub async fn new_with_port_async(port: u16) -> Server
Server::new_with_opts_async insteadDEPRECATED: Use Server::new_with_opts_async instead.
Sourcepub async fn new_with_opts_async(opts: ServerOpts) -> Server
pub async fn new_with_opts_async(opts: ServerOpts) -> Server
Same as Server::new_with_opts 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 fn socket_address(&self) -> SocketAddr
pub fn socket_address(&self) -> SocketAddr
The raw address of the mock server.
Sourcepub async fn reset_async(&mut self)
👎Deprecated since 1.0.1: Use Server::reset instead
pub async fn reset_async(&mut self)
Server::reset insteadDEPRECATED: Use Server::reset instead. The implementation is not async any more.