Mockito is a library for generating and delivering HTTP mocks in Rust. You can use it for integration testing or offline work. Mockito runs a local pool of HTTP servers which create, deliver and remove the mocks.
Features
- Supports HTTP1/2
- Runs your tests in parallel
- Comes with a wide range of request matchers (Regex, JSON, query parameters etc.)
- Checks that a mock was called (spy)
- Mocks multiple hosts at the same time
- Exposes sync and async interfaces
- Prints out a colored diff of the last unmatched request in case of errors
- Simple, intuitive API
- An awesome logo
The full documentation is available at https://docs.rs/mockito.
Before upgrading, make sure to check out the changelog.
Getting Started
Add mockito
to your Cargo.toml
and start mocking:
If Mock::assert
fails, a colored diff of the last unmatched request is displayed:
Use matchers to handle requests to the same endpoint in a different way:
Start multiple servers to simulate requests to different hosts:
Write async tests (make sure to use the _async
methods!):
async
Start a stand-alone server on a dedicated port:
Minimum supported Rust toolchain
The current minimum support Rust toolchain is 1.70.0
Contribution Guidelines
- Check the existing issues and pull requests.
- One commit is one feature - consider squashing.
- Format code with
cargo fmt
. - :shipit:
Development
Tests
Run tests:
...or run tests using a different toolchain:
...or run tests while disabling the default features (e.g. the colors):
Code style
Mockito uses rustfmt as a general code style.
Install rustfmt
:
Format code:
Some editors might provide a plugin to format your Rust code automatically.
Linter
Mockito uses clippy and it should be run always on the minimum supported Rust version, in order to ensure backwards compatibility.
Install clippy
:
The linter is always run on the minimum supported Rust version:
Release
Release:
Benchmarks
Install rust nightly
:
Run benchmarks: