MCPcopy Index your code
hub / github.com/IBM/mocktail

github.com/IBM/mocktail @0.3.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.3.2 ↗ · + Follow
290 symbols 789 edges 30 files 112 documented · 39%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

default-monochrome

mocktail is a minimal crate for mocking HTTP and gRPC servers in Rust, with native support for streaming.

Crates.io Documentation Book Crates.io

Table of contents

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.3.1"

  2. Basic usage example: ```rust use mocktail::prelude::*;

    [tokio::test]

    async fn test_example() -> Result<(), Box> { // Create a mock set let mut mocks = MockSet::new();

    // Build a mock that returns a "hello world!" response
    // to POST requests to the /hello endpoint with the text "world"
    // in the body.
    mocks.mock(|when, then| {
        when.post().path("/hello").text("world");
        then.text("hello world!");
    });
    
    // Create and start a mock server
    let mut server = MockServer::new_http("example").with_mocks(mocks);
    server.start().await?;
    
    // Create a client
    let client = reqwest::Client::builder().http2_prior_knowledge().build()?;
    
    // Send a request that matches the mock created above
    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!");
    
    // Send a request that doesn't match a mock
    let response = client.get(server.url("/nope")).send().await?;
    assert_eq!(response.status(), http::StatusCode::NOT_FOUND);
    
    // Mocks can also be registered to the server directly
    // Register a mock that will match the request above that returned 404
    server.mock(|when, then| {
        when.get().path("/nope");
        then.text("yep!");
    });
    
    // Send the request again, it should now match
    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!");
    
    // Mocks can be cleared from the server, enabling server reuse
    server.mocks.clear();
    
    Ok(())
    

    } ```

  3. See the book and examples in the mocktail-tests crate.

Examples

See examples in the mocktail-tests crate.

Related projects

This crate takes inspiration from other great mocking libraries including: - wiremock - wiremock-rs - httpmock

Extension points exported contracts — how you extend this code

Matcher (Interface)
A matcher. [12 implementers]
mocktail/src/matchers.rs
MessageExt (Interface)
(no doc) [1 implementers]
mocktail/src/ext.rs
AsMatcherEq (Interface)
(no doc) [1 implementers]
mocktail/src/matchers.rs
MatcherEq (Interface)
(no doc) [1 implementers]
mocktail/src/matchers.rs

Core symbols most depended-on inside this repo

push
called by 27
mocktail/src/mock_builder/when.rs
update
called by 26
mocktail/src/mock_builder/then.rs
post
called by 24
mocktail/src/mock_builder/when.rs
url
called by 20
mocktail/src/server.rs
text
called by 20
mocktail/src/mock_builder/when.rs
path
called by 18
mocktail/src/request.rs
mock
called by 17
mocktail/src/server.rs
clone
called by 16
mocktail/src/mock.rs

Shape

Method 212
Function 37
Class 33
Enum 4
Interface 4

Languages

Rust100%

Modules by API surface

mocktail/src/matchers.rs36 symbols
mocktail/src/mock_builder/when.rs31 symbols
mocktail/src/mock_builder/then.rs30 symbols
mocktail/src/server.rs26 symbols
mocktail/src/status.rs21 symbols
mocktail/src/headers.rs21 symbols
mocktail/src/body.rs18 symbols
mocktail/src/request.rs17 symbols
mocktail/src/mock_set.rs16 symbols
mocktail/src/body/buf_list.rs16 symbols
mocktail/src/mock.rs14 symbols
mocktail/src/response.rs12 symbols

For agents

$ claude mcp add mocktail \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact