wiremock-grpc

Crates.iowiremock-grpc
lib.rswiremock-grpc
version0.4.3
created_at2021-11-01 19:07:13.512165+00
updated_at2026-01-17 17:54:29.379895+00
descriptionMock gRPC server to test your outgoing gRPC requests
homepage
repositoryhttps://github.com/mustakimali/wiremock-grpc-rs
max_upload_size
id475389
size62,119
Mohammad Mustakim Ali (mustakimali)

documentation

https://docs.rs/wiremock-grpc/

README

wiremock grpc

gRPC mocking to test Rust applications.

Crates.io version Download docs.rs docs

Usage

Use the generate_svc! macro to generate a mock server with type-safe RPC methods:

use wiremock_grpc::{generate_svc, MockBuilder};
use tonic::Code;

generate_svc! {
    package hello;
    service Greeter {
        SayHello,
        WeatherInfo,
        // ... add the name of other rpc methods here.
    }
}

#[tokio::test]
async fn test_grpc() {
    // GreeterMockServer is generated by the macro
    let mut server = GreeterMockServer::start_default().await;

    // Use type-safe path_* methods
    server.setup(
        MockBuilder::when()
            .path_say_hello()
            .then()
            .return_status(Code::Ok)
            .return_body(|| HelloReply {
                message: "Hello!".into(),
            }),
    );

    // Connect your client
    let channel = tonic::transport::Channel::from_shared(
        format!("http://[::1]:{}", server.address().port())
    )
    .unwrap()
    .connect()
    .await
    .unwrap();
    let mut client = GreeterClient::new(channel);

    // Make requests
    let response = client
        .say_hello(HelloRequest { name: "World".into() })
        .await
        .unwrap();

    assert_eq!("Hello!", response.into_inner().message);
}

Custom Server Name

You can specify a custom name for the generated server using as:

generate_svc! {
    package hello;
    service Greeter as MyMockServer {
        SayHello,
    }
}

let server = MyMockServer::start_default().await;

Combining with Headers

server.setup(
    MockBuilder::when()
        .path_weather_info()
        .header("x-session-id", "abc123")
        .then()
        .return_body(|| WeatherReply {
            weather: "Sunny".into(),
        }),
);

String-based API

⚠️ Deprecated: Use the type-safe API instead. String-based API is there for backward compatibility but will be removed in the future.

You can also use the string-based API for paths:

server.setup(
    MockBuilder::when()
        .path("/hello.Greeter/SayHello")
        .then()
        .return_body(|| HelloReply {
            message: "Hello!".into(),
        }),
);

What the Macro Generates

The generate_svc! macro generates:

  • {ServiceName}MockServer (or custom name) - the mock server struct with start_default(), start(port), and start_with_addr(addr) methods
  • {ServiceName}TypeSafeExt trait - extension trait for WhenBuilder with path_{method_name} methods

Project Structure

Commit count: 73

cargo fmt