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
/*!
# Test Utilities
This module provides helper types and functions to simplify testing gRPC services
with streaming interfaces. It is enabled by default but can be disabled by setting
`default-features = false` when including the crate.
## Core Components
- [`TestRequest`]: A simple request message type for testing
- [`TestResponse`]: A simple response message type for testing
- [`create_test_messages`]: Create a vector of test messages with sequential IDs
- [`create_stream_response`]: Create a streaming response from a vector of messages
- [`create_stream_response_with_errors`]: Create a streaming response with errors at specified indices
- [`assert_message_eq`]: Assert that a message matches expected values
- [`assert_response_eq`]: Assert that a response matches expected values
## Example Usage
```rust
use tonic::{Request, Response, Status, Code};
use tonic_mock::{streaming_request, process_streaming_response};
use tonic_mock::test_utils::{
TestRequest, TestResponse, create_test_messages,
create_stream_response, assert_response_eq
};
// Create test messages
let messages = create_test_messages(5);
assert_eq!(messages.len(), 5);
// Create a streaming request
let request = streaming_request(messages);
// Call your service (or mock it for this example)
let responses = vec![
TestResponse::new(200, "OK: 0"),
TestResponse::new(200, "OK: 1"),
TestResponse::new(200, "OK: 2"),
TestResponse::new(200, "OK: 3"),
TestResponse::new(200, "OK: 4"),
];
let response = create_stream_response(responses);
// Process the streaming response
process_streaming_response(response, |result, index| {
assert!(result.is_ok());
let response = result.unwrap();
assert_response_eq(&response, 200, format!("OK: {}", index));
}).await;
// Test error handling
let responses = vec![
TestResponse::new(200, "OK: 0"),
TestResponse::new(200, "OK: 1"),
TestResponse::new(200, "OK: 2"),
];
let error_status = Status::new(Code::Internal, "Simulated error");
let response = create_stream_response_with_errors(
responses,
vec![1], // Error at index 1
error_status
);
// Process response with errors
process_streaming_response(response, |result, index| {
match index {
1 => {
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), Code::Internal);
},
_ => {
assert!(result.is_ok());
let response = result.unwrap();
assert_eq!(response.code, 200);
}
}
}).await;
```
These utilities make it easier to test gRPC streaming services by providing
ready-to-use message types and helper functions for common testing patterns.
*/
use crateStreamResponseInner;
use Bytes;
use Message;
use ;
/// Test request message for use in gRPC service tests
///
/// This provides a simple message type that implements the required traits
/// for use with tonic and can be used for testing streaming requests.
/// Test response message for use in gRPC service tests
///
/// This provides a simple response type that can be used for testing
/// streaming responses from gRPC services.
/// Create a vector of test messages with sequential IDs
///
/// This is useful for generating a batch of test messages to use
/// with the streaming_request function.
///
/// # Example
/// ```
/// # use tonic_mock::test_utils::{create_test_messages, TestRequest};
/// let messages = create_test_messages(5);
/// assert_eq!(messages.len(), 5);
/// ```
/// Create a streaming response from a vector of response messages
///
/// This is useful for simulating streaming responses in test code.
///
/// # Example
/// ```
/// # use tonic_mock::test_utils::{create_stream_response, TestResponse};
/// let responses = vec![
/// TestResponse::new(0, "Response 0"),
/// TestResponse::new(1, "Response 1"),
/// ];
/// let stream_response = create_stream_response(responses);
/// ```
/// Create a streaming response with errors at specified indices
///
/// This is useful for testing error handling in code that processes
/// streaming responses.
///
/// # Example
/// ```
/// # use tonic_mock::test_utils::{create_stream_response_with_errors, TestResponse};
/// # use tonic::{Status, Code};
/// let responses = vec![
/// TestResponse::new(0, "Response 0"),
/// TestResponse::new(1, "Response 1"),
/// TestResponse::new(2, "Response 2"),
/// ];
/// let error_status = Status::new(Code::Internal, "Test error");
/// let stream_response = create_stream_response_with_errors(
/// responses,
/// vec![1],
/// error_status
/// );
/// ```
/// Assert that a test message matches the expected ID and data
///
/// This is a convenience function for testing that a message's content
/// matches the expected values.
///
/// # Example
/// ```
/// # use tonic_mock::test_utils::{assert_message_eq, TestRequest};
/// let message = TestRequest::new("test_id", "test_data");
/// assert_message_eq(&message, "test_id", "test_data");
/// ```
/// Assert that a test response matches the expected code and message
///
/// This is a convenience function for testing that a response's content
/// matches the expected values.
///
/// # Example
/// ```
/// # use tonic_mock::test_utils::{assert_response_eq, TestResponse};
/// let response = TestResponse::new(200, "OK");
/// assert_response_eq(&response, 200, "OK");
/// ```