Use Hypermocker for async HTTP mocking
mainHypermocker is an HTTP mocking library designed for async programming. It uses an 'anticipate' pattern to handle requests.
By default, Hypermocker will panic if an unanticipated request is made to the server. To prevent this, use the anticipate function to register a specific path.
Unlike traditional mock servers, an anticipated request does not respond immediately. Instead, the request remains in an active state, allowing you to perform other asynchronous tasks before manually triggering the response using request.respond().
tokio_test::block_on(async {
// Bind HTTP server to random, free port.
let server = hypermocker::Server::bind().await;
// Tell the server not to panic on such request.
let mut request = server.anticipate("/api/article").await;
// Do some work, while HTTP request remains in progress.
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
// Send the response.
request.respond(b"hello").await;
});