rift-http-proxy 0.2.0

Rift: high-performance HTTP chaos engineering proxy with Lua/Rhai/JavaScript scripting for fault injection.
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
//! Body matching configuration and compilation.
//!
//! Supports various body matching strategies including JSON and XPath.

use super::matcher::CachedValue;
use super::string_matcher::{CompiledStringMatcher, StringMatcher};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Body matching configuration.
///
/// Supports various body matching strategies for request body content.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum BodyMatcher {
    /// Exact string match
    Equals(String),

    /// String contains substring
    Contains(String),

    /// Regex pattern match
    Matches(String),

    /// JSON deep equality (for JSON bodies)
    #[serde(rename = "jsonEquals")]
    JsonEquals(serde_json::Value),

    /// JSON path expression match
    #[serde(rename = "jsonPath")]
    JsonPath {
        path: String,
        #[serde(flatten)]
        matcher: StringMatcher,
    },

    /// XPath expression match for XML bodies (Mountebank compatibility)
    #[serde(rename = "xpath")]
    XPath {
        path: String,
        #[serde(flatten)]
        matcher: StringMatcher,
    },
}

/// Compiled body matcher for efficient runtime evaluation.
#[derive(Debug, Clone)]
pub enum CompiledBodyMatcher {
    Equals(CachedValue),
    Contains(CachedValue),
    Matches(Arc<Regex>),
    JsonEquals(serde_json::Value),
    JsonPath {
        path: String,
        matcher: CompiledStringMatcher,
    },
    XPath {
        path: String,
        matcher: CompiledStringMatcher,
    },
}

impl CompiledBodyMatcher {
    /// Compile a BodyMatcher configuration.
    pub fn compile(matcher: &BodyMatcher) -> Result<Self, regex::Error> {
        match matcher {
            BodyMatcher::Equals(v) => Ok(CompiledBodyMatcher::Equals(CachedValue::new(v))),
            BodyMatcher::Contains(v) => Ok(CompiledBodyMatcher::Contains(CachedValue::new(v))),
            BodyMatcher::Matches(pattern) => {
                Ok(CompiledBodyMatcher::Matches(Arc::new(Regex::new(pattern)?)))
            }
            BodyMatcher::JsonEquals(value) => Ok(CompiledBodyMatcher::JsonEquals(value.clone())),
            BodyMatcher::JsonPath { path, matcher } => Ok(CompiledBodyMatcher::JsonPath {
                path: path.clone(),
                matcher: CompiledStringMatcher::compile(matcher)?,
            }),
            BodyMatcher::XPath { path, matcher } => Ok(CompiledBodyMatcher::XPath {
                path: path.clone(),
                matcher: CompiledStringMatcher::compile(matcher)?,
            }),
        }
    }

    /// Check if a body matches this matcher.
    pub fn matches(&self, body: &str, case_sensitive: bool) -> bool {
        match self {
            CompiledBodyMatcher::Equals(cached) => cached.equals(body, case_sensitive),
            CompiledBodyMatcher::Contains(cached) => cached.contained_in(body, case_sensitive),
            CompiledBodyMatcher::Matches(regex) => regex.is_match(body),
            CompiledBodyMatcher::JsonEquals(expected) => {
                // Parse body as JSON and compare
                match serde_json::from_str::<serde_json::Value>(body) {
                    Ok(actual) => json_deep_equals(&actual, expected, case_sensitive),
                    Err(_) => false,
                }
            }
            CompiledBodyMatcher::JsonPath { path, matcher } => {
                // Simple JSONPath implementation for common patterns
                match extract_json_path(body, path) {
                    Some(value) => matcher.matches(Some(&value), case_sensitive),
                    None => matcher.matches(None, case_sensitive),
                }
            }
            CompiledBodyMatcher::XPath { path, matcher } => {
                // XPath extraction for XML bodies
                match extract_xpath(body, path) {
                    Some(value) => matcher.matches(Some(&value), case_sensitive),
                    None => matcher.matches(None, case_sensitive),
                }
            }
        }
    }
}

