mockforge-http 0.3.116

HTTP/REST protocol support for MockForge
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Overrides engine with templating helpers.
use globwalk::GlobWalkerBuilder;
use json_patch::{patch, AddOperation, PatchOperation, RemoveOperation, ReplaceOperation};
use mockforge_core::conditions::{evaluate_condition, ConditionContext, ConditionError};
use mockforge_core::templating::expand_tokens as core_expand_tokens;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug, Clone, Deserialize)]
pub struct OverrideRule {
    pub targets: Vec<String>, // "operation:opId", "tag:Tag", "regex:pattern", or "path:pattern"
    pub patch: Vec<PatchOp>,
    pub when: Option<String>,
    /// Override mode: "replace" (default) or "merge"
    #[serde(default = "default_mode")]
    pub mode: OverrideMode,
    /// Whether to apply post-templating expansion after patching
    #[serde(default = "default_post_templating")]
    pub post_templating: bool,
}

/// Override mode for applying patches
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub enum OverrideMode {
    /// Replace values (default JSON patch behavior)
    #[serde(rename = "replace")]
    Replace,
    /// Merge objects and arrays instead of replacing
    #[serde(rename = "merge")]
    Merge,
}

fn default_mode() -> OverrideMode {
    OverrideMode::Replace
}

fn default_post_templating() -> bool {
    false
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "op")]
pub enum PatchOp {
    #[serde(rename = "add")]
    Add { path: String, value: Value },
    #[serde(rename = "replace")]
    Replace { path: String, value: Value },
    #[serde(rename = "remove")]
    Remove { path: String },
}

#[derive(Debug, Default, Clone)]
pub struct Overrides {
    rules: Vec<OverrideRule>,
    /// Compiled regex patterns for performance
    regex_cache: HashMap<String, Regex>,
}

impl Overrides {
    /// Get the loaded override rules
    pub fn rules(&self) -> &[OverrideRule] {
        &self.rules
    }

    /// Load overrides from glob patterns, with support for MOCKFORGE_HTTP_OVERRIDES_GLOB
    pub async fn load_from_globs(patterns: &[&str]) -> anyhow::Result<Self> {
        // Check for environment variable override
        let patterns = if let Ok(env_patterns) = std::env::var("MOCKFORGE_HTTP_OVERRIDES_GLOB") {
            env_patterns.split(',').map(|s| s.trim()).collect::<Vec<_>>()
        } else {
            patterns.iter().map(|s| *s).collect::<Vec<_>>()
        };

        let mut rules = Vec::new();
        let mut regex_cache = HashMap::new();

        for pat in patterns {
            for entry in GlobWalkerBuilder::from_patterns(".", &[pat]).build()? {
                let path = entry?.path().to_path_buf();
                if path.extension().map(|e| e == "yaml" || e == "yml").unwrap_or(false) {
                    let text = tokio::fs::read_to_string(&path).await?;
                    let mut file_rules: Vec<OverrideRule> = serde_yaml::from_str(&text)?;

                    for r in file_rules.iter_mut() {
                        // Pre-expand templating tokens in patch values
                        for op in r.patch.iter_mut() {
                            match op {
                                PatchOp::Add { value, .. } | PatchOp::Replace { value, .. } => {
                                    *value = core_expand_tokens(value);
                                }
                                _ => {}
                            }
                        }

                        // Compile regex patterns for performance
                        for target in &r.targets {
                            if let Some(pattern) = target.strip_prefix("regex:").or_else(|| target.strip_prefix("path:")) {
                                if !regex_cache.contains_key(pattern) {
                                    if let Ok(regex) = Regex::new(pattern) {
                                        regex_cache.insert(pattern.to_string(), regex);
                                    }
                                }
                            }
                        }
                    }
                    rules.extend(file_rules);
                }
            }
        }
        Ok(Overrides { rules, regex_cache })
    }

    pub fn apply(&self, operation_id: &str, tags: &[String], path: &str, body: &mut Value) {
        self.apply_with_context(operation_id, tags, path, body, &ConditionContext::new())
    }

    /// Apply overrides with condition evaluation
    pub fn apply_with_context(&self, operation_id: &str, tags: &[String], path: &str, body: &mut Value, context: &ConditionContext) {
        for r in &self.rules {
            if !matches_target(&r.targets, operation_id, tags, path, &self.regex_cache) {
                continue;
            }

            // Evaluate condition if present
            if let Some(ref condition) = r.when {
                match evaluate_condition(condition, context) {
                    Ok(true) => {
                        // Condition passed, continue with patch application
                    }
                    Ok(false) => {
                        // Condition failed, skip this rule
                        continue;
                    }
                    Err(e) => {
                        // Log condition evaluation error but don't fail the entire override process
                        tracing::warn!("Failed to evaluate condition '{}': {}", condition, e);
                        continue;
                    }
                }
            }

            // Apply patches based on mode
            match r.mode {
                OverrideMode::Replace => {
                    for op in &r.patch {
                        apply_patch(body, op);
                    }
                }
                OverrideMode::Merge => {
                    for op in &r.patch {
                        apply_merge_patch(body, op);
                    }
                }
            }

            // Apply post-templating expansion if enabled
            if r.post_templating {
                *body = core_expand_tokens(body);
            }
        }
    }
}

