Struct httpmock::MockRef[][src]

pub struct MockRef<'a> {
    pub id: usize,
    // some fields omitted
}

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::{MockServer, Mock};

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

impl<'a> MockRef<'a>[src]

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

pub fn assert(&self)[src]

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 MockRef::assert_hits or MockRef::hits.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};

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.

pub async fn assert_async(&self)[src]

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 MockRef::assert_hits or MockRef::hits.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};

 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.

pub fn assert_hits(&self, hits: usize)[src]

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 MockRef::assert if you want to assert only one hit.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};
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.

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

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 MockRef::assert_async if you want to assert only one hit.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};

 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.

pub fn hits(&self) -> usize[src]

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::{MockServer, Mock};

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.

pub fn times_called(&self) -> usize[src]

👎 Deprecated since 0.5.0:

Please use ‘hits’ function instead

This method returns the number of times a mock has been called at the mock server. Deprecated, use Mock::hits instead.

pub async fn hits_async(&self) -> usize[src]

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::{MockServer, Mock};

    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.

pub async fn times_called_async(&self) -> usize[src]

👎 Deprecated since 0.5.0:

Please use ‘hits_async’ function instead

This method returns the number of times a mock has been called at the mock server. Deprecated, use Mock::hits instead.

pub fn delete(&mut self)[src]

Deletes the associated mock object from the mock server.

Example

// Arrange
use httpmock::{MockServer, Mock};

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

pub async fn delete_async(&self)[src]

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

Example

async_std::task::block_on(async {
    // Arrange
    use httpmock::{MockServer, Mock};

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

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

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::{MockServer, Mock};

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

impl<'a> MockRefExt<'a> for MockRef<'a>[src]

Auto Trait Implementations

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

impl<'a> Send for MockRef<'a>

impl<'a> Sync for MockRef<'a>

impl<'a> Unpin for MockRef<'a>

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

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]