Struct httpmock::Mock

source ·
pub struct Mock<'a> {
    pub id: usize,
    /* private fields */
}
Expand description

Represents a reference to the mock object on a MockServer. It can be used to spy on the mock and also perform some management operations, such as deleting the mock from the MockServer.

Example

// Arrange
use httpmock::prelude::*;

let server = MockServer::start();

let mut mock = server.mock(|when, then|{
   when.path("/test");
   then.status(202);
});

// Send a first request, then delete the mock from the mock and send another request.
let response1 = isahc::get(server.url("/test")).unwrap();

// Fetch how often this mock has been called from the server until now
assert_eq!(mock.hits(), 1);

// Delete the mock from the mock server
mock.delete();

let response2 = isahc::get(server.url("/test")).unwrap();

// Assert
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404);

Fields§

§id: usize

Implementations§

source§

impl<'a> Mock<'a>

source

pub fn new(id: usize, server: &'a MockServer) -> Self

source

pub fn assert(&self)

This method asserts that the mock server received exactly one HTTP request that matched all the request requirements of this mock.

Attention: If you want to assert more than one request, consider using either Mock::assert_hits or Mock::hits.

Example
// Arrange: Create mock server and a mock
use httpmock::prelude::*;

let server = MockServer::start();

let mut mock = server.mock(|when, then| {
    when.path("/hits");
    then.status(200);
});

// Act: Send a request, then delete the mock from the mock and send another request.
isahc::get(server.url("/hits")).unwrap();

// Assert: Make sure the mock server received exactly one request that matched all
// the request requirements of the mock.
mock.assert();
Panics

This method will panic if there is a problem with the (standalone) mock server.

source

pub async fn assert_async(&self)

This method asserts that the mock server received exactly one HTTP request that matched all the request requirements of this mock.

Attention: If you want to assert more than one request, consider using either Mock::assert_hits or Mock::hits.

Example
// Arrange: Create mock server and a mock
use httpmock::prelude::*;

 async_std::task::block_on(async {
    let server = MockServer::start_async().await;

    let mut mock = server.mock_async(|when, then| {
        when.path("/hits");
        then.status(200);
    }).await;

    // Act: Send a request, then delete the mock from the mock and send another request.
    isahc::get_async(server.url("/hits")).await.unwrap();

    // Assert: Make sure the mock server received exactly one request that matched all
    // the request requirements of the mock.
    mock.assert_async().await;
});
Panics

This method will panic if there is a problem with the (standalone) mock server.

source

pub fn assert_hits(&self, hits: usize)

This method asserts that the mock server received the provided number of HTTP requests which matched all the request requirements of this mock.

Attention: Consider using the shorthand version Mock::assert if you want to assert only one hit.

Example
// Arrange: Create mock server and a mock
use httpmock::prelude::*;
use isahc::get;

let server = MockServer::start();

let mut mock = server.mock(|when, then| {
    when.path("/hits");
    then.status(200);
});

// Act: Send a request, then delete the mock from the mock and send another request.
get(server.url("/hits")).unwrap();
get(server.url("/hits")).unwrap();

// Assert: Make sure the mock server received exactly two requests that matched all
// the request requirements of the mock.
mock.assert_hits(2);
Panics

This method will panic if there is a problem with the (standalone) mock server.

source

pub async fn assert_hits_async(&self, hits: usize)

This method asserts that the mock server received the provided number of HTTP requests which matched all the request requirements of this mock.

Attention: Consider using the shorthand version Mock::assert_async if you want to assert only one hit.

Example
// Arrange: Create mock server and a mock
use httpmock::prelude::*;

 async_std::task::block_on(async {
    let server = MockServer::start_async().await;

    let mut mock = server.mock_async(|when, then| {
        when.path("/hits");
        then.status(200);
    }).await;

    // Act: Send a request, then delete the mock from the mock and send another request.
    isahc::get_async(server.url("/hits")).await.unwrap();
    isahc::get_async(server.url("/hits")).await.unwrap();

    // Assert: Make sure the mock server received exactly two requests that matched all
    // the request requirements of the mock.
    mock.assert_hits_async(2).await;
});
Panics

This method will panic if there is a problem with the (standalone) mock server.

source

pub fn hits(&self) -> usize

This method returns the number of times a mock has been called at the mock server.

Example
// Arrange: Create mock server and a mock
use httpmock::prelude::*;

let server = MockServer::start();

let mut mock = server.mock(|when, then| {
    when.path("/hits");
    then.status(200);
});

// Act: Send a request, then delete the mock from the mock and send another request.
isahc::get(server.url("/hits")).unwrap();

// Assert: Make sure the mock has been called exactly one time
assert_eq!(1, mock.hits());
Panics

This method will panic if there is a problem with the (standalone) mock server.

source

pub async fn hits_async(&self) -> usize

This method returns the number of times a mock has been called at the mock server.

Example
async_std::task::block_on(async {
    // Arrange: Create mock server and a mock
    use httpmock::prelude::*;

    let server = MockServer::start_async().await;

    let mut mock = server
        .mock_async(|when, then| {
            when.path("/hits");
            then.status(200);
        })
        .await;

    // Act: Send a request, then delete the mock from the mock and send another request.
    isahc::get_async(server.url("/hits")).await.unwrap();

    // Assert: Make sure the mock was called with all required attributes exactly one time.
    assert_eq!(1, mock.hits_async().await);
});
Panics

This method will panic if there is a problem with the (standalone) mock server.

source

pub fn delete(&mut self)

Deletes the associated mock object from the mock server.

Example
// Arrange
use httpmock::prelude::*;

let server = MockServer::start();

let mut mock = server.mock(|when, then|{
   when.path("/test");
   then.status(202);
});

// Send a first request, then delete the mock from the mock and send another request.
let response1 = isahc::get(server.url("/test")).unwrap();

// Fetch how often this mock has been called from the server until now
assert_eq!(mock.hits(), 1);

// Delete the mock from the mock server
mock.delete();

let response2 = isahc::get(server.url("/test")).unwrap();

// Assert
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404);
source

