Crate httpmock

Crate httpmock 

Source
Expand description

HTTP mocking library that allows you to simulate responses from HTTP-based services.

§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
  • 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"

You can then use httpmock as follows:

use httpmock::prelude::*;
use reqwest::blocking::get;

// Start a lightweight mock server.
let server = MockServer::start();

// Create a mock on the server.
let hello_mock = server.mock(|when, then| {
    when.method(GET)
        .path("/translate")
        .query_param("word", "hello");
    then.status(200)
        .header("content-type", "text/html")
        .body("ohi");
});

// Send an HTTP request to the mock server. This simulates your code.
let response = get(&server.url("/translate?word=hello")).unwrap();

// Ensure the specified mock was called exactly one time (or fail with a detailed error description).
hello_mock.assert();
// Ensure the mock server did respond as specified.
assert_eq!(response.status(), 200);

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.8.0/httpmock/struct.When.html#method.query_param

§Usage

See the official website a 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.

Modules§

common
prelude
server

Structs§

ForwardingRule
Represents a forwarding rule on a MockServer, allowing HTTP requests that meet specific criteria to be redirected to a designated destination. Each rule is uniquely identified by an ID within the server context.
ForwardingRuleBuilder
Mock
Provides a reference to a mock configuration stored on a MockServer. This structure is used for interacting with, monitoring, and managing a specific mock’s lifecycle, such as observing call counts or removing the mock from the server.
MockServer
Represents a mock server designed to simulate HTTP server behaviors for testing purposes. This server intercepts HTTP requests and can be configured to return predetermined responses. It is used extensively in automated tests to validate client behavior without the need for a live server, ensuring that applications behave as expected in controlled environments.
ProxyRule
Provides methods for managing a proxy rule from the server.
ProxyRuleBuilder
Recording
Represents a recording of interactions (requests and responses) on a mock server. This structure is used to capture and store detailed information about the HTTP requests received by the server and the corresponding responses sent back.
RecordingRuleBuilder
Then
Represents the configuration of HTTP responses in a mock server environment.
When
A function that encapsulates one or more When method calls as an abstraction or convenience A function that encapsulates one or more Then method calls as an abstraction or convenience Represents the conditions that an incoming HTTP request must satisfy to be handled by the mock server.

Enums§

Method
Represents an HTTP method.

Traits§

MockExt
The MockExt trait extends the Mock structure with some additional functionality, that is usually not required.

Type Aliases§

Regex
Type alias for regex::Regex.