fn matches_target(
    targets: &[String],
    op_id: &str,
    tags: &[String],
    path: &str,
    regex_cache: &HashMap<String, Regex>
) -> bool {
    targets.iter().any(|t| {
        if let Some(rest) = t.strip_prefix("operation:") {
            rest == op_id
        } else if let Some(rest) = t.strip_prefix("tag:") {
            tags.iter().any(|g| g == rest)
        } else if let Some(pattern) = t.strip_prefix("regex:") {
            // Match against operation ID
            regex_cache.get(pattern).map_or(false, |re| re.is_match(op_id))
        } else if let Some(pattern) = t.strip_prefix("path:") {
            // Match against request path
            regex_cache.get(pattern).map_or(false, |re| re.is_match(path))
        } else {
            false
        }
    })
}

fn apply_patch(doc: &mut Value, op: &PatchOp) {
    let ops = match op {
        PatchOp::Add { path, value } => vec![PatchOperation::Add(AddOperation {
            path: path.parse().unwrap_or_else(|_| json_patch::jsonptr::PointerBuf::new()),
            value: value.clone(),
        })],
        PatchOp::Replace { path, value } => vec![PatchOperation::Replace(ReplaceOperation {
            path: path.parse().unwrap_or_else(|_| json_patch::jsonptr::PointerBuf::new()),
            value: value.clone(),
        })],
        PatchOp::Remove { path } => vec![PatchOperation::Remove(RemoveOperation {
            path: path.parse().unwrap_or_else(|_| json_patch::jsonptr::PointerBuf::new()),
        })],
    };

    // `Patch` is just a Vec<PatchOperation>
    let _ = patch(doc, &ops);
}

/// Apply merge patch operation (deep merge for objects, append for arrays)
fn apply_merge_patch(doc: &mut Value, op: &PatchOp) {
    match op {
        PatchOp::Add { path, value } => {
            if let Ok(pointer) = path.parse::<json_patch::jsonptr::PointerBuf>() {
                if let Some(target) = pointer.get_mut(doc) {
                    match (target, value) {
                        (Value::Object(target_obj), Value::Object(value_obj)) => {
                            // Deep merge objects
                            for (key, val) in value_obj {
                                target_obj.insert(key.clone(), val.clone());
                            }
                        }
                        (Value::Array(target_arr), Value::Array(value_arr)) => {
                            // Append to arrays
                            target_arr.extend(value_arr.iter().cloned());
                        }
                        (target, value) => {
                            // Replace for other types
                            *target = value.clone();
                        }
                    }
                } else {
                    // Path doesn't exist, create it
                    let _ = pointer.set(doc, value.clone());
                }
            }
        }
        PatchOp::Replace { path, value } => {
            if let Ok(pointer) = path.parse::<json_patch::jsonptr::PointerBuf>() {
                if let Some(target) = pointer.get_mut(doc) {
                    match (target, value) {
                        (Value::Object(target_obj), Value::Object(value_obj)) => {
                            // Deep merge objects
                            for (key, val) in value_obj {
                                target_obj.insert(key.clone(), val.clone());
                            }
                        }
                        (Value::Array(target_arr), Value::Array(value_arr)) => {
                            // Replace array contents
                            target_arr.clear();
                            target_arr.extend(value_arr.iter().cloned());
                        }
                        (target, value) => {
                            // Replace for other types
                            *target = value.clone();
                        }
                    }
                } else {
                    // Path doesn't exist, create it
                    let _ = pointer.set(doc, value.clone());
                }
            }
        }
        PatchOp::Remove { path } => {
            if let Ok(pointer) = path.parse::<json_patch::jsonptr::PointerBuf>() {
                let _ = pointer.remove(doc);
            }
        }
    }
}