/// Deep JSON equality comparison with optional case sensitivity.
fn json_deep_equals(
    actual: &serde_json::Value,
    expected: &serde_json::Value,
    case_sensitive: bool,
) -> bool {
    use serde_json::Value;

    match (actual, expected) {
        (Value::Null, Value::Null) => true,
        (Value::Bool(a), Value::Bool(b)) => a == b,
        (Value::Number(a), Value::Number(b)) => a == b,
        (Value::String(a), Value::String(b)) => {
            if case_sensitive {
                a == b
            } else {
                a.to_lowercase() == b.to_lowercase()
            }
        }
        (Value::Array(a), Value::Array(b)) => {
            a.len() == b.len()
                && a.iter()
                    .zip(b.iter())
                    .all(|(x, y)| json_deep_equals(x, y, case_sensitive))
        }
        (Value::Object(a), Value::Object(b)) => {
            // All expected keys must be present and match
            b.iter().all(|(key, expected_val)| {
                a.get(key).is_some_and(|actual_val| {
                    json_deep_equals(actual_val, expected_val, case_sensitive)
                })
            })
        }
        _ => false,
    }
}

/// Extract a value from JSON using a simple JSONPath expression.
///
/// Supports:
/// - `$.field` - top-level field
/// - `$.field.nested` - nested field
/// - `$.array[0]` - array index
/// - `$.array[*].field` - all elements' field (returns first match)
pub fn extract_json_path(body: &str, path: &str) -> Option<String> {
    let json: serde_json::Value = serde_json::from_str(body).ok()?;

    // Remove leading $. if present
    let path = path.strip_prefix("$.").unwrap_or(path);
    let path = path.strip_prefix('$').unwrap_or(path);

    let value = navigate_json(&json, path)?;

    match value {
        serde_json::Value::String(s) => Some(s.clone()),
        serde_json::Value::Number(n) => Some(n.to_string()),
        serde_json::Value::Bool(b) => Some(b.to_string()),
        serde_json::Value::Null => Some("null".to_string()),
        _ => Some(value.to_string()),
    }
}

/// Navigate JSON structure following a path.
fn navigate_json<'a>(value: &'a serde_json::Value, path: &str) -> Option<&'a serde_json::Value> {
    if path.is_empty() {
        return Some(value);
    }

    // Split on first . or [
    let (segment, rest) = if let Some(bracket_pos) = path.find('[') {
        let dot_pos = path.find('.');
        match dot_pos {
            Some(d) if d < bracket_pos => {
                let (seg, rest) = path.split_at(d);
                (seg, rest.strip_prefix('.').unwrap_or(rest))
            }
            _ => {
                let (seg, rest) = path.split_at(bracket_pos);
                (seg, rest)
            }
        }
    } else if let Some(dot_pos) = path.find('.') {
        let (seg, rest) = path.split_at(dot_pos);
        (seg, rest.strip_prefix('.').unwrap_or(rest))
    } else {
        (path, "")
    };

    // Handle array index
    if segment.is_empty() && path.starts_with('[') {
        if let Some(end) = path.find(']') {
            let index_str = &path[1..end];
            let rest = path[end + 1..]
                .strip_prefix('.')
                .unwrap_or(&path[end + 1..]);

            if index_str == "*" {
                // Wildcard - return first match from array
                if let serde_json::Value::Array(arr) = value {
                    for item in arr {
                        if let Some(result) = navigate_json(item, rest) {
                            return Some(result);
                        }
                    }
                }
                return None;
            } else if let Some(stripped) = index_str.strip_prefix(':') {
                // Slice notation like [:0] - means first element (index 0)
                // In Python slicing [:0] is empty, but in Mountebank/Solo [:0] means index 0
                let slice_index = stripped.parse::<usize>().unwrap_or(0);
                let arr = value.as_array()?;
                let item = arr.get(slice_index)?;
                return navigate_json(item, rest);
            } else if let Ok(index) = index_str.parse::<usize>() {
                let arr = value.as_array()?;
                let item = arr.get(index)?;
                return navigate_json(item, rest);
            }
        }
        return None;
    }

    // Handle object field
    let obj = value.as_object()?;
    let next = obj.get(segment)?;
    navigate_json(next, rest)
}

