Struct httpmock::MockServer

source ·
pub struct MockServer { /* private fields */ }
Expand description

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

Implementations§

source§

impl MockServer

source

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

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. Note: This method requires the feature remote to be enabled.

source

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

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. Note: This method requires the feature remote to be enabled.

source

pub async fn connect_from_env_async() -> Self

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. Note: This method requires the feature remote to be enabled.

source

pub fn connect_from_env() -> Self

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. Note: This method requires the feature remote to be enabled.

source

pub async fn start_async() -> Self

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.

source

pub fn start() -> MockServer

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.

source

pub fn host(&self) -> String

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.

source

pub fn port(&self) -> u16

The TCP port that the mock server is listening on.

source

pub fn address(&self) -> &SocketAddr

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

pub fn url<S: Into<String>>(&self, path: S) -> 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);
source

pub fn base_url(&self) -> String

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

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

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

pub async fn mock_async<'a, F>(&'a self, spec_fn: F) -> Mock<'a>
where F: FnOnce(When, Then),

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§

source§

impl Drop for MockServer

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more