<h1 align="center">wiremock grpc</h1>
<div align="center">
<strong>
gRPC mocking to test Rust applications.
</strong>
</div>
<br />
<div align="center">
<a href="https://crates.io/crates/wiremock-grpc">
<img src="https://img.shields.io/crates/v/wiremock-grpc.svg?style=flat-square"
alt="Crates.io version" />
</a>
<a href="https://crates.io/crates/wiremock-grpc">
<img src="https://img.shields.io/crates/d/wiremock-grpc.svg?style=flat-square"
alt="Download" />
</a>
<a href="https://docs.rs/wiremock-grpc">
<img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square"
alt="docs.rs docs" />
</a>
</div>
<br/>
## Usage
Use the `generate_svc!` macro to generate a mock server with type-safe RPC methods:
```rust
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`:
```rust
generate_svc! {
package hello;
service Greeter as MyMockServer {
SayHello,
}
}
let server = MyMockServer::start_default().await;
```
### Combining with Headers
```rust
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:
```rust
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
* [wiremock-grpc/](wiremock-grpc/) - Main crate published to crates.io
* [wiremock-grpc-macros/](wiremock-grpc-macros/) - Proc macro crate for type-safe RPC methods. You do not need to depend on it directly.