mockforge-core 0.3.114

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
//! Record and replay functionality for HTTP requests and responses
//! Implements the Replay and Record parts of the priority chain.

use crate::{Error, RequestFingerprint, Result};
use axum::http::{HeaderMap, Method};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::fs;

/// Recorded request/response pair
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordedRequest {
    /// Request fingerprint
    pub fingerprint: RequestFingerprint,
    /// Request timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Response status code
    pub status_code: u16,
    /// Response headers
    pub response_headers: HashMap<String, String>,
    /// Response body
    pub response_body: String,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Replay handler for serving recorded requests
pub struct ReplayHandler {
    /// Directory containing recorded fixtures
    fixtures_dir: PathBuf,
    /// Whether replay is enabled
    enabled: bool,
}

impl ReplayHandler {
    /// Create a new replay handler
    pub fn new(fixtures_dir: PathBuf, enabled: bool) -> Self {
        Self {
            fixtures_dir,
            enabled,
        }
    }

    /// Get the fixture path for a request fingerprint
    fn get_fixture_path(&self, fingerprint: &RequestFingerprint) -> PathBuf {
        let hash = fingerprint.to_hash();
        let method = fingerprint.method.to_lowercase();
        let path_hash = fingerprint.path.replace(['/', ':'], "_");

        self.fixtures_dir
            .join("http")
            .join(&method)
            .join(&path_hash)
            .join(format!("{}.json", hash))
    }

    /// Check if a fixture exists for the given fingerprint
    pub async fn has_fixture(&self, fingerprint: &RequestFingerprint) -> bool {
        if !self.enabled {
            return false;
        }

        let fixture_path = self.get_fixture_path(fingerprint);
        fixture_path.exists()
    }

    /// Load a recorded request from fixture
    pub async fn load_fixture(
        &self,
        fingerprint: &RequestFingerprint,
    ) -> Result<Option<RecordedRequest>> {
        if !self.enabled {
            return Ok(None);
        }

        let fixture_path = self.get_fixture_path(fingerprint);

        if !fixture_path.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(&fixture_path).await.map_err(|e| {
            Error::io_with_context(
                format!("reading fixture {}", fixture_path.display()),
                e.to_string(),
            )
        })?;

        let recorded_request: RecordedRequest = serde_json::from_str(&content).map_err(|e| {
            Error::config(format!("Failed to parse fixture {}: {}", fixture_path.display(), e))
        })?;

        Ok(Some(recorded_request))
    }
}

/// Record handler for saving requests and responses
pub struct RecordHandler {
    /// Directory to save recorded fixtures
    fixtures_dir: PathBuf,
    /// Whether recording is enabled
    enabled: bool,
    /// Whether to record only GET requests
    record_get_only: bool,
}

impl RecordHandler {
    /// Create a new record handler
    pub fn new(fixtures_dir: PathBuf, enabled: bool, record_get_only: bool) -> Self {
        Self {
            fixtures_dir,
            enabled,
            record_get_only,
        }
    }

    /// Check if a request should be recorded
    pub fn should_record(&self, method: &Method) -> bool {
        if !self.enabled {
            return false;
        }

        if self.record_get_only {
            method == Method::GET
        } else {
            true
        }
    }

    /// Record a request and response
    pub async fn record_request(
        &self,
        fingerprint: &RequestFingerprint,
        status_code: u16,
        response_headers: &HeaderMap,
        response_body: &str,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<()> {
        if !self.should_record(
            &Method::from_bytes(fingerprint.method.as_bytes()).unwrap_or(Method::GET),
        ) {
            return Ok(());
        }

        let fixture_path = self.get_fixture_path(fingerprint);

        // Create directory if it doesn't exist
        if let Some(parent) = fixture_path.parent() {
            fs::create_dir_all(parent).await.map_err(|e| {
                Error::io_with_context(
                    format!("creating directory {}", parent.display()),
                    e.to_string(),
                )
            })?;
        }

        // Convert response headers to HashMap
        let mut response_headers_map = HashMap::new();
        for (key, value) in response_headers.iter() {
            let key_str = key.as_str();
            if let Ok(value_str) = value.to_str() {
                response_headers_map.insert(key_str.to_string(), value_str.to_string());
            }
        }

        let recorded_request = RecordedRequest {
            fingerprint: fingerprint.clone(),
            timestamp: chrono::Utc::now(),
            status_code,
            response_headers: response_headers_map,
            response_body: response_body.to_string(),
            metadata: metadata.unwrap_or_default(),
        };

        let content = serde_json::to_string_pretty(&recorded_request)
            .map_err(|e| Error::internal(format!("Failed to serialize recorded request: {}", e)))?;

        fs::write(&fixture_path, content).await.map_err(|e| {
            Error::io_with_context(
                format!("writing fixture {}", fixture_path.display()),
                e.to_string(),
            )
        })?;

        tracing::info!("Recorded request to {}", fixture_path.display());
        Ok(())
    }

    /// Get the fixture path for a request fingerprint
    fn get_fixture_path(&self, fingerprint: &RequestFingerprint) -> PathBuf {
        let hash = fingerprint.to_hash();
        let method = fingerprint.method.to_lowercase();
        let path_hash = fingerprint.path.replace(['/', ':'], "_");

        self.fixtures_dir
            .join("http")
            .join(&method)
            .join(&path_hash)
            .join(format!("{}.json", hash))
    }
}

/// Combined record/replay handler
pub struct RecordReplayHandler {
    replay_handler: ReplayHandler,
    record_handler: RecordHandler,
}

impl RecordReplayHandler {
    /// Create a new record/replay handler
    pub fn new(
        fixtures_dir: PathBuf,
        replay_enabled: bool,
        record_enabled: bool,
        record_get_only: bool,
    ) -> Self {
        Self {
            replay_handler: ReplayHandler::new(fixtures_dir.clone(), replay_enabled),
            record_handler: RecordHandler::new(fixtures_dir, record_enabled, record_get_only),
        }
    }

    /// Get the replay handler
    pub fn replay_handler(&self) -> &ReplayHandler {
        &self.replay_handler
    }

    /// Get the record handler
    pub fn record_handler(&self) -> &RecordHandler {
        &self.record_handler
    }
}

/// List all available fixtures
pub async fn list_fixtures(fixtures_dir: &Path) -> Result<Vec<RecordedRequest>> {
    let mut fixtures = Vec::new();

    if !fixtures_dir.exists() {
        return Ok(fixtures);
    }

    let http_dir = fixtures_dir.join("http");
    if !http_dir.exists() {
        return Ok(fixtures);
    }

    // Use globwalk to find all JSON files recursively
    let walker = globwalk::GlobWalkerBuilder::from_patterns(&http_dir, &["**/*.json"])
        .build()
        .map_err(|e| Error::io_with_context("building glob walker for fixtures", e.to_string()))?;

    for entry in walker {
        let entry =
            entry.map_err(|e| Error::io_with_context("reading directory entry", e.to_string()))?;
        let path = entry.path();

        if path.is_file() && path.extension().is_some_and(|ext| ext == "json") {
            if let Ok(content) = fs::read_to_string(&path).await {
                if let Ok(recorded_request) = serde_json::from_str::<RecordedRequest>(&content) {
                    fixtures.push(recorded_request);
                }
            }
        }
    }

    // Sort by timestamp (newest first)
    fixtures.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

    Ok(fixtures)
}

/// Clean old fixtures (older than specified days)
pub async fn clean_old_fixtures(fixtures_dir: &Path, older_than_days: u32) -> Result<usize> {
    let cutoff_date = chrono::Utc::now() - chrono::Duration::days(older_than_days as i64);
    let mut cleaned_count = 0;

    if !fixtures_dir.exists() {
        return Ok(0);
    }

    let http_dir = fixtures_dir.join("http");
    if !http_dir.exists() {
        return Ok(0);
    }

    let mut entries = fs::read_dir(&http_dir)
        .await
        .map_err(|e| Error::io_with_context("reading fixtures directory", e.to_string()))?;

    while let Some(entry) = entries
        .next_entry()
        .await
        .map_err(|e| Error::io_with_context("reading directory entry", e.to_string()))?
    {
        let path = entry.path();
        if path.is_file() && path.extension().is_some_and(|ext| ext == "json") {
            if let Ok(content) = fs::read_to_string(&path).await {
                if let Ok(recorded_request) = serde_json::from_str::<RecordedRequest>(&content) {
                    if recorded_request.timestamp < cutoff_date {
                        if let Err(e) = fs::remove_file(&path).await {
                            tracing::warn!(
                                "Failed to remove old fixture {}: {}",
                                path.display(),
                                e
                            );
                        } else {
                            cleaned_count += 1;
                        }
                    }
                }
            }
        }
    }

    Ok(cleaned_count)
}

/// List ready-to-run fixtures (fixtures that can be used for smoke testing)
pub async fn list_ready_fixtures(fixtures_dir: &Path) -> Result<Vec<RecordedRequest>> {
    let mut fixtures = Vec::new();

    if !fixtures_dir.exists() {
        return Ok(fixtures);
    }

    let http_dir = fixtures_dir.join("http");
    if !http_dir.exists() {
        return Ok(fixtures);
    }

    // Use globwalk to find all JSON files recursively
    let walker = globwalk::GlobWalkerBuilder::from_patterns(&http_dir, &["**/*.json"])
        .build()
        .map_err(|e| Error::io_with_context("building glob walker for fixtures", e.to_string()))?;

    for entry in walker {
        let entry =
            entry.map_err(|e| Error::io_with_context("reading directory entry", e.to_string()))?;
        let path = entry.path();

        if path.is_file() && path.extension().is_some_and(|ext| ext == "json") {
            if let Ok(content) = fs::read_to_string(&path).await {
                if let Ok(recorded_request) = serde_json::from_str::<RecordedRequest>(&content) {
                    // Check if this is a ready-to-run fixture (has a smoke_test metadata flag)
                    if recorded_request.metadata.get("smoke_test").is_some_and(|v| v == "true") {
                        fixtures.push(recorded_request);
                    }
                }
            }
        }
    }

    // Sort by timestamp (newest first)
    fixtures.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

    Ok(fixtures)
}

/// Create a smoke test endpoint listing
pub async fn list_smoke_endpoints(fixtures_dir: &Path) -> Result<Vec<(String, String, String)>> {
    let fixtures = list_ready_fixtures(fixtures_dir).await?;
    let mut endpoints = Vec::new();

    for fixture in fixtures {
        let method = fixture.fingerprint.method.clone();
        let path = fixture.fingerprint.path.clone();
        let name = fixture
            .metadata
            .get("name")
            .cloned()
            .unwrap_or_else(|| format!("{} {}", method, path));

        endpoints.push((method, path, name));
    }

    Ok(endpoints)
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::http::Uri;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_record_and_replay() {
        let temp_dir = TempDir::new().unwrap();
        let fixtures_dir = temp_dir.path().to_path_buf();

        let handler = RecordReplayHandler::new(fixtures_dir.clone(), true, true, false);

        // Create a test fingerprint
        let method = Method::GET;
        let uri: Uri = "/api/users?page=1".parse().unwrap();
        let headers = HeaderMap::new();
        let fingerprint = RequestFingerprint::new(method, &uri, &headers, None);

        // Record a request
        let mut response_headers = HeaderMap::new();
        response_headers.insert("content-type", "application/json".parse().unwrap());

        handler
            .record_handler()
            .record_request(&fingerprint, 200, &response_headers, r#"{"users": []}"#, None)
            .await
            .unwrap();

        // Check if fixture exists
        assert!(handler.replay_handler().has_fixture(&fingerprint).await);

        // Load the fixture
        let recorded = handler.replay_handler().load_fixture(&fingerprint).await.unwrap().unwrap();
        assert_eq!(recorded.status_code, 200);
        assert_eq!(recorded.response_body, r#"{"users": []}"#);
    }

    #[tokio::test]
    async fn test_list_fixtures() {
        let temp_dir = TempDir::new().unwrap();
        let fixtures_dir = temp_dir.path().to_path_buf();

        let handler = RecordReplayHandler::new(fixtures_dir.clone(), true, true, false);

        // Record a few requests
        for i in 0..3 {
            let method = Method::GET;
            let uri: Uri = format!("/api/users/{}", i).parse().unwrap();
            let headers = HeaderMap::new();
            let fingerprint = RequestFingerprint::new(method, &uri, &headers, None);

            handler
                .record_handler()
                .record_request(
                    &fingerprint,
                    200,
                    &HeaderMap::new(),
                    &format!(r#"{{"id": {}}}"#, i),
                    None,
                )
                .await
                .unwrap();
        }

        // List fixtures
        let fixtures = list_fixtures(&fixtures_dir).await.unwrap();
        assert_eq!(fixtures.len(), 3);
    }
}