
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.2.5-alpha"
```
2. Basic usage example:
```rust
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("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(())
}
```
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)