httpmock is an open-source HTTP mocking library for Rust that starts lightweight mock HTTP servers inside a test, and it is aimed at Rust developers who need to test code that talks to HTTP services without standing up the real ones.
What it is
httpmock is a Rust crate published on crates.io and consumed through Cargo as a development dependency. Its core offers a mock server that a test can start, configure with expectations about incoming requests, and then query to confirm the expected calls actually happened. The API is described as simple, expressive and fluent, and the project ships a fully asynchronous core with both synchronous and asynchronous API surfaces, so it fits test suites written in either style. A standalone mode is also available for running the server as its own process rather than inside a Rust test.
The concrete problem it solves is the cost of testing code whose behaviour depends on another HTTP service. Rather than pointing tests at a live third-party endpoint or hand-writing a stub server, a developer declares which requests to expect and which responses to return directly in the test. Matching is built in for the common shapes of HTTP assertions, and failure output is built for reading: mock.assert() fails the test with a detailed description, including a diff between the expected and the actual HTTP request. Record and playback of third-party services, plus forward and proxy mode, extend the same idea to services that are captured once and replayed many times.
Key capabilities
- Fluent mock definition through
server.mock(|when, then| { ... }), with when.method(GET).path("/translate").query_param("word", "hello") on the matching side and then.status(200).header(...).body(...) on the response side.
- Built-in request matchers for Regex, JSON, serde and cookies, alongside support for custom request matchers.
- Record and playback of third-party services, plus forward and proxy mode.
- HTTPS support and simulation of faults and network delays.
- Standalone mode backed by a Docker image published as
httpmock/httpmock on Docker Hub.
- Mock configuration from YAML files, in addition to mocks defined in code.
- Advanced verification and debugging support, including diff generation between actual and expected HTTP request values, with parallel test execution.
Who uses it and how
- Rust teams that add
httpmock under [dev-dependencies] and mock external HTTP APIs directly in unit and integration tests.
- Test suites that depend on parallel test execution, since each test starts its own mock server rather than sharing one.
- CI pipelines that need deterministic answers from a third-party service, using record and playback to capture the service once and replay it without network access.
- Failure-path and resilience testing, where fault and network delay simulation stands in for an unreliable upstream.
- Mixed or non-Rust harnesses that run standalone mode as a separate process through the
httpmock/httpmock Docker image.
Getting started
Add httpmock = "0.9.0" to the [dev-dependencies] section of Cargo.toml, then import httpmock::prelude::* and call MockServer::start(). The crate requires Rust 1.88 or newer, and the standalone Docker image covers running the mock server outside a Rust test process.
How it compares
No list of paid products that httpmock replaces appears in the facts, and no comparable tool is named there either, so on the available evidence it stands alone in this registry.
When to use it — and when not to
Adopt httpmock when the test suite is Rust and depends on HTTP services that should not be called for real; the MIT licence, an actively pushed repository and official documentation at httpmock.rs and docs.rs support that. Anyone on a Rust toolchain older than 1.88 must upgrade first or look elsewhere, and standalone Docker mode adds a container to operate for those who need it. The README itself is thin and defers detail to the website and reference docs, so expect to leave the repository to learn the full API, and note the 37 open issues plus the MIT licence's disclaimer of all warranties.
project readme (upstream, from github) — read inline

Simple yet powerful HTTP mocking library for Rust

Website
·
API Reference
·
Crate
·
Chat on Discord
·
Report Bug
·
Request Feature
·
Support this Project
Features
- Mocks responses from HTTP services
- Simple, expressive, fluent API.
- Many built-in helpers for easy request matching (Regex, JSON, serde, cookies, and more).
- Record and Playback third-party services
- Forward and Proxy Mode
- HTTPS support
- Fault and network delay simulation.
- Custom request matchers.
- Standalone mode with an accompanying Docker image.
- Helpful error messages
- Advanced verification and debugging support (including diff generation between actual and expected HTTP request values)
- Parallel test execution.
- Fully asynchronous core with synchronous and asynchronous APIs.
- Support for mock configuration using YAML files.
Getting Started
Add httpmock to Cargo.toml:
[dev-dependencies]
httpmock = "0.9.0"
You can then use httpmock as follows:
use httpmock::prelude::*;
// Start a lightweight mock server.
let server = MockServer::start();
// Create a mock on the server.
let mock = server.mock(|when, then| {
when.method(GET)
.path("/translate")
.query_param("word", "hello");
then.status(200)
.header("content-type", "text/html; charset=UTF-8")
.body("hola");
});
// Send an HTTP request to the mock server. This simulates your code.
let response = isahc::get(server.url("/translate?word=hello")).unwrap();
// Ensure the specified mock was called exactly one time (or fail with a
// detailed error description).
mock.assert();
// Ensure the mock server did respond as specified.
assert_eq!(response.status(), 200);
The above example will spin up a lightweight HTTP mock server and configure it to respond to all GET requests
to path /translate with query parameter word=hello. The corresponding HTTP response will contain the text body
hola.
When the specified expectations do not match the received request, mock.assert() fails the test with a detailed error description,
including a diff that shows the differences between the expected and actual HTTP requests. Example:
0 of 1 expected requests matched the mock specification.
Here is a comparison with the most similar unmatched request (request number 1):
------------------------------------------------------------
1 : Query Parameter Mismatch
------------------------------------------------------------
Expected:
key [equals] word
value [equals] hello-rustaceans
Received (most similar query parameter):
word=hello
All received query parameter values:
1. word=hello
Matcher: query_param
Docs: https://docs.rs/httpmock/0.9.0/httpmock/struct.When.html#method.query_param
Usage
See the official website for detailed API documentation.
Examples
You can find examples in the
httpmock test directory.
The official website and reference docs also contain a lot of examples.
License
httpmock is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.