39 releases

0.8.0-alpha.1 Sep 1, 2024
0.7.0 Jan 5, 2024
0.7.0-rc.1 Oct 15, 2023
0.6.8 Jul 5, 2023
0.3.3 Oct 3, 2019

#14 in Testing

Download history 79610/week @ 2024-08-20 75664/week @ 2024-08-27 76002/week @ 2024-09-03 76138/week @ 2024-09-10 67401/week @ 2024-09-17 81846/week @ 2024-09-24 83374/week @ 2024-10-01 94638/week @ 2024-10-08 89229/week @ 2024-10-15 85780/week @ 2024-10-22 78884/week @ 2024-10-29 88531/week @ 2024-11-05 91171/week @ 2024-11-12 87704/week @ 2024-11-19 72551/week @ 2024-11-26 80276/week @ 2024-12-03

345,620 downloads per month
Used in 154 crates

MIT license

1.5MB
12K SLoC

httpmock

Simple yet powerful HTTP mocking library for Rust

Build codecov crates.io Mentioned in Awesome Rust Discord

Website · API Reference · Chat · Crate · Report Bug · Request Feature · Changelog · Support this Project

Features

  • Simple, expressive, fluent API.
  • Many built-in helpers for easy request matching (Regex, JSON, serde, cookies, and more).
  • Record and Playback
  • 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.8.0-alpha.1"

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("Привет");
});

// 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 Привет.

When the specified expectations do not match the received request, httpmock provides 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.8.0-alpha.1/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.

Dependencies

~15–32MB
~508K SLoC