camel-processor 0.70.0

Message processors for rust-camel
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
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use tower::Service;

use camel_api::exchange::Exchange;
use camel_api::{CamelError, Headers, PROPERTY_HTTP_CONTENT_TYPE_INHERITED, Value};
use camel_language_api::{EvalMeta, MutatingExpression, to_expression_failed};

/// Collect the exact Content-Type header entries (key spelling and value) as a
/// deterministically ordered list.
///
/// Both language surfaces commit changed-only per exact key, so a before/after
/// comparison of this list detects every landed Content-Type mutation while
/// ignoring same-value assignments.
fn content_type_entries(headers: &Headers) -> Vec<(String, Value)> {
    let mut entries: Vec<(String, Value)> = headers
        .iter()
        .filter(|(key, _)| key.eq_ignore_ascii_case("content-type"))
        .map(|(key, value)| (key.clone(), value.clone()))
        .collect();
    entries.sort_by(|a, b| a.0.cmp(&b.0));
    entries
}

/// Processor that executes a mutating expression, allowing scripts to modify the Exchange.
/// Uses `Arc<dyn MutatingExpression>` to enable `Clone` (required by `BoxProcessor`).
///
/// Evaluation failures are mapped to [`CamelError::ExpressionFailed`] with
/// the trusted route metadata supplied via [`ScriptMutator::with_meta`]
/// (ParseError routes as class `Parse` via `LanguageError::class()`).
#[derive(Clone)]
pub struct ScriptMutator {
    expression: Arc<dyn MutatingExpression>,
    meta: EvalMeta,
}

impl ScriptMutator {
    /// Create without route metadata. The default metadata reports language
    /// `unknown` — tests only; production code should use
    /// [`ScriptMutator::with_meta`].
    pub fn new(expression: Box<dyn MutatingExpression>) -> Self {
        Self::with_meta(
            expression,
            EvalMeta {
                language: "unknown".to_string(),
                route_id: String::new(),
                step_id: String::new(),
                verb: String::new(),
                target: None,
            },
        )
    }

    /// Create with trusted route metadata used to enrich evaluation
    /// failures as [`CamelError::ExpressionFailed`].
    pub fn with_meta(expression: Box<dyn MutatingExpression>, meta: EvalMeta) -> Self {
        Self {
            expression: expression.into(),
            meta,
        }
    }
}

