mockforge-core 0.3.115

Shared logic for MockForge - routing, validation, latency, proxy
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Unified middleware implementations for common patterns across protocols

use super::{MiddlewareAction, Protocol, ProtocolMiddleware, ProtocolRequest, ProtocolResponse};
use crate::{request_logger::log_request_global, Result};
use std::time::Instant;

/// Logging middleware that works across all protocols
pub struct LoggingMiddleware {
    /// Middleware name
    name: String,
    /// Whether to log request/response bodies in debug traces
    log_bodies: bool,
}

impl LoggingMiddleware {
    /// Create a new logging middleware
    pub fn new(log_bodies: bool) -> Self {
        Self {
            name: "LoggingMiddleware".to_string(),
            log_bodies,
        }
    }
}

#[async_trait::async_trait]
impl ProtocolMiddleware for LoggingMiddleware {
    fn name(&self) -> &str {
        &self.name
    }

    async fn process_request(&self, request: &mut ProtocolRequest) -> Result<MiddlewareAction> {
        // Add timestamp to request metadata
        let timestamp = chrono::Utc::now().to_rfc3339();
        request.metadata.insert("x-mockforge-request-time".to_string(), timestamp);

        // Store start time for duration calculation
        request.metadata.insert(
            "x-mockforge-request-start".to_string(),
            Instant::now().elapsed().as_millis().to_string(),
        );

        if self.log_bodies {
            tracing::debug!(
                protocol = %request.protocol,
                operation = %request.operation,
                path = %request.path,
                body_size = request.body.as_ref().map(|b| b.len()).unwrap_or(0),
                body = ?request.body.as_deref().and_then(|b| std::str::from_utf8(b).ok()),
                "Processing request through logging middleware (with body)"
            );
        } else {
            tracing::debug!(
                protocol = %request.protocol,
                operation = %request.operation,
                path = %request.path,
                "Processing request through logging middleware"
            );
        }

        Ok(MiddlewareAction::Continue)
    }

