Struct httpmock::Mock [−][src]
pub struct Mock<'a> {
pub id: usize,
// some fields omitted
}
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
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.
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.
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.
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.
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.
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.
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);
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);
});
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
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
Returns the ID that the mock was assigned to on the MockServer. Read more
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Mock<'a>
impl<'a> !UnwindSafe for Mock<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more