Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

HTTP & gRPC server mocking for Rust, with native support for streaming

License

NotificationsYou must be signed in to change notification settings

IBM/mocktail

Repository files navigation

default-monochrome

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

Crates.ioDocumentationBookCrates.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. Addmocktail toCargo.toml as a development dependency:

    [dev-dependencies]mocktail ="0.3.0"
  2. Basic usage example:

    use mocktail::prelude::*;#[tokio::test]asyncfntest_example() ->Result<(),Box<dyn std::error::Error>>{// Create a mock setletmut 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 serverletmut server =MockServer::new_http("example").with_mocks(mocks);    server.start().await?;// Create a clientlet client = reqwest::Client::builder().http2_prior_knowledge().build()?;// Send a request that matches the mock created abovelet 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 mocklet 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 matchlet 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 thebook andexamples in themocktail-tests crate.

Examples

Seeexamples in themocktail-tests crate.

Related projects

This crate takes inspiration from other great mocking libraries including:

About

HTTP & gRPC server mocking for Rust, with native support for streaming

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors6

Languages


[8]ページ先頭

©2009-2025 Movatter.jp