// templating moved to mockforge-core::templating

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use tempfile::TempDir;
    use tokio::fs;

    #[test]
    fn test_override_mode_default() {
        let mode = default_mode();
        assert_eq!(mode, OverrideMode::Replace);
    }

    #[test]
    fn test_post_templating_default() {
        assert!(!default_post_templating());
    }

    #[test]
    fn test_patch_op_serialization() {
        let add_op = PatchOp::Add {
            path: "/name".to_string(),
            value: json!("John"),
        };

        let serialized = serde_json::to_string(&add_op).unwrap();
        assert!(serialized.contains("\"op\":\"add\""));
        assert!(serialized.contains("\"path\":\"/name\""));
    }

    #[tokio::test]
    async fn test_overrides_default() {
        let overrides = Overrides::default();
        assert_eq!(overrides.rules.len(), 0);
        assert_eq!(overrides.regex_cache.len(), 0);
    }

    #[tokio::test]
    async fn test_load_from_globs_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());

        let result = Overrides::load_from_globs(&[&pattern]).await;
        assert!(result.is_ok());
        let overrides = result.unwrap();
        assert_eq!(overrides.rules.len(), 0);
    }

    #[tokio::test]
    async fn test_load_from_globs_with_yaml_file() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_path = temp_dir.path().join("overrides.yaml");

        let yaml_content = r#"
- targets:
    - "operation:getUser"
  patch:
    - op: replace
      path: "/name"
      value: "Jane Doe"
"#;

        fs::write(&yaml_path, yaml_content).await.unwrap();

        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());
        let result = Overrides::load_from_globs(&[&pattern]).await;

        assert!(result.is_ok());
        let overrides = result.unwrap();
        assert_eq!(overrides.rules.len(), 1);
        assert_eq!(overrides.rules[0].targets[0], "operation:getUser");
    }

    #[tokio::test]
    async fn test_load_from_globs_with_regex_pattern() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_path = temp_dir.path().join("overrides.yaml");

        let yaml_content = r#"
- targets:
    - "regex:get.*"
  patch:
    - op: add
      path: "/timestamp"
      value: "2024-01-01"
"#;

        fs::write(&yaml_path, yaml_content).await.unwrap();

        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());
        let result = Overrides::load_from_globs(&[&pattern]).await;

        assert!(result.is_ok());
        let overrides = result.unwrap();
        assert_eq!(overrides.rules.len(), 1);
        // Regex should be cached
        assert!(overrides.regex_cache.contains_key("get.*"));
    }

    #[test]
    fn test_matches_target_operation() {
        let targets = vec!["operation:getUser".to_string()];
        let regex_cache = HashMap::new();

        assert!(matches_target(&targets, "getUser", &[], "/users", &regex_cache));
        assert!(!matches_target(&targets, "createUser", &[], "/users", &regex_cache));
    }

    #[test]
    fn test_matches_target_tag() {
        let targets = vec!["tag:admin".to_string()];
        let regex_cache = HashMap::new();
        let tags = vec!["admin".to_string(), "users".to_string()];

        assert!(matches_target(&targets, "getUser", &tags, "/users", &regex_cache));

        let tags_no_match = vec!["users".to_string()];
        assert!(!matches_target(&targets, "getUser", &tags_no_match, "/users", &regex_cache));
    }

    #[test]
    fn test_matches_target_regex() {
        let targets = vec!["regex:get.*".to_string()];
        let mut regex_cache = HashMap::new();
        regex_cache.insert("get.*".to_string(), Regex::new("get.*").unwrap());

        assert!(matches_target(&targets, "getUser", &[], "/users", &regex_cache));
        assert!(matches_target(&targets, "getUserById", &[], "/users", &regex_cache));
        assert!(!matches_target(&targets, "createUser", &[], "/users", &regex_cache));
    }

    #[test]
    fn test_matches_target_path() {
        let targets = vec!["path:/users/.*".to_string()];
        let mut regex_cache = HashMap::new();
        regex_cache.insert("/users/.*".to_string(), Regex::new("/users/.*").unwrap());

        assert!(matches_target(&targets, "getUser", &[], "/users/123", &regex_cache));
        assert!(!matches_target(&targets, "getUser", &[], "/posts/456", &regex_cache));
    }

    #[test]
    fn test_apply_patch_add() {
        let mut doc = json!({"name": "John"});
        let op = PatchOp::Add {
            path: "/age".to_string(),
            value: json!(30),
        };

        apply_patch(&mut doc, &op);
        assert_eq!(doc["age"], 30);
    }

    #[test]
    fn test_apply_patch_replace() {
        let mut doc = json!({"name": "John", "age": 25});
        let op = PatchOp::Replace {
            path: "/age".to_string(),
            value: json!(30),
        };

        apply_patch(&mut doc, &op);
        assert_eq!(doc["age"], 30);
    }

    #[test]
    fn test_apply_patch_remove() {
        let mut doc = json!({"name": "John", "age": 30});
        let op = PatchOp::Remove {
            path: "/age".to_string(),
        };

        apply_patch(&mut doc, &op);
        assert!(doc.get("age").is_none());
    }

    #[test]
    fn test_apply_merge_patch_add_object() {
        let mut doc = json!({"user": {"name": "John"}});
        let op = PatchOp::Add {
            path: "/user".to_string(),
            value: json!({"age": 30}),
        };

        apply_merge_patch(&mut doc, &op);

        assert_eq!(doc["user"]["name"], "John");
        assert_eq!(doc["user"]["age"], 30);
    }

    #[test]
    fn test_apply_merge_patch_add_array() {
        let mut doc = json!({"items": [1, 2]});
        let op = PatchOp::Add {
            path: "/items".to_string(),
            value: json!([3, 4]),
        };

        apply_merge_patch(&mut doc, &op);

        assert_eq!(doc["items"], json!([1, 2, 3, 4]));
    }

    #[test]
    fn test_apply_merge_patch_replace_array() {
        let mut doc = json!({"items": [1, 2]});
        let op = PatchOp::Replace {
            path: "/items".to_string(),
            value: json!([3, 4]),
        };

        apply_merge_patch(&mut doc, &op);

        assert_eq!(doc["items"], json!([3, 4]));
    }

    #[test]
    fn test_apply_merge_patch_remove() {
        let mut doc = json!({"name": "John", "age": 30});
        let op = PatchOp::Remove {
            path: "/age".to_string(),
        };

        apply_merge_patch(&mut doc, &op);
        assert!(doc.get("age").is_none());
    }

    #[test]
    fn test_overrides_apply_no_match() {
        let overrides = Overrides::default();
        let mut body = json!({"name": "John"});
        let original = body.clone();

        overrides.apply("getUser", &[], "/users", &mut body);

        assert_eq!(body, original);
    }

    #[tokio::test]
    async fn test_overrides_apply_with_operation_match() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_path = temp_dir.path().join("overrides.yaml");

        let yaml_content = r#"
