1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*!
# gRPC Mock Utilities
This module provides low-level utilities for mocking gRPC messages and calls. These utilities
give you fine-grained control over gRPC message encoding, decoding, and handling, which is
useful for:
- Testing gRPC handlers directly without setting up a full service
- Implementing custom gRPC client/server logic
- Debugging gRPC message format issues
- Creating mock implementations that work with the wire format
## Core Functions
- [`encode_grpc_request`]: Encodes a message into the gRPC wire format for requests
- [`encode_grpc_response`]: Encodes a message into the gRPC wire format for responses
- [`decode_grpc_message`]: Decodes a message from the gRPC wire format
- [`mock_grpc_call`]: Simulates a gRPC method call with a handler function
- [`create_grpc_uri`]: Creates a URI for a gRPC service method
## Example: Encoding and Decoding
```rust
use tonic_mock::grpc_mock::{encode_grpc_request, decode_grpc_message};
use prost::Message;
// Define a message type
#[derive(Clone, PartialEq, Message)]
pub struct ExampleRequest {
#[prost(string, tag = "1")]
pub id: String,
#[prost(string, tag = "2")]
pub data: String,
}
// Encode a request
let request = ExampleRequest {
id: "request-123".to_string(),
data: "test data".to_string(),
};
let encoded = encode_grpc_request(request.clone());
// Decode the request
let decoded: ExampleRequest = decode_grpc_message(&encoded).unwrap();
assert_eq!(decoded, request);
```
## Example: Mocking a gRPC Call
```rust
use tonic_mock::grpc_mock::mock_grpc_call;
use tonic::Status;
use prost::Message;
// Define message types
#[derive(Clone, PartialEq, Message)]
pub struct GreetRequest {
#[prost(string, tag = "1")]
pub name: String,
}
#[derive(Clone, PartialEq, Message)]
pub struct GreetResponse {
#[prost(string, tag = "1")]
pub message: String,
}
// Mock a gRPC call
let request = GreetRequest { name: "Alice".to_string() };
let response = mock_grpc_call(
"greeter.GreeterService",
"Greet",
request,
|req: GreetRequest| {
Ok(GreetResponse {
message: format!("Hello, {}!", req.name),
})
}
).unwrap();
assert_eq!(response.message, "Hello, Alice!");
```
These utilities are primarily intended for internal use by the [`client_mock`](crate::client_mock)
module, but are exposed for advanced use cases where direct control over the gRPC wire format
is needed.
*/
use ;
use ;
use Message;
use Debug;
use ;
/// Creates a gRPC HTTP request for a specific service method and request message.
///
/// # Arguments
/// * `service_name` - The full gRPC service name, e.g., "package.ServiceName"
/// * `method_name` - The method name to call
/// * `request` - The request message to send
///
/// # Returns
/// The encoded request for use in testing
///
/// # Example
/// ```
/// use tonic_mock::grpc_mock::encode_grpc_request;
/// use tonic_mock::test_utils::TestRequest;
///
/// let request = TestRequest::new("test-id", "test-data");
/// let encoded = encode_grpc_request(request);
/// ```
/// Creates a gRPC HTTP response for a specific response message.
///
/// # Arguments
/// * `response` - The response message to send
///
/// # Returns
/// The encoded response for testing
///
/// # Example
/// ```
/// use tonic_mock::grpc_mock::encode_grpc_response;
/// use tonic_mock::test_utils::TestResponse;
///
/// let response = TestResponse::new(200, "OK");
/// let encoded = encode_grpc_response(response);
/// ```
/// Decodes a gRPC request body into a message.
///
/// # Arguments
/// * `bytes` - The raw bytes of the gRPC request body
///
/// # Returns
/// The decoded message or an error
///
/// # Example
/// ```
/// use tonic_mock::grpc_mock::{encode_grpc_request, decode_grpc_message};
/// use tonic_mock::test_utils::TestRequest;
///
/// // Create a request and encode it
/// let request = TestRequest::new("test-id", "test-data");
/// let encoded = encode_grpc_request(request);
///
/// // Decode the message
/// let decoded: TestRequest = decode_grpc_message(&encoded).unwrap();
/// assert_eq!(decoded.id, "test-id".as_bytes());
/// ```
/// Constructs a URI for a gRPC service method.
///
/// # Arguments
/// * `service_name` - The full gRPC service name, e.g., "package.ServiceName"
/// * `method_name` - The method name to call
///
/// # Returns
/// A URI for the gRPC method
///
/// # Example
/// ```
/// use tonic_mock::grpc_mock::create_grpc_uri;
///
/// let uri = create_grpc_uri("example.TestService", "TestMethod");
/// assert_eq!(uri.path(), "/example.TestService/TestMethod");
/// ```
/// A simple helper function to mock a gRPC service call.
///
/// # Arguments
/// * `service_name` - The full gRPC service name, e.g., "package.ServiceName"
/// * `method_name` - The method name to call
/// * `request` - The request message to send
/// * `handler` - A function that takes a request and returns a response or error
///
/// # Returns
/// The response from the handler
///
/// # Example
/// ```
/// use tonic_mock::grpc_mock::mock_grpc_call;
/// use tonic_mock::test_utils::{TestRequest, TestResponse};
/// use tonic::Status;
///
/// let request = TestRequest::new("test-id", "test-data");
///
/// let response = mock_grpc_call(
/// "example.TestService",
/// "TestMethod",
/// request,
/// |req: TestRequest| {
/// Ok(TestResponse::new(200, format!("Processed: {}", String::from_utf8_lossy(&req.id))))
/// }
/// ).unwrap();
///
/// assert_eq!(response.code, 200);
/// ```