
mocktail is a minimal crate for mocking HTTP and gRPC servers in Rust, with native support for streaming.

Table of contents
Features
- Mocks HTTP and gRPC servers
- Mocks defined in Rust using a simple, ergonomic API
- Provides first-class support for streaming
- Supports gRPC unary, client-streaming, server-streaming, and bidirectional-streaming methods
- Match requests to mock responses using built-in matchers or custom matchers
- Fully asynchronous
Getting Started
-
Add mocktail to Cargo.toml as a development dependency:
[dev-dependencies]
mocktail = "0.3.0"
-
Basic usage example:
use mocktail::prelude::*;
#[tokio::test]
async fn test_example() -> Result<(), Box<dyn std::error::Error>> {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/hello").text("world");
then.text("hello world!");
});
let mut server = MockServer::new_http("example").with_mocks(mocks);
server.start().await?;
let client = reqwest::Client::builder().http2_prior_knowledge().build()?;
let response = client
.post(server.url("/hello"))
.body("world")
.send()
.await?;
assert_eq!(response.status(), http::StatusCode::OK);
let body = response.text().await?;
assert_eq!(body, "hello world!");
let response = client.get(server.url("/nope")).send().await?;
assert_eq!(response.status(), http::StatusCode::NOT_FOUND);
server.mock(|when, then| {
when.get().path("/nope");
then.text("yep!");
});
let response = client.get(server.url("/nope")).send().await?;
assert_eq!(response.status(), http::StatusCode::OK);
let body = response.text().await?;
assert_eq!(body, "yep!");
server.mocks.clear();
Ok(())
}
-
See the book and examples in the mocktail-tests crate.
Examples
See examples in the mocktail-tests crate.
Related projects
This crate takes inspiration from other great mocking libraries including: