pub struct Mock<'a> {
pub id: usize,
/* private fields */
}Expand description
Provides a reference to a mock configuration stored on a MockServer. This structure is used for interacting with, monitoring, and managing a specific mock’s lifecycle, such as observing call counts or removing the mock from the server.
This reference allows you to control and verify the behavior of the server in response to incoming HTTP requests that match the mock criteria.
§Example
Demonstrates how to create and manipulate a mock on the server. This includes monitoring its usage and effectively managing its lifecycle by removing it when necessary.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange
let server = MockServer::start();
// Create and configure a mock
let mut mock = server.mock(|when, then| {
when.path("/test");
then.status(202);
});
// Act by sending a request and verifying the mock's hit count
let response1 = get(&server.url("/test")).unwrap();
assert_eq!(mock.hits(), 1); // Verify the mock was triggered
// Remove the mock and test the server's response to the same path again
mock.delete();
let response2 = get(&server.url("/test")).unwrap();
// Assert
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404); // Expect a 404 status after the mock is deletedFields§
§id: usizeImplementations§
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)
Verifies that the mock server received exactly one HTTP request matching all specified request conditions for this mock. This method is useful for confirming that a particular operation interacts with the server as expected in test scenarios.
Attention: To assert receipt of multiple requests, use Mock::assert_hits or Mock::hits methods instead.
§Example
Demonstrates creating a mock to match a specific request path, sending a request to that path, and then verifying that exactly one such request was received.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange: Start a mock server and set up a mock
let server = MockServer::start();
let mut mock = server.mock(|when, then| {
when.path("/hits");
then.status(200);
});
// Act: Send a request to the specified path
get(&server.url("/hits")).unwrap();
// Assert: Check that the server received exactly one request that matched the mock
mock.assert();§Panics
This method will panic if the mock server did not receive exactly one matching request or if there are issues with the mock server’s availability.
Sourcepub async fn assert_async(&self)
pub async fn assert_async(&self)
Asynchronously verifies that the mock server received exactly one HTTP request matching all specified request conditions for this mock. This method is suited for asynchronous testing environments where operations against the mock server occur non-blockingly.
Attention: To assert the receipt of multiple requests asynchronously, consider using Mock::assert_hits_async or Mock::hits_async.
§Example
Demonstrates setting up an asynchronous mock, sending a request, and verifying that exactly one such request was received.
use httpmock::prelude::*;
use reqwest::get;
use syn::token;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Arrange: Start a mock server asynchronously and set up a 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 to the specified path asynchronously
get(&server.url("/hits")).await.unwrap();
// Assert: Check that the server received exactly one request that matched the mock
mock.assert_async().await;
});§Panics
This method will panic if the mock server did not receive exactly one matching request or if there are issues with the mock server’s availability.
Sourcepub fn assert_hits(&self, hits: usize)
👎Deprecated since 0.8.0: please use assert_calls instead
pub fn assert_hits(&self, hits: usize)
assert_calls insteadVerifies that the mock server received the specified number of HTTP requests matching all the request conditions defined for this mock.
This method is useful for confirming that a series of operations interact with the server as expected within test scenarios, especially when specific interaction counts are significant.
Attention: Use Mock::assert for the common case of asserting exactly one hit.
§Example
Demonstrates creating a mock, sending multiple requests, and verifying the number of received requests matches expectations.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange: Start a mock server and configure a mock
let server = MockServer::start();
let mut mock = server.mock(|when, then| {
when.path("/hits");
then.status(200);
});
// Act: Send multiple requests to the configured path
get(&server.url("/hits")).unwrap();
get(&server.url("/hits")).unwrap();
// Assert: Check that the server received exactly two requests that matched the mock
mock.assert_hits(2);§Panics
This method will panic if the actual number of hits differs from the specified hits, or if
there are issues with the mock server’s availability.
Sourcepub fn assert_calls(&self, count: usize)
pub fn assert_calls(&self, count: usize)
Verifies that the mock server received the specified number of HTTP requests matching all the request conditions defined for this mock.
This method is useful for confirming that a series of operations interact with the server as expected within test scenarios, especially when specific interaction counts are significant.
Attention: Use Mock::assert for the common case of asserting exactly one hit.
§Example
Demonstrates creating a mock, sending multiple requests, and verifying the number of received requests matches expectations.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange: Start a mock server and configure a mock
let server = MockServer::start();
let mut mock = server.mock(|when, then| {
when.path("/hits");
then.status(200);
});
// Act: Send multiple requests to the configured path
get(&server.url("/hits")).unwrap();
get(&server.url("/hits")).unwrap();
// Assert: Check that the server received exactly two requests that matched the mock
mock.assert_calls(2);§Panics
This method will panic if the actual number of hits differs from the specified hits, or if
there are issues with the mock server’s availability.
Sourcepub async fn assert_hits_async(&self, hits: usize)
👎Deprecated since 0.8.0: please use assert_calls_async instead
pub async fn assert_hits_async(&self, hits: usize)
assert_calls_async insteadAsynchronously verifies that the mock server received the specified number of HTTP requests matching all defined request conditions for this mock.
This method supports asynchronous testing environments, enabling non-blocking verification of multiple interactions with the mock server. It’s particularly useful when exact counts of interactions are critical for test assertions.
Attention: For asserting exactly one request asynchronously, use Mock::assert_async for simpler syntax.
§Example
Demonstrates setting up an asynchronous mock, sending multiple requests, and verifying the number of requests received matches expectations.
use httpmock::prelude::*;
use reqwest::get;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Arrange: Start a mock server asynchronously and set up a mock
let server = MockServer::start_async().await;
let mut mock = server.mock_async(|when, then| {
when.path("/hits");
then.status(200);
}).await;
// Act: Send multiple asynchronous requests to the configured path
get(&server.url("/hits")).await.unwrap();
get(&server.url("/hits")).await.unwrap();
// Assert: Check that the server received exactly two requests that matched the mock
mock.assert_hits_async(2).await;
});§Panics
This method will panic if the actual number of hits differs from the specified hits, or if
there are issues with the mock server’s availability.
Sourcepub async fn assert_calls_async(&self, hits: usize)
pub async fn assert_calls_async(&self, hits: usize)
Asynchronously verifies that the mock server received the specified number of HTTP requests matching all defined request conditions for this mock.
This method supports asynchronous testing environments, enabling non-blocking verification of multiple interactions with the mock server. It’s particularly useful when exact counts of interactions are critical for test assertions.
Attention: For asserting exactly one request asynchronously, use Mock::assert_async for simpler syntax.
§Example
Demonstrates setting up an asynchronous mock, sending multiple requests, and verifying the number of requests received matches expectations.
use httpmock::prelude::*;
use reqwest::get;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Arrange: Start a mock server asynchronously and set up a mock
let server = MockServer::start_async().await;
let mut mock = server.mock_async(|when, then| {
when.path("/hits");
then.status(200);
}).await;
// Act: Send multiple asynchronous requests to the configured path
get(&server.url("/hits")).await.unwrap();
get(&server.url("/hits")).await.unwrap();
// Assert: Check that the server received exactly two requests that matched the mock
mock.assert_calls_async(2).await;
});§Panics
This method will panic if the actual number of hits differs from the specified hits, or if
there are issues with the mock server’s availability.
Sourcepub fn hits(&self) -> usize
👎Deprecated since 0.8.0: please use calls instead
pub fn hits(&self) -> usize
calls insteadReturns the number of times the specified mock has been triggered on the mock server.
This method is useful for verifying that a mock has been invoked the expected number of times, allowing for precise control and assertion of interactions within test scenarios.
§Example
Demonstrates setting up a mock, sending a request, and then verifying that the mock was triggered exactly once.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange: Start a mock server and create a mock
let server = MockServer::start();
let mut mock = server.mock(|when, then| {
when.path("/hits");
then.status(200);
});
// Act: Send a request to the mock path
get(&server.url("/hits")).unwrap();
// Assert: Verify the mock was called once
assert_eq!(1, mock.hits());§Panics
This method will panic if there are issues accessing the mock server or retrieving the hit count.
Sourcepub fn calls(&self) -> usize
pub fn calls(&self) -> usize
Returns the number of times the specified mock has been triggered on the mock server.
This method is useful for verifying that a mock has been invoked the expected number of times, allowing for precise control and assertion of interactions within test scenarios.
§Example
Demonstrates setting up a mock, sending a request, and then verifying that the mock was triggered exactly once.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange: Start a mock server and create a mock
let server = MockServer::start();
let mut mock = server.mock(|when, then| {
when.path("/hits");
then.status(200);
});
// Act: Send a request to the mock path
get(&server.url("/hits")).unwrap();
// Assert: Verify the mock was called once
assert_eq!(1, mock.calls());§Panics
This method will panic if there are issues accessing the mock server or retrieving the hit count.
Sourcepub async fn hits_async(&self) -> usize
👎Deprecated since 0.8.0: please use calls_async instead
pub async fn hits_async(&self) -> usize
calls_async insteadAsynchronously returns the number of times the specified mock has been triggered on the mock server.
This method is particularly useful in asynchronous test setups where non-blocking verifications are needed to confirm that a mock has been invoked the expected number of times. It ensures test assertions align with asynchronous operations.
§Example
Demonstrates setting up an asynchronous mock, sending a request, and then verifying the number of times the mock was triggered.
use httpmock::prelude::*;
use reqwest::get;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Arrange: Start an asynchronous mock server and create a mock
let server = MockServer::start_async().await;
let mut mock = server
.mock_async(|when, then| {
when.path("/hits");
then.status(200);
})
.await;
// Act: Send an asynchronous request to the mock path
get(&server.url("/hits")).await.unwrap();
// Assert: Verify the mock was called once
assert_eq!(1, mock.hits_async().await);
});§Panics
This method will panic if there are issues accessing the mock server or retrieving the hit count asynchronously.
Sourcepub async fn calls_async(&self) -> usize
pub async fn calls_async(&self) -> usize
Asynchronously returns the number of times the specified mock has been triggered on the mock server.
This method is particularly useful in asynchronous test setups where non-blocking verifications are needed to confirm that a mock has been invoked the expected number of times. It ensures test assertions align with asynchronous operations.
§Example
Demonstrates setting up an asynchronous mock, sending a request, and then verifying the number of times the mock was triggered.
use httpmock::prelude::*;
use reqwest::get;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Arrange: Start an asynchronous mock server and create a mock
let server = MockServer::start_async().await;
let mut mock = server
.mock_async(|when, then| {
when.path("/hits");
then.status(200);
})
.await;
// Act: Send an asynchronous request to the mock path
get(&server.url("/hits")).await.unwrap();
// Assert: Verify the mock was called once
assert_eq!(1, mock.calls_async().await);
});§Panics
This method will panic if there are issues accessing the mock server or retrieving the hit count asynchronously.
Sourcepub fn delete(&mut self)
pub fn delete(&mut self)
Removes the specified mock from the mock server. This operation is useful for testing scenarios where the mock should no longer intercept requests, effectively simulating an environment where certain endpoints may go offline or change behavior dynamically during testing.
§Example
Demonstrates creating a mock, verifying its behavior with a request, then deleting the mock and verifying that subsequent requests are not intercepted.
use httpmock::prelude::*;
use reqwest::blocking::get;
// Arrange: Start a mock server and set up a mock
let server = MockServer::start();
let mut mock = server.mock(|when, then| {
when.path("/test");
then.status(202);
});
// Act: Send a request to the mock and verify its behavior
let response1 = get(&server.url("/test")).unwrap();
assert_eq!(mock.hits(), 1); // Verify the mock was called once
// Delete the mock from the server
mock.delete();
// Send another request and verify the response now that the mock is deleted
let response2 = get(&server.url("/test")).unwrap();
// Assert: The first response should be 202 as the mock was active, the second should be 404
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404);This method ensures that the mock is completely removed, and any subsequent requests to the same path will not be intercepted by this mock, typically resulting in a 404 Not Found response unless another active mock matches the request.
Sourcepub async fn delete_async(&self)
pub async fn delete_async(&self)
Asynchronously deletes this mock from the mock server. This method is the asynchronous equivalent of Mock::delete and is suited for use in asynchronous testing environments where non-blocking operations are preferred.
§Example
Demonstrates creating an asynchronous mock, sending a request to verify its behavior, then deleting the mock and verifying that subsequent requests are not intercepted.
use httpmock::prelude::*;
use reqwest::get;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Arrange: Start an asynchronous mock server and create a mock
let server = MockServer::start_async().await;
let mut mock = server
.mock_async(|when, then| {
when.path("/test");
then.status(202);
})
.await;
// Act: Send a request to the mock path and verify the mock's hit count
let response1 = get(&server.url("/test")).await.unwrap();
assert_eq!(mock.hits_async().await, 1); // Verify the mock was called once
// Delete the mock asynchronously from the server
mock.delete_async().await;
// Send another request and check the response now that the mock is deleted
let response2 = get(&server.url("/test")).await.unwrap();
// Assert: The first response should be 202 as the mock was active, the second should be 404
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404);
});This method ensures that the mock is completely removed asynchronously, and any subsequent requests to the same path will not be intercepted by this mock, typically resulting in a 404 Not Found response unless another active mock matches the request.
Sourcepub fn server_address(&self) -> &SocketAddr
pub fn server_address(&self) -> &SocketAddr
Returns the network address of the mock server where the associated mock object is stored.
This method provides access to the IP address and port number of the mock server, useful for connecting to it in tests or displaying its address in debugging output.
§Example
Demonstrates how to retrieve and print the address of a mock server after it has been started.
use httpmock::prelude::*;
use std::net::SocketAddr;
// Arrange: Start a mock server
let server = MockServer::start();
// Print the address of the server
let address: &SocketAddr = server.address();
println!("{}", address);
// Output will be something like "127.0.0.1:12345", where 12345 is the port the server is running on.