Struct httpmock::MockServer[][src]

pub struct MockServer { /* fields omitted */ }

A mock server that is able to receive and respond to HTTP requests.

Implementations

impl MockServer[src]

pub async fn connect_async(address: &str) -> Self[src]

Asynchronously connects to a remote mock server that is running in standalone mode using the provided address of the form : (e.g. “127.0.0.1:8080”) to establish the connection.

pub fn connect(address: &str) -> Self[src]

Synchronously connects to a remote mock server that is running in standalone mode using the provided address of the form : (e.g. “127.0.0.1:8080”) to establish the connection.

pub async fn connect_from_env_async() -> Self[src]

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.

pub fn connect_from_env() -> Self[src]

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.

pub async fn start_async() -> Self[src]

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.

pub fn start() -> MockServer[src]

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.

pub fn host(&self) -> String[src]

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.

pub fn port(&self) -> u16[src]

The TCP port that the mock server is listening on.

pub fn address(&self) -> &SocketAddr[src]

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());

pub fn url<S: Into<String>>(&self, path: S) -> String[src]

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);

pub fn base_url(&self) -> String[src]

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);

pub fn mock<F>(&self, config_fn: F) -> MockRef<'_> where
    F: FnOnce(When, Then), 
[src]

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();

pub async fn mock_async<'a, F>(&'a self, config_fn: F) -> MockRef<'a> where
    F: FnOnce(When, Then), 
[src]

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

impl Drop for MockServer[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> WithSubscriber for T[src]