Struct httpmock::MockServer [−][src]
pub struct MockServer { /* fields omitted */ }
Expand description
A mock server that is able to receive and respond to HTTP requests.
Implementations
Asynchronously connects to a remote mock server that is running in standalone mode using
the provided address of the form
Synchronously connects to a remote mock server that is running in standalone mode using
the provided address of the form
Asynchronously connects to a remote mock server that is running in standalone mode using
connection parameters stored in HTTPMOCK_HOST
and HTTPMOCK_PORT
environment variables.
Synchronously connects to a remote mock server that is running in standalone mode using
connection parameters stored in HTTPMOCK_HOST
and HTTPMOCK_PORT
environment variables.
Starts a new MockServer
asynchronously.
Attention: This library manages a pool of MockServer
instances in the background.
Instead of always starting a new mock server, a MockServer
instance is only created
on demand if there is no free MockServer
instance in the pool and the pool has not
reached a maximum size yet. Otherwise, THIS METHOD WILL BLOCK the executing function
until a free mock server is available.
This allows to run many tests in parallel, but will prevent exhaust the executing machine by creating too many mock servers.
A MockServer
instance is automatically taken from the pool whenever this method is called.
The instance is put back into the pool automatically when the corresponding
‘MockServer’ variable gets out of scope.
Starts a new MockServer
synchronously.
Attention: This library manages a pool of MockServer
instances in the background.
Instead of always starting a new mock server, a MockServer
instance is only created
on demand if there is no free MockServer
instance in the pool and the pool has not
reached a maximum size yet. Otherwise, THIS METHOD WILL BLOCK the executing function
until a free mock server is available.
This allows to run many tests in parallel, but will prevent exhaust the executing machine by creating too many mock servers.
A MockServer
instance is automatically taken from the pool whenever this method is called.
The instance is put back into the pool automatically when the corresponding
‘MockServer’ variable gets out of scope.
The hostname of the MockServer
. By default, this is 127.0.0.1
.
In standalone mode, the hostname will be the host where the standalone mock server is
running.
Builds the address for a specific path on the mock server.
Example:
// Start a local mock server for exclusive use by this test function.
let server = httpmock::MockServer::start();
let expected_addr_str = format!("127.0.0.1:{}", server.port());
// Get the address of the MockServer.
let addr = server.address();
// Ensure the returned URL is as expected
assert_eq!(expected_addr_str, addr.to_string());
Builds the URL for a specific path on the mock server.
Example:
// Start a local mock server for exclusive use by this test function.
let server = httpmock::MockServer::start();
let expected_url = format!("http://127.0.0.1:{}/hello", server.port());
// Get the URL for path "/hello".
let url = server.url("/hello");
// Ensure the returned URL is as expected
assert_eq!(expected_url, url);
Builds the base URL for the mock server.
Example:
// Start a local mock server for exclusive use by this test function.
let server = httpmock::MockServer::start();
let expected_url = format!("http://127.0.0.1:{}", server.port());
// Get the URL for path "/hello".
let url = server.base_url();
// Ensure the returned URL is as expected
assert_eq!(expected_url, url);
Creates a Mock object on the mock server.
Example:
use isahc::get;
let server = httpmock::MockServer::start();
let mock = server.mock(|when, then| {
when.path("/hello");
then.status(200);
});
get(server.url("/hello")).unwrap();
mock.assert();
Creates a Mock object on the mock server.
Example:
use isahc::{get_async};
async_std::task::block_on(async {
let server = httpmock::MockServer::start();
let mock = server
.mock_async(|when, then| {
when.path("/hello");
then.status(200);
})
.await;
get_async(server.url("/hello")).await.unwrap();
mock.assert_async().await;
});
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for MockServer
impl Send for MockServer
impl Sync for MockServer
impl Unpin for MockServer
impl !UnwindSafe for MockServer
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more