- targets:
    - "operation:getUser"
  patch:
    - op: replace
      path: "/name"
      value: "Jane Doe"
"#;

        fs::write(&yaml_path, yaml_content).await.unwrap();

        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());
        let overrides = Overrides::load_from_globs(&[&pattern]).await.unwrap();

        let mut body = json!({"name": "John"});
        overrides.apply("getUser", &[], "/users", &mut body);

        assert_eq!(body["name"], "Jane Doe");
    }

    #[tokio::test]
    async fn test_overrides_apply_with_tag_match() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_path = temp_dir.path().join("overrides.yaml");

        let yaml_content = r#"
- targets:
    - "tag:admin"
  patch:
    - op: add
      path: "/role"
      value: "administrator"
"#;

        fs::write(&yaml_path, yaml_content).await.unwrap();

        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());
        let overrides = Overrides::load_from_globs(&[&pattern]).await.unwrap();

        let mut body = json!({"name": "John"});
        let tags = vec!["admin".to_string()];
        overrides.apply("getUser", &tags, "/users", &mut body);

        assert_eq!(body["role"], "administrator");
    }

    #[tokio::test]
    async fn test_overrides_with_merge_mode() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_path = temp_dir.path().join("overrides.yaml");

        let yaml_content = r#"
- targets:
    - "operation:getUser"
  mode: merge
  patch:
    - op: add
      path: "/user"
      value:
        age: 30
"#;

        fs::write(&yaml_path, yaml_content).await.unwrap();

        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());
        let overrides = Overrides::load_from_globs(&[&pattern]).await.unwrap();

        let mut body = json!({"user": {"name": "John"}});
        overrides.apply("getUser", &[], "/users", &mut body);

        assert_eq!(body["user"]["name"], "John");
        assert_eq!(body["user"]["age"], 30);
    }

    #[tokio::test]
    async fn test_load_from_globs_multiple_files() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_path1 = temp_dir.path().join("override1.yaml");
        let yaml_path2 = temp_dir.path().join("override2.yaml");

        let yaml_content1 = r#"
- targets:
    - "operation:getUser"
  patch:
    - op: add
      path: "/field1"
      value: "value1"
"#;

        let yaml_content2 = r#"
- targets:
    - "operation:createUser"
  patch:
    - op: add
      path: "/field2"
      value: "value2"
"#;

        fs::write(&yaml_path1, yaml_content1).await.unwrap();
        fs::write(&yaml_path2, yaml_content2).await.unwrap();

        let pattern = format!("{}/**/*.yaml", temp_dir.path().display());
        let result = Overrides::load_from_globs(&[&pattern]).await;

        assert!(result.is_ok());
        let overrides = result.unwrap();
        assert_eq!(overrides.rules.len(), 2);
    }

    #[test]
    fn test_override_rule_deserialize() {
        let yaml = r#"
targets:
  - "operation:getUser"
patch:
  - op: replace
    path: "/name"
    value: "Jane"
when: "env == 'test'"
mode: merge
post_templating: true
"#;

        let rule: OverrideRule = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(rule.targets.len(), 1);
        assert_eq!(rule.patch.len(), 1);
        assert_eq!(rule.when, Some("env == 'test'".to_string()));
        assert_eq!(rule.mode, OverrideMode::Merge);
        assert!(rule.post_templating);
    }
}