impl Service<Exchange> for ScriptMutator {
    type Response = Exchange;
    type Error = CamelError;
    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, mut exchange: Exchange) -> Self::Future {
        let expression = self.expression.clone();
        let meta = self.meta.clone();
        let content_type_before = content_type_entries(&exchange.input.headers);
        Box::pin(async move {
            let result = expression.evaluate(&mut exchange).await;
            match result {
                Ok(_) => {
                    if content_type_entries(&exchange.input.headers) != content_type_before {
                        exchange.remove_property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED);
                    }
                    Ok(exchange)
                }
                Err(e) => Err(to_expression_failed(e, &meta)),
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use camel_api::{CamelError, Exchange, ExpressionErrorClass, Message, Value};
    use camel_language_api::{EvalMeta, LanguageError};
    use tower::ServiceExt;

    use super::*;

    /// A mutating expression that always fails with a structured failure.
    struct EvalFailureMutatingExpression;

    #[async_trait::async_trait]
    impl MutatingExpression for EvalFailureMutatingExpression {
        async fn evaluate(&self, _exchange: &mut Exchange) -> Result<Value, LanguageError> {
            Err(LanguageError::EvalFailure {
                class: ExpressionErrorClass::Runtime,
                position: None,
                detail: None,
            })
        }
    }

    /// A simple test mutating expression that sets a header
    struct TestMutatingExpression;

    struct ParseErrorMutatingExpression;
    struct NotSupportedMutatingExpression;
    struct UnknownVariableMutatingExpression;

    #[async_trait::async_trait]
    impl MutatingExpression for TestMutatingExpression {
        async fn evaluate(&self, exchange: &mut Exchange) -> Result<Value, LanguageError> {
            exchange
                .input
                .headers
                .insert("mutated".into(), Value::Bool(true));
            Ok(Value::Null)
        }
    }

    #[async_trait::async_trait]
    impl MutatingExpression for ParseErrorMutatingExpression {
        async fn evaluate(&self, _exchange: &mut Exchange) -> Result<Value, LanguageError> {
            Err(LanguageError::ParseError {
                expr: "x".to_string(),
                reason: "bad".to_string(),
            })
        }
    }

    #[async_trait::async_trait]
    impl MutatingExpression for NotSupportedMutatingExpression {
        async fn evaluate(&self, _exchange: &mut Exchange) -> Result<Value, LanguageError> {
            Err(LanguageError::NotSupported {
                feature: "f".to_string(),
                language: "l".to_string(),
            })
        }
    }

    #[async_trait::async_trait]
    impl MutatingExpression for UnknownVariableMutatingExpression {
        async fn evaluate(&self, _exchange: &mut Exchange) -> Result<Value, LanguageError> {
            Err(LanguageError::UnknownVariable("foo".to_string()))
        }
    }

    #[tokio::test]
    async fn test_script_mutator_modifies_exchange() {
        let exchange = Exchange::new(Message::new("test"));

        let mutator = ScriptMutator::new(Box::new(TestMutatingExpression));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(result.input.header("mutated"), Some(&Value::Bool(true)));
    }

    #[tokio::test]
    async fn test_script_mutator_preserves_body() {
        let exchange = Exchange::new(Message::new("original body"));

        let mutator = ScriptMutator::new(Box::new(TestMutatingExpression));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(result.input.body.as_text(), Some("original body"));
    }

    #[tokio::test]
    async fn test_script_mutator_is_clone() {
        let mutator = ScriptMutator::new(Box::new(TestMutatingExpression));
        let _cloned = mutator.clone();
    }

    #[tokio::test]
    async fn script_mutator_error_is_expression_failed() {
        let meta = EvalMeta {
            language: "rhai".to_string(),
            route_id: "r1".to_string(),
            step_id: "script#0".to_string(),
            verb: "script".to_string(),
            target: None,
        };
        let mutator = ScriptMutator::with_meta(Box::new(EvalFailureMutatingExpression), meta);

        let result = mutator.oneshot(Exchange::new(Message::new("test"))).await;

        assert!(
            matches!(
                result,
                Err(CamelError::ExpressionFailed {
                    ref language,
                    ref verb,
                    class: ExpressionErrorClass::Runtime,
                    ..
                }) if language == "rhai" && verb == "script"
            ),
            "mutating-eval failure must map to ExpressionFailed with the bound meta"
        );
    }

    #[tokio::test]
    async fn test_script_mutator_maps_parse_error() {
        let exchange = Exchange::new(Message::new("test"));
        let mutator = ScriptMutator::new(Box::new(ParseErrorMutatingExpression));
        let result = mutator.oneshot(exchange).await;
        assert!(matches!(
            result,
            Err(CamelError::ExpressionFailed {
                class: ExpressionErrorClass::Parse,
                ..
            })
        ));
    }

    #[tokio::test]
    async fn test_script_mutator_maps_not_supported_error() {
        let exchange = Exchange::new(Message::new("test"));
        let mutator = ScriptMutator::new(Box::new(NotSupportedMutatingExpression));
        let result = mutator.oneshot(exchange).await;
        // NotSupported carries no class: to_expression_failed defaults it to
        // Runtime under the default "unknown" language.
        assert!(matches!(
            result,
            Err(CamelError::ExpressionFailed {
                ref language,
                class: ExpressionErrorClass::Runtime,
                ..
            }) if language == "unknown"
        ));
    }

    #[tokio::test]
    async fn test_script_mutator_maps_other_language_error() {
        let exchange = Exchange::new(Message::new("test"));
        let mutator = ScriptMutator::new(Box::new(UnknownVariableMutatingExpression));
        let result = mutator.oneshot(exchange).await;
        assert!(matches!(
            result,
            Err(CamelError::ExpressionFailed {
                class: ExpressionErrorClass::Runtime,
                ..
            })
        ));
    }

    // ── Content-Type provenance marker ──

    /// A script that assigns a new value to the `Content-Type` header.
    struct SetContentTypeMutatingExpression(&'static str);

    #[async_trait::async_trait]
    impl MutatingExpression for SetContentTypeMutatingExpression {
        async fn evaluate(&self, exchange: &mut Exchange) -> Result<Value, LanguageError> {
            exchange
                .input
                .headers
                .insert("Content-Type".into(), Value::String(self.0.to_string()));
            Ok(Value::Null)
        }
    }

    /// A script that renames the `Content-Type` header key casing, keeping the value.
    struct RenameContentTypeCaseMutatingExpression;

    #[async_trait::async_trait]
    impl MutatingExpression for RenameContentTypeCaseMutatingExpression {
        async fn evaluate(&self, exchange: &mut Exchange) -> Result<Value, LanguageError> {
            let value = exchange
                .input
                .headers
                .remove("Content-Type")
                .expect("test double expects a Content-Type header");
            exchange.input.headers.insert("content-type".into(), value);
            Ok(Value::Null)
        }
    }

    /// A script that removes the `Content-Type` header.
    struct RemoveContentTypeMutatingExpression;

    #[async_trait::async_trait]
    impl MutatingExpression for RemoveContentTypeMutatingExpression {
        async fn evaluate(&self, exchange: &mut Exchange) -> Result<Value, LanguageError> {
            exchange.input.headers.remove("Content-Type");
            Ok(Value::Null)
        }
    }

    /// A script that only touches the body.
    struct BodyOnlyMutatingExpression;

    #[async_trait::async_trait]
    impl MutatingExpression for BodyOnlyMutatingExpression {
        async fn evaluate(&self, exchange: &mut Exchange) -> Result<Value, LanguageError> {
            exchange.input.body = "mutated body".into();
            Ok(Value::Null)
        }
    }

    fn exchange_with_content_type(value: &str) -> Exchange {
        let mut exchange = Exchange::new(Message::new("test"));
        exchange
            .input
            .headers
            .insert("Content-Type".into(), Value::String(value.into()));
        exchange.set_property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED, Value::Bool(true));
        exchange
    }

    #[tokio::test]
    async fn script_mutator_clears_marker_on_ct_update() {
        let exchange = exchange_with_content_type("application/json");
        let mutator = ScriptMutator::new(Box::new(SetContentTypeMutatingExpression("text/html")));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(
            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
            None,
            "a script write to Content-Type must clear the inherited marker"
        );
        assert_eq!(
            result.input.header("Content-Type"),
            Some(&Value::String("text/html".into()))
        );
    }

    #[tokio::test]
    async fn script_mutator_clears_marker_on_case_rename() {
        let exchange = exchange_with_content_type("application/json");
        let mutator = ScriptMutator::new(Box::new(RenameContentTypeCaseMutatingExpression));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(
            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
            None,
            "renaming the Content-Type key casing lands a mutation and must clear the marker"
        );
        assert_eq!(
            result.input.header("content-type"),
            Some(&Value::String("application/json".into()))
        );
    }

    #[tokio::test]
    async fn script_mutator_clears_marker_on_ct_removal() {
        let exchange = exchange_with_content_type("application/json");
        let mutator = ScriptMutator::new(Box::new(RemoveContentTypeMutatingExpression));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(
            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
            None,
            "removing Content-Type must clear the inherited marker"
        );
        assert_eq!(result.input.header("Content-Type"), None);
    }

    #[tokio::test]
    async fn script_mutator_keeps_marker_on_same_value_assignment() {
        let exchange = exchange_with_content_type("application/json");
        let mutator = ScriptMutator::new(Box::new(SetContentTypeMutatingExpression(
            "application/json",
        )));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(
            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
            Some(&Value::Bool(true)),
            "a same-value assignment does not land a change and must keep the marker"
        );
    }

    #[tokio::test]
    async fn script_mutator_keeps_marker_on_body_only_script() {
        let exchange = exchange_with_content_type("application/json");
        let mutator = ScriptMutator::new(Box::new(BodyOnlyMutatingExpression));

        let result = mutator.oneshot(exchange).await.unwrap();
        assert_eq!(
            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
            Some(&Value::Bool(true)),
            "a body-only script must keep the marker"
        );
        assert_eq!(result.input.body.as_text(), Some("mutated body"));
    }

    /// Marker value and headers captured by the snapshotting test double.
    type CapturedHeaderState = Option<(Option<Value>, camel_api::Headers)>;

    /// A failing script that snapshots the marker and headers it is handed. The
    /// `Err` path consumes the exchange, so recording the state inside `evaluate`
    /// is the only way to assert the caller cleared nothing on the error path.
    struct SnapshottingParseErrorMutatingExpression(Arc<std::sync::Mutex<CapturedHeaderState>>);

    #[async_trait::async_trait]
    impl MutatingExpression for SnapshottingParseErrorMutatingExpression {
        async fn evaluate(&self, exchange: &mut Exchange) -> Result<Value, LanguageError> {
            *self
                .0
                .lock()
                .expect("test double snapshot mutex is not poisoned") = Some((
                exchange
                    .property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED)
                    .cloned(),
                exchange.input.headers.clone(),
            ));
            Err(LanguageError::ParseError {
                expr: "x".to_string(),
                reason: "bad".to_string(),
            })
        }
    }

    #[tokio::test]
    async fn script_mutator_keeps_marker_on_eval_error() {
        let snapshot = Arc::new(std::sync::Mutex::new(None));
        let mutator = ScriptMutator::new(Box::new(SnapshottingParseErrorMutatingExpression(
            snapshot.clone(),
        )));

        let result = mutator
            .oneshot(exchange_with_content_type("application/json"))
            .await;
        assert!(
            matches!(result, Err(CamelError::ExpressionFailed { .. })),
            "the parse error must propagate"
        );

        let (marker, headers) = snapshot
            .lock()
            .expect("test double snapshot mutex is not poisoned")
            .take()
            .expect("the expression must have run");
        assert_eq!(
            marker,
            Some(Value::Bool(true)),
            "a failed script must not clear the marker"
        );
        assert_eq!(
            headers.get("Content-Type"),
            Some(&Value::String("application/json".into())),
            "a failed script must not touch the headers"
        );
    }
}