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
//! gRPC interceptor tests (authentication, logging, rate limiting)
use api_tests::TestResult;
#[tokio::test]
async fn test_grpc_auth_interceptor() -> TestResult {
// Test authentication interceptor
// Interceptor should validate JWT token in metadata
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// // Request without token should fail
// let request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
//
// let result = client.get_config(request).await;
// assert!(result.is_err());
//
// // Request with valid token should succeed
// let mut request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
// request.metadata_mut().insert("authorization", "Bearer valid_token".parse()?);
//
// let result = client.get_config(request).await;
// assert!(result.is_ok());
Ok(())
}
#[tokio::test]
async fn test_grpc_logging_interceptor() -> TestResult {
// Test logging interceptor
// Should log request/response details
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// let request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
//
// client.get_config(request).await?;
//
// // Verify logs contain request details
// // (In real tests, would check log output)
Ok(())
}
#[tokio::test]
async fn test_grpc_rate_limit_interceptor() -> TestResult {
// Test rate limiting interceptor
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// // Send requests up to rate limit
// for _ in 0..10 {
// let request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
// client.get_config(request).await?;
// }
//
// // Next request should be rate limited
// let request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
//
// let result = client.get_config(request).await;
// assert!(result.is_err());
// assert_eq!(result.unwrap_err().code(), tonic::Code::ResourceExhausted);
Ok(())
}
#[tokio::test]
async fn test_grpc_compression_interceptor() -> TestResult {
// Test compression interceptor
// let channel = Channel::from_static("http://localhost:50051")
// .send_compressed(CompressionEncoding::Gzip)
// .accept_compressed(CompressionEncoding::Gzip)
// .connect()
// .await?;
//
// let mut client = OptimizerServiceClient::new(channel);
//
// let request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
//
// let response = client.get_config(request).await?;
// // Response should be compressed
// assert!(response.metadata().contains_key("grpc-encoding"));
Ok(())
}
#[tokio::test]
async fn test_grpc_timeout_interceptor() -> TestResult {
// Test timeout interceptor
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// let mut request = tonic::Request::new(GetConfigRequest {
// id: "slow_config".to_string(),
// });
//
// // Set timeout in metadata
// request.set_timeout(std::time::Duration::from_millis(10));
//
// let result = client.get_config(request).await;
// assert!(result.is_err());
// assert_eq!(result.unwrap_err().code(), tonic::Code::DeadlineExceeded);
Ok(())
}
#[tokio::test]
async fn test_grpc_retry_interceptor() -> TestResult {
// Test retry interceptor
// Should automatically retry on transient failures
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// let request = tonic::Request::new(GetConfigRequest {
// id: "transient_error".to_string(),
// });
//
// // First few attempts fail, then succeed
// let result = client.get_config(request).await;
// assert!(result.is_ok());
Ok(())
}
#[tokio::test]
async fn test_grpc_metadata_propagation() -> TestResult {
// Test metadata propagation through interceptors
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// let mut request = tonic::Request::new(GetConfigRequest {
// id: "cfg_123".to_string(),
// });
//
// // Add custom metadata
// request.metadata_mut().insert("x-request-id", "req_12345".parse()?);
// request.metadata_mut().insert("x-correlation-id", "corr_67890".parse()?);
//
// let response = client.get_config(request).await?;
//
// // Response should include propagated metadata
// assert!(response.metadata().contains_key("x-request-id"));
Ok(())
}
#[tokio::test]
async fn test_grpc_custom_error_interceptor() -> TestResult {
// Test custom error handling interceptor
// let channel = Channel::from_static("http://localhost:50051").connect().await?;
// let mut client = OptimizerServiceClient::new(channel);
//
// let request = tonic::Request::new(GetConfigRequest {
// id: "error_config".to_string(),
// });
//
// let result = client.get_config(request).await;
// assert!(result.is_err());
//
// let err = result.unwrap_err();
// // Custom error details should be preserved
// let details = err.details();
// assert!(!details.is_empty());
Ok(())
}