Network Delay
This section describes functions designed to simulate network issues, such as latency (delay).
delay
Sets a delay for the mock server response.
This method configures the server to wait for a specified duration before sending a response, which can be useful for testing timeout scenarios or asynchronous operations.
Parameters
duration
: The length of the delay as astd::time::Duration
.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Panics
Panics if the specified duration results in a delay that cannot be represented as a 64-bit unsigned integer of milliseconds (more than approximately 584 million years).
Example
Demonstrates setting a 3-second delay for a request to the path /delay
.
use std::time::{SystemTime, Duration};use httpmock::prelude::*;use reqwest::blocking::Client;
// Arrangelet _ = env_logger::try_init();let start_time = SystemTime::now();let three_seconds = Duration::from_secs(3);let server = MockServer::start();
// Configure the mocklet mock = server.mock(|when, then| { when.path("/delay"); then.status(200) .delay(three_seconds);});
// Actlet response = Client::new() .get(server.url("/delay")) .send() .unwrap();
// Assertmock.assert();assert!(start_time.elapsed().unwrap() >= three_seconds);