    async fn process_response(
        &self,
        request: &ProtocolRequest,
        response: &mut ProtocolResponse,
    ) -> Result<()> {
        let duration_ms = if let Some(start) = request.metadata.get("x-mockforge-request-start") {
            let start: u128 = start.parse().unwrap_or(0);
            Instant::now().elapsed().as_millis() - start
        } else {
            0
        };

        // Create appropriate log entry based on protocol
        let log_entry = match request.protocol {
            Protocol::Http => crate::create_http_log_entry(
                &request.operation,
                &request.path,
                response.status.as_code().unwrap_or(0) as u16,
                duration_ms as u64,
                request.client_ip.clone(),
                request.metadata.get("user-agent").cloned(),
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("Error response: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::Grpc => {
                // Extract service and method from operation (e.g., "greeter.SayHello")
                let parts: Vec<&str> = request.operation.split('.').collect();
                let (service, method) = if parts.len() == 2 {
                    (parts[0], parts[1])
                } else {
                    ("unknown", request.operation.as_str())
                };
                crate::create_grpc_log_entry(
                    service,
                    method,
                    response.status.as_code().unwrap_or(0) as u16,
                    duration_ms as u64,
                    request.client_ip.clone(),
                    request.body.as_ref().map(|b| b.len() as u64).unwrap_or(0),
                    response.body.len() as u64,
                    if !response.status.is_success() {
                        Some(format!("Error response: {:?}", response.status))
                    } else {
                        None
                    },
                )
            }
            Protocol::GraphQL => crate::create_http_log_entry(
                "GraphQL",
                &request.path,
                if response.status.is_success() {
                    200
                } else {
                    400
                },
                duration_ms as u64,
                request.client_ip.clone(),
                request.metadata.get("user-agent").cloned(),
                request.metadata.clone(),
                response.body.len() as u64,
                None,
            ),
            Protocol::WebSocket => crate::create_websocket_log_entry(
                &request.operation,
                &request.path,
                response.status.as_code().unwrap_or(0) as u16,
                request.client_ip.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("Error response: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::Smtp => crate::create_http_log_entry(
                "SMTP",
                &request.path,
                response.status.as_code().unwrap_or(250) as u16,
                duration_ms as u64,
                request.client_ip.clone(),
                None,
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("SMTP Error: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::Mqtt => crate::create_http_log_entry(
                "MQTT",
                &request.topic.clone().unwrap_or_else(|| request.path.clone()),
                if response.status.is_success() {
                    200
                } else {
                    500
                },
                duration_ms as u64,
                request.client_ip.clone(),
                None,
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("MQTT Error: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::Ftp => crate::create_http_log_entry(
                "FTP",
                &request.path,
                response.status.as_code().unwrap_or(226) as u16,
                duration_ms as u64,
                request.client_ip.clone(),
                None,
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("FTP Error: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::Kafka => crate::create_http_log_entry(
                "Kafka",
                &request.topic.clone().unwrap_or_else(|| request.path.clone()),
                response.status.as_code().unwrap_or(0) as u16,
                duration_ms as u64,
                request.client_ip.clone(),
                None,
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("Kafka Error: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::RabbitMq | Protocol::Amqp => crate::create_http_log_entry(
                "AMQP",
                &request.routing_key.clone().unwrap_or_else(|| request.path.clone()),
                response.status.as_code().unwrap_or(200) as u16,
                duration_ms as u64,
                request.client_ip.clone(),
                None,
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("AMQP Error: {:?}", response.status))
                } else {
                    None
                },
            ),
            Protocol::Tcp => crate::create_http_log_entry(
                "TCP",
                &request.path,
                response.status.as_code().unwrap_or(0) as u16,
                duration_ms as u64,
                request.client_ip.clone(),
                None,
                request.metadata.clone(),
                response.body.len() as u64,
                if !response.status.is_success() {
                    Some(format!("TCP Error: {:?}", response.status))
                } else {
                    None
                },
            ),
        };

        // Log to centralized logger
        log_request_global(log_entry).await;

        if self.log_bodies {
            tracing::debug!(
                protocol = %request.protocol,
                operation = %request.operation,
                path = %request.path,
                duration_ms = duration_ms,
                success = response.status.is_success(),
                response_body_size = response.body.len(),
                response_body = ?std::str::from_utf8(&response.body).ok(),
                "Request processed (with body)"
            );
        } else {
            tracing::debug!(
                protocol = %request.protocol,
                operation = %request.operation,
                path = %request.path,
                duration_ms = duration_ms,
                success = response.status.is_success(),
                "Request processed"
            );
        }

        Ok(())
    }

    fn supports_protocol(&self, _protocol: Protocol) -> bool {
        // Logging middleware supports all protocols
        true
    }
}

/// Metrics middleware that collects metrics across all protocols
pub struct MetricsMiddleware {
    /// Middleware name
    name: String,
}

impl MetricsMiddleware {
    /// Create a new metrics middleware
    pub fn new() -> Self {
        Self {
            name: "MetricsMiddleware".to_string(),
        }
    }
}

impl Default for MetricsMiddleware {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl ProtocolMiddleware for MetricsMiddleware {
    fn name(&self) -> &str {
        &self.name
    }

    async fn process_request(&self, request: &mut ProtocolRequest) -> Result<MiddlewareAction> {
        // Store start time for metrics calculation
        request.metadata.insert(
            "x-mockforge-metrics-start".to_string(),
            Instant::now().elapsed().as_millis().to_string(),
        );

        tracing::debug!(
            protocol = %request.protocol,
            operation = %request.operation,
            "Metrics: request started"
        );

        Ok(MiddlewareAction::Continue)
    }

    async fn process_response(
        &self,
        request: &ProtocolRequest,
        response: &mut ProtocolResponse,
    ) -> Result<()> {
        let duration_ms = if let Some(start) = request.metadata.get("x-mockforge-metrics-start") {
            let start: u128 = start.parse().unwrap_or(0);
            Instant::now().elapsed().as_millis() - start
        } else {
            0
        };

        let status_code = response.status.as_code().unwrap_or(0);

        tracing::info!(
            protocol = %request.protocol,
            operation = %request.operation,
            status_code = status_code,
            duration_ms = duration_ms,
            response_size = response.body.len(),
            success = response.status.is_success(),
            "Metrics: request completed"
        );

        Ok(())
    }

    fn supports_protocol(&self, _protocol: Protocol) -> bool {
        // Metrics middleware supports all protocols
        true
    }
}

/// Latency injection middleware for simulating delays
pub struct LatencyMiddleware {
    /// Middleware name
    name: String,
    /// Latency injector
    injector: crate::latency::LatencyInjector,
}

impl LatencyMiddleware {
    /// Create a new latency middleware
    pub fn new(injector: crate::latency::LatencyInjector) -> Self {
        Self {
            name: "LatencyMiddleware".to_string(),
            injector,
        }
    }
}

#[async_trait::async_trait]
impl ProtocolMiddleware for LatencyMiddleware {
    fn name(&self) -> &str {
        &self.name
    }

    async fn process_request(&self, request: &mut ProtocolRequest) -> Result<MiddlewareAction> {
        // Extract tags from request metadata
        let tags: Vec<String> = request
            .metadata
            .get("x-mockforge-tags")
            .map(|t| t.split(',').map(|s| s.trim().to_string()).collect())
            .unwrap_or_default();

        // Inject latency
        self.injector.inject_latency(&tags).await?;

        Ok(MiddlewareAction::Continue)
    }

    async fn process_response(
        &self,
        _request: &ProtocolRequest,
        _response: &mut ProtocolResponse,
    ) -> Result<()> {
        // No post-processing needed for latency
        Ok(())
    }

    fn supports_protocol(&self, _protocol: Protocol) -> bool {
        // Latency middleware supports all protocols
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_logging_middleware_creation() {
        let middleware = LoggingMiddleware::new(true);
        assert_eq!(middleware.name(), "LoggingMiddleware");
        assert!(middleware.supports_protocol(Protocol::Http));
        assert!(middleware.supports_protocol(Protocol::GraphQL));
        assert!(middleware.supports_protocol(Protocol::Grpc));
    }

    #[test]
    fn test_metrics_middleware_creation() {
        let middleware = MetricsMiddleware::new();
        assert_eq!(middleware.name(), "MetricsMiddleware");
        assert!(middleware.supports_protocol(Protocol::Http));
        assert!(middleware.supports_protocol(Protocol::GraphQL));
    }

    #[test]
    fn test_latency_middleware_creation() {
        let injector = crate::latency::LatencyInjector::default();
        let middleware = LatencyMiddleware::new(injector);
        assert_eq!(middleware.name(), "LatencyMiddleware");
        assert!(middleware.supports_protocol(Protocol::Http));
    }

    #[tokio::test]
    async fn test_logging_middleware_process_request() {
        let middleware = LoggingMiddleware::new(false);
        let mut request = ProtocolRequest {
            protocol: Protocol::Http,
            pattern: crate::MessagePattern::RequestResponse,
            operation: "GET".to_string(),
            path: "/test".to_string(),
            topic: None,
            routing_key: None,
            partition: None,
            qos: None,
            metadata: HashMap::new(),
            body: None,
            client_ip: None,
        };

        let result = middleware.process_request(&mut request).await;
        assert!(result.is_ok());
        assert!(request.metadata.contains_key("x-mockforge-request-time"));
    }
}