pub async fn delete_async(&self)

Deletes this mock from the mock server. This method is the asynchronous equivalent of Mock::delete.

Example
async_std::task::block_on(async {
    // Arrange
    use httpmock::prelude::*;

    let server = MockServer::start_async().await;

    let mut mock = server
      .mock_async(|when, then|{
          when.path("/test");
          then.status(202);
      })
      .await;

    // Send a first request, then delete the mock from the mock and send another request.
    let response1 = isahc::get_async(server.url("/test")).await.unwrap();

    // Fetch how often this mock has been called from the server until now
    assert_eq!(mock.hits_async().await, 1);

    // Delete the mock from the mock server
    mock.delete_async().await;

    let response2 = isahc::get_async(server.url("/test")).await.unwrap();

    // Assert
    assert_eq!(response1.status(), 202);
    assert_eq!(response2.status(), 404);
});
source

pub fn server_address(&self) -> &SocketAddr

Returns the address of the mock server where the associated mock object is store on.

Example
// Arrange: Create mock server and a mock
use httpmock::prelude::*;

let server = MockServer::start();

println!("{}", server.address());
// Will print "127.0.0.1:12345",
// where 12345 is the port that the mock server is running on.

Trait Implementations§

source§

impl<'a> MockExt<'a> for Mock<'a>

source§

fn new(id: usize, mock_server: &'a MockServer) -> Mock<'a>

Creates a new Mock instance that references an already existing mock on a MockServer. This functionality is usually not required. You can use it if for you need to recreate Mock instances . Read more
source§

fn id(&self) -> usize

Returns the ID that the mock was assigned to on the MockServer.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Mock<'a>

§

impl<'a> Send for Mock<'a>

§

impl<'a> Sync for Mock<'a>

§

impl<'a> Unpin for Mock<'a>

§

impl<'a> !UnwindSafe for Mock<'a>

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