https://github.com/IBM/mocktail
HTTP & gRPC server mocking for Rust, with native support for streaming
https://github.com/IBM/mocktail
Last synced: 4 months ago
JSON representation
HTTP & gRPC server mocking for Rust, with native support for streaming
- Host: GitHub
- URL: https://github.com/IBM/mocktail
- Owner: IBM
- License: apache-2.0
- Created: 2025-01-06T19:03:02.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-07-11T20:02:52.000Z (4 months ago)
- Last Synced: 2025-07-11T20:23:03.603Z (4 months ago)
- Language: Rust
- Homepage: https://ibm.github.io/mocktail/
- Size: 155 KB
- Stars: 71
- Watchers: 5
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rust - mocktail - HTTP & gRPC server mocking for Rust  (Development tools / Testing)
- fucking-awesome-rust - mocktail - HTTP & gRPC server mocking for Rust  (Development tools / Testing)
README

mocktail is a **minimal** crate for mocking HTTP and gRPC servers in Rust, with native support for streaming.
[](https://crates.io/crates/mocktail)
[](https://docs.rs/mocktail)
[](https://ibm.github.io/mocktail/)
[](LICENSE)
# Table of contents
* [Features](#features)
* [Getting Started](#getting-started)
* [Examples](#examples)
# 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
1. Add `mocktail` to `Cargo.toml` as a development dependency:
```toml
[dev-dependencies]
mocktail = "0.3.0"
```
2. Basic usage example:
```rust
use mocktail::prelude::*;
#[tokio::test]
async fn test_example() -> Result<(), Box> {
// Create a mock set
let mut mocks = MockSet::new();
// Build a mock that returns a "hello world!" response
// to POST requests to the /hello endpoint with the text "world"
// in the body.
mocks.mock(|when, then| {
when.post().path("/hello").text("world");
then.text("hello world!");
});
// Create and start a mock server
let mut server = MockServer::new_http("example").with_mocks(mocks);
server.start().await?;
// Create a client
let client = reqwest::Client::builder().http2_prior_knowledge().build()?;
// Send a request that matches the mock created above
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!");
// Send a request that doesn't match a mock
let response = client.get(server.url("/nope")).send().await?;
assert_eq!(response.status(), http::StatusCode::NOT_FOUND);
// Mocks can also be registered to the server directly
// Register a mock that will match the request above that returned 404
server.mock(|when, then| {
when.get().path("/nope");
then.text("yep!");
});
// Send the request again, it should now match
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!");
// Mocks can be cleared from the server, enabling server reuse
server.mocks.clear();
Ok(())
}
```
3. See the [book](https://ibm.github.io/mocktail/) and [examples](/mocktail-tests/tests/examples) in the `mocktail-tests` crate.
# Examples
See [examples](/mocktail-tests/tests/examples) in the `mocktail-tests` crate.
# Related projects
This crate takes inspiration from other great mocking libraries including:
- [wiremock](https://github.com/wiremock/wiremock)
- [wiremock-rs](https://github.com/LukeMathWalker/wiremock-rs)
- [httpmock](https://github.com/alexliesenfeld/httpmock)