mockforge_tcp/
fixtures.rs

1//! TCP fixture definitions and loading
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6/// A TCP fixture defining how to handle TCP connections
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct TcpFixture {
9    /// Unique identifier for this fixture
10    pub identifier: String,
11
12    /// Human-readable name
13    pub name: String,
14
15    /// Description of what this fixture does
16    #[serde(default)]
17    pub description: String,
18
19    /// Matching criteria for incoming data
20    pub match_criteria: MatchCriteria,
21
22    /// Response configuration
23    pub response: TcpResponse,
24
25    /// Behavior simulation
26    #[serde(default)]
27    pub behavior: BehaviorConfig,
28}
29
30/// Criteria for matching incoming TCP data
31#[derive(Debug, Clone, Serialize, Deserialize, Default)]
32pub struct MatchCriteria {
33    /// Match by data pattern (hex string, e.g., "48656c6c6f" for "Hello")
34    #[serde(default)]
35    pub data_pattern: Option<String>,
36
37    /// Match by data pattern (regex for text data)
38    #[serde(default)]
39    pub text_pattern: Option<String>,
40
41    /// Match by exact bytes (base64 encoded)
42    #[serde(default)]
43    pub exact_bytes: Option<String>,
44
45    /// Match all (default fixture)
46    #[serde(default)]
47    pub match_all: bool,
48
49    /// Minimum data length required to match
50    #[serde(default)]
51    pub min_length: Option<usize>,
52
53    /// Maximum data length to match
54    #[serde(default)]
55    pub max_length: Option<usize>,
56}
57
58/// TCP response configuration
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct TcpResponse {
61    /// Response data (hex string, base64, or plain text)
62    pub data: String,
63
64    /// Data encoding: "hex", "base64", "text", "file"
65    #[serde(default = "default_encoding")]
66    pub encoding: String,
67
68    /// File path to load response from (if encoding is "file")
69    #[serde(default)]
70    pub file_path: Option<PathBuf>,
71
72    /// Delay before responding (milliseconds)
73    #[serde(default)]
74    pub delay_ms: u64,
75
76    /// Close connection after sending response
77    #[serde(default)]
78    pub close_after_response: bool,
79
80    /// Keep connection open for streaming
81    #[serde(default)]
82    pub keep_alive: bool,
83}
84
85fn default_encoding() -> String {
86    "text".to_string()
87}
88
89/// Behavior simulation configuration
90#[derive(Debug, Clone, Serialize, Deserialize, Default)]
91pub struct BehaviorConfig {
92    /// Simulate connection delay (milliseconds)
93    #[serde(default)]
94    pub connection_delay_ms: u64,
95
96    /// Simulate slow data transfer (bytes per second)
97    #[serde(default)]
98    pub throttle_bytes_per_sec: Option<u64>,
99
100    /// Simulate connection drops (probability 0.0-1.0)
101    #[serde(default)]
102    pub drop_connection_probability: f64,
103
104    /// Simulate partial data (send N bytes then close)
105    #[serde(default)]
106    pub partial_data_bytes: Option<usize>,
107}