mockforge_tcp/lib.rs
1//! TCP server mocking for MockForge
2//!
3//! This crate provides TCP server functionality for MockForge, allowing you to mock
4//! raw TCP connections for testing purposes.
5//!
6//! # Example
7//!
8//! ```no_run
9//! use mockforge_tcp::{TcpServer, TcpConfig, TcpSpecRegistry};
10//! use std::sync::Arc;
11//!
12//! #[tokio::main]
13//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
14//! let config = TcpConfig::default();
15//! let registry = Arc::new(TcpSpecRegistry::new());
16//!
17//! let server = TcpServer::new(config, registry)?;
18//! server.start().await?;
19//!
20//! Ok(())
21//! }
22//! ```
23
24mod fixtures;
25mod server;
26mod spec_registry;
27
28pub use fixtures::*;
29pub use server::*;
30pub use spec_registry::*;
31
32/// TCP server configuration
33#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
34pub struct TcpConfig {
35 /// Server port (default: 9999)
36 pub port: u16,
37 /// Host address (default: 0.0.0.0)
38 pub host: String,
39 /// Directory containing fixture files
40 pub fixtures_dir: Option<std::path::PathBuf>,
41 /// Connection timeout in seconds
42 pub timeout_secs: u64,
43 /// Maximum connections
44 pub max_connections: usize,
45 /// Buffer size for reading data (bytes)
46 pub read_buffer_size: usize,
47 /// Buffer size for writing data (bytes)
48 pub write_buffer_size: usize,
49 /// Enable TLS/SSL support
50 pub enable_tls: bool,
51 /// Path to TLS certificate file
52 pub tls_cert_path: Option<std::path::PathBuf>,
53 /// Path to TLS private key file
54 pub tls_key_path: Option<std::path::PathBuf>,
55 /// Echo mode: echo back received data (if no fixture matches)
56 pub echo_mode: bool,
57 /// Delimiter for message boundaries (None = stream mode, Some = frame by delimiter)
58 pub delimiter: Option<Vec<u8>>,
59}
60
61impl Default for TcpConfig {
62 fn default() -> Self {
63 Self {
64 port: 9999,
65 host: "0.0.0.0".to_string(),
66 fixtures_dir: Some(std::path::PathBuf::from("./fixtures/tcp")),
67 timeout_secs: 300,
68 max_connections: 100,
69 read_buffer_size: 8192,
70 write_buffer_size: 8192,
71 enable_tls: false,
72 tls_cert_path: None,
73 tls_key_path: None,
74 echo_mode: true,
75 delimiter: None, // Stream mode by default
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 use mockforge_core::protocol_abstraction::Protocol;
84
85 #[test]
86 fn test_default_config() {
87 let config = TcpConfig::default();
88 assert_eq!(config.port, 9999);
89 assert_eq!(config.host, "0.0.0.0");
90 assert!(config.echo_mode);
91 }
92
93 #[test]
94 fn test_protocol_display() {
95 assert_eq!(Protocol::Tcp.to_string(), "TCP");
96 }
97}