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>
impl<'a> Mock<'a>
pub fn new(id: usize, server: &'a MockServer) -> Self
sourcepub fn assert(&self)
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.
sourcepub async fn assert_async(&self)
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.
sourcepub fn assert_hits(&self, hits: usize)
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.
sourcepub async fn assert_hits_async(&self, hits: usize)
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.
sourcepub fn hits(&self) -> usize
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.
sourcepub async fn hits_async(&self) -> usize
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.
sourcepub fn delete(&mut self)
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);
sourcepub async fn delete_async(&self)
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);
});
sourcepub fn server_address(&self) -> &SocketAddr
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.