Skip to main content

camel_processor/
remove_header.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use tower::Service;
6
7use camel_api::{CamelError, Exchange, PROPERTY_HTTP_CONTENT_TYPE_INHERITED};
8
9/// A processor that removes a header from the exchange's input message.
10#[derive(Clone)]
11pub struct RemoveHeader<P> {
12    inner: P,
13    key: String,
14}
15
16impl<P> RemoveHeader<P> {
17    /// Create a new RemoveHeader processor that removes the given header.
18    pub fn new(inner: P, key: impl Into<String>) -> Self {
19        Self {
20            inner,
21            key: key.into(),
22        }
23    }
24}
25
26/// A Tower Layer that wraps an inner service with a [`RemoveHeader`].
27#[derive(Clone)]
28pub struct RemoveHeaderLayer {
29    key: String,
30}
31
32impl RemoveHeaderLayer {
33    pub fn new(key: impl Into<String>) -> Self {
34        Self { key: key.into() }
35    }
36}
37
38impl<S> tower::Layer<S> for RemoveHeaderLayer {
39    type Service = RemoveHeader<S>;
40
41    fn layer(&self, inner: S) -> Self::Service {
42        RemoveHeader::new(inner, self.key.clone())
43    }
44}
45
46impl<P> Service<Exchange> for RemoveHeader<P>
47where
48    P: Service<Exchange, Response = Exchange, Error = CamelError> + Clone + Send + 'static,
49    P::Future: Send,
50{
51    type Response = Exchange;
52    type Error = CamelError;
53    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;
54
55    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
56        self.inner.poll_ready(cx)
57    }
58
59    fn call(&mut self, mut exchange: Exchange) -> Self::Future {
60        exchange.input.remove_header(&self.key);
61        if self.key.eq_ignore_ascii_case("content-type") {
62            exchange.remove_property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED);
63        }
64        let fut = self.inner.call(exchange);
65        Box::pin(fut)
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use camel_api::{IdentityProcessor, Message, Value};
73    use tower::ServiceExt;
74
75    #[tokio::test]
76    async fn test_remove_header_deletes_existing() {
77        let mut exchange = Exchange::new(Message::default());
78        exchange
79            .input
80            .set_header("CamelHttpPath", Value::String("x".into()));
81
82        let processor = RemoveHeader::new(IdentityProcessor, "CamelHttpPath");
83
84        let result = processor.oneshot(exchange).await.unwrap();
85        assert!(!result.input.headers.contains_key("CamelHttpPath"));
86    }
87
88    #[tokio::test]
89    async fn test_remove_header_noop_on_missing() {
90        let mut exchange = Exchange::new(Message::default());
91        exchange.input.set_header("A", Value::from(1));
92        exchange.input.set_header("B", Value::from(2));
93
94        let processor = RemoveHeader::new(IdentityProcessor, "C");
95
96        let result = processor.oneshot(exchange).await.unwrap();
97        assert_eq!(result.input.header("A"), Some(&Value::from(1)));
98        assert_eq!(result.input.header("B"), Some(&Value::from(2)));
99        assert!(!result.input.headers.contains_key("C"));
100    }
101
102    #[tokio::test]
103    async fn test_remove_header_preserves_other_headers() {
104        let mut exchange = Exchange::new(Message::default());
105        exchange.input.set_header("X", Value::from(1));
106        exchange.input.set_header("Y", Value::from(2));
107        exchange.input.set_header("Z", Value::from(3));
108
109        let processor = RemoveHeader::new(IdentityProcessor, "Y");
110
111        let result = processor.oneshot(exchange).await.unwrap();
112        assert_eq!(result.input.header("X"), Some(&Value::from(1)));
113        assert_eq!(result.input.header("Z"), Some(&Value::from(3)));
114        assert!(!result.input.headers.contains_key("Y"));
115    }
116
117    #[tokio::test]
118    async fn remove_header_removes_different_case() {
119        let mut exchange = Exchange::new(Message::default());
120        exchange
121            .input
122            .set_header("X-Flag", Value::String("old".into()));
123
124        let processor = RemoveHeader::new(IdentityProcessor, "x-flag");
125
126        let result = processor.oneshot(exchange).await.unwrap();
127        assert_eq!(result.input.header("X-Flag"), None);
128        assert_eq!(result.input.header_ic("x-flag"), None);
129    }
130
131    #[tokio::test]
132    async fn test_remove_header_preserves_body() {
133        let exchange = Exchange::new(Message::new("hello"));
134
135        let processor = RemoveHeader::new(IdentityProcessor, "anything");
136
137        let result = processor.oneshot(exchange).await.unwrap();
138        assert_eq!(result.input.body.as_text(), Some("hello"));
139    }
140
141    // ── Content-Type provenance marker ──
142
143    #[tokio::test]
144    async fn remove_header_clears_marker_on_content_type() {
145        let mut exchange = Exchange::new(Message::default());
146        exchange.set_property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED, Value::Bool(true));
147        exchange
148            .input
149            .set_header("Content-Type", Value::String("application/json".into()));
150
151        let processor = RemoveHeader::new(IdentityProcessor, "Content-Type");
152
153        let result = processor.oneshot(exchange).await.unwrap();
154        assert_eq!(
155            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
156            None,
157            "removing Content-Type must clear the marker"
158        );
159        assert!(result.input.header_ic("content-type").is_none());
160    }
161
162    #[tokio::test]
163    async fn remove_header_keeps_marker_on_other_header() {
164        let mut exchange = Exchange::new(Message::default());
165        exchange.set_property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED, Value::Bool(true));
166        exchange.input.set_header("X-Flag", Value::Bool(true));
167
168        let processor = RemoveHeader::new(IdentityProcessor, "X-Flag");
169
170        let result = processor.oneshot(exchange).await.unwrap();
171        assert_eq!(
172            result.property(PROPERTY_HTTP_CONTENT_TYPE_INHERITED),
173            Some(&Value::Bool(true)),
174            "removing an unrelated header must leave the marker untouched"
175        );
176    }
177}