/// Extract a value from XML using an XPath expression.
///
/// Supports common XPath patterns:
/// - `/root/element` - absolute path
/// - `//element` - descendant search
/// - `/root/element/@attribute` - attribute selection
/// - `/root/element/text()` - text content
pub fn extract_xpath(body: &str, path: &str) -> Option<String> {
    use sxd_document::parser;
    use sxd_xpath::{evaluate_xpath, Value};

    // Parse the XML document
    let package = parser::parse(body).ok()?;
    let document = package.as_document();

    // Evaluate the XPath expression
    match evaluate_xpath(&document, path) {
        Ok(value) => match value {
            Value::String(s) => Some(s),
            Value::Number(n) => {
                // Format number without unnecessary decimal places
                if n.fract() == 0.0 {
                    Some(format!("{}", n as i64))
                } else {
                    Some(n.to_string())
                }
            }
            Value::Boolean(b) => Some(b.to_string()),
            Value::Nodeset(nodes) => {
                // Return the text content of the first node
                nodes.iter().next().map(|node| node.string_value())
            }
        },
        Err(_) => None,
    }
}

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

    #[test]
    fn test_body_matcher_equals() {
        let matcher =
            CompiledBodyMatcher::compile(&BodyMatcher::Equals("hello world".to_string())).unwrap();

        assert!(matcher.matches("hello world", true));
        assert!(!matcher.matches("HELLO WORLD", true));
        assert!(matcher.matches("HELLO WORLD", false));
        assert!(!matcher.matches("hello", true));
    }

    #[test]
    fn test_body_matcher_contains() {
        let matcher =
            CompiledBodyMatcher::compile(&BodyMatcher::Contains("api".to_string())).unwrap();

        assert!(matcher.matches("this is an api call", true));
        assert!(!matcher.matches("this is an API call", true));
        assert!(matcher.matches("this is an API call", false));
        assert!(!matcher.matches("no match here", true));
    }

    #[test]
    fn test_body_matcher_regex() {
        let matcher =
            CompiledBodyMatcher::compile(&BodyMatcher::Matches(r"\d{3}-\d{4}".to_string()))
                .unwrap();

        assert!(matcher.matches("Call me at 123-4567", true));
        assert!(matcher.matches("Phone: 999-0000", true));
        assert!(!matcher.matches("No phone number", true));
    }

    #[test]
    fn test_body_matcher_json_equals() {
        let expected = serde_json::json!({
            "name": "John",
            "age": 30
        });
        let matcher = CompiledBodyMatcher::compile(&BodyMatcher::JsonEquals(expected)).unwrap();

        // Exact match
        assert!(matcher.matches(r#"{"name": "John", "age": 30}"#, true));

        // Order doesn't matter
        assert!(matcher.matches(r#"{"age": 30, "name": "John"}"#, true));

        // Extra fields in actual are OK (partial match)
        assert!(matcher.matches(r#"{"name": "John", "age": 30, "city": "NYC"}"#, true));

        // Missing fields fail
        assert!(!matcher.matches(r#"{"name": "John"}"#, true));

        // Wrong values fail
        assert!(!matcher.matches(r#"{"name": "Jane", "age": 30}"#, true));

        // Case insensitive string comparison
        assert!(!matcher.matches(r#"{"name": "JOHN", "age": 30}"#, true));
        assert!(matcher.matches(r#"{"name": "JOHN", "age": 30}"#, false));
    }

    #[test]
    fn test_body_matcher_json_path() {
        let matcher = CompiledBodyMatcher::compile(&BodyMatcher::JsonPath {
            path: "$.user.name".to_string(),
            matcher: StringMatcher::Equals("John".to_string()),
        })
        .unwrap();

        assert!(matcher.matches(r#"{"user": {"name": "John", "age": 30}}"#, true));
        assert!(!matcher.matches(r#"{"user": {"name": "Jane", "age": 25}}"#, true));
        assert!(!matcher.matches(r#"{"user": {"age": 30}}"#, true));
    }

    #[test]
    fn test_json_path_simple_field() {
        let body = r#"{"name": "John", "age": 30}"#;
        assert_eq!(extract_json_path(body, "$.name"), Some("John".to_string()));
        assert_eq!(extract_json_path(body, "$.age"), Some("30".to_string()));
        assert_eq!(extract_json_path(body, "$.missing"), None);
    }

    #[test]
    fn test_json_path_nested() {
        let body = r#"{"user": {"profile": {"name": "John"}}}"#;
        assert_eq!(
            extract_json_path(body, "$.user.profile.name"),
            Some("John".to_string())
        );
    }

    #[test]
    fn test_json_path_array_index() {
        let body = r#"{"users": [{"name": "Alice"}, {"name": "Bob"}]}"#;
        assert_eq!(
            extract_json_path(body, "$.users[0].name"),
            Some("Alice".to_string())
        );
        assert_eq!(
            extract_json_path(body, "$.users[1].name"),
            Some("Bob".to_string())
        );
        assert_eq!(extract_json_path(body, "$.users[2].name"), None);
    }

    #[test]
    fn test_json_path_wildcard() {
        let body = r#"{"items": [{"id": 1}, {"id": 2}, {"id": 3}]}"#;
        // Wildcard returns first match
        assert_eq!(
            extract_json_path(body, "$.items[*].id"),
            Some("1".to_string())
        );
    }

    #[test]
    fn test_json_path_slice_notation() {
        // Test [:0] slice notation used by Solo/Mountebank
        let body = r#"{"receiver":{"context":{"correlationKeys":[{"keyValue":"728839"}]}}}"#;
        assert_eq!(
            extract_json_path(body, "$.receiver.context.correlationKeys.[:0].keyValue"),
            Some("728839".to_string())
        );

        // Test with multiple items
        let body2 = r#"{"items":[{"name":"first"},{"name":"second"}]}"#;
        assert_eq!(
            extract_json_path(body2, "$.items.[:0].name"),
            Some("first".to_string())
        );
        assert_eq!(
            extract_json_path(body2, "$.items.[:1].name"),
            Some("second".to_string())
        );
    }

    #[test]
    fn test_xpath_simple_element() {
        let xml = r#"<root><name>John</name><age>30</age></root>"#;
        assert_eq!(extract_xpath(xml, "/root/name"), Some("John".to_string()));
        assert_eq!(extract_xpath(xml, "/root/age"), Some("30".to_string()));
        assert_eq!(extract_xpath(xml, "/root/missing"), None);
    }

    #[test]
    fn test_xpath_nested() {
        let xml = r#"<root><user><profile><name>Jane</name></profile></user></root>"#;
        assert_eq!(
            extract_xpath(xml, "/root/user/profile/name"),
            Some("Jane".to_string())
        );
    }

    #[test]
    fn test_xpath_attribute() {
        let xml = r#"<root><item id="123">Content</item></root>"#;
        assert_eq!(
            extract_xpath(xml, "/root/item/@id"),
            Some("123".to_string())
        );
    }

    #[test]
    fn test_xpath_descendant() {
        let xml = r#"<root><level1><level2><target>Found</target></level2></level1></root>"#;
        assert_eq!(extract_xpath(xml, "//target"), Some("Found".to_string()));
    }

    #[test]
    fn test_body_matcher_xpath() {
        let matcher = CompiledBodyMatcher::compile(&BodyMatcher::XPath {
            path: "/order/customer/name".to_string(),
            matcher: StringMatcher::Equals("Alice".to_string()),
        })
        .unwrap();

        let xml = r#"<order><customer><name>Alice</name><email>alice@example.com</email></customer></order>"#;
        assert!(matcher.matches(xml, true));

        let xml_wrong = r#"<order><customer><name>Bob</name></customer></order>"#;
        assert!(!matcher.matches(xml_wrong, true));
    }

    #[test]
    fn test_xpath_invalid_xml() {
        let invalid = "not xml at all";
        assert_eq!(extract_xpath(invalid, "/root/name"), None);
    }
}