1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use tower::Service;
6
7use camel_api::{CamelError, Exchange, Value};
8
9#[derive(Clone)]
11pub struct SetHeader<P> {
12 inner: P,
13 key: String,
14 value: Value,
15}
16
17impl<P> SetHeader<P> {
18 pub fn new(inner: P, key: impl Into<String>, value: impl Into<Value>) -> Self {
20 Self {
21 inner,
22 key: key.into(),
23 value: value.into(),
24 }
25 }
26}
27
28#[derive(Clone)]
30pub struct SetHeaderLayer {
31 key: String,
32 value: Value,
33}
34
35impl SetHeaderLayer {
36 pub fn new(key: impl Into<String>, value: impl Into<Value>) -> Self {
37 Self {
38 key: key.into(),
39 value: value.into(),
40 }
41 }
42}
43
44impl<S> tower::Layer<S> for SetHeaderLayer {
45 type Service = SetHeader<S>;
46
47 fn layer(&self, inner: S) -> Self::Service {
48 SetHeader::new(inner, self.key.clone(), self.value.clone())
49 }
50}
51
52impl<P> Service<Exchange> for SetHeader<P>
53where
54 P: Service<Exchange, Response = Exchange, Error = CamelError> + Clone + Send + 'static,
55 P::Future: Send,
56{
57 type Response = Exchange;
58 type Error = CamelError;
59 type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;
60
61 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
62 self.inner.poll_ready(cx)
63 }
64
65 fn call(&mut self, mut exchange: Exchange) -> Self::Future {
66 exchange
67 .input
68 .set_header(self.key.clone(), self.value.clone());
69 let fut = self.inner.call(exchange);
70 Box::pin(fut)
71 }
72}
73
74#[derive(Clone)]
77pub struct SetHeaderIfAbsent<P> {
78 inner: P,
79 key: String,
80 value: Value,
81}
82
83impl<P> SetHeaderIfAbsent<P> {
84 pub fn new(inner: P, key: impl Into<String>, value: impl Into<Value>) -> Self {
87 Self {
88 inner,
89 key: key.into(),
90 value: value.into(),
91 }
92 }
93}
94
95impl<P> Service<Exchange> for SetHeaderIfAbsent<P>
96where
97 P: Service<Exchange, Response = Exchange, Error = CamelError> + Clone + Send + 'static,
98 P::Future: Send,
99{
100 type Response = Exchange;
101 type Error = CamelError;
102 type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;
103
104 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
105 self.inner.poll_ready(cx)
106 }
107
108 fn call(&mut self, mut exchange: Exchange) -> Self::Future {
109 if exchange.input.header_ic(&self.key).is_none() {
110 exchange
111 .input
112 .set_header(self.key.clone(), self.value.clone());
113 }
114 let fut = self.inner.call(exchange);
115 Box::pin(fut)
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122 use camel_api::{IdentityProcessor, Message};
123 use tower::ServiceExt;
124
125 #[tokio::test]
126 async fn test_set_header_adds_header() {
127 let exchange = Exchange::new(Message::default());
128
129 let processor = SetHeader::new(IdentityProcessor, "source", Value::String("timer".into()));
130
131 let result = processor.oneshot(exchange).await.unwrap();
132 assert_eq!(
133 result.input.header("source"),
134 Some(&Value::String("timer".into()))
135 );
136 }
137
138 #[tokio::test]
139 async fn test_set_header_overwrites_existing() {
140 let mut exchange = Exchange::new(Message::default());
141 exchange
142 .input
143 .set_header("key", Value::String("old".into()));
144
145 let processor = SetHeader::new(IdentityProcessor, "key", Value::String("new".into()));
146
147 let result = processor.oneshot(exchange).await.unwrap();
148 assert_eq!(
149 result.input.header("key"),
150 Some(&Value::String("new".into()))
151 );
152 }
153
154 #[tokio::test]
155 async fn set_header_replaces_different_case() {
156 let mut exchange = Exchange::new(Message::default());
157 exchange
158 .input
159 .set_header("X-Flag", Value::String("old".into()));
160
161 let processor = SetHeader::new(IdentityProcessor, "x-flag", Value::String("new".into()));
162
163 let result = processor.oneshot(exchange).await.unwrap();
164 assert_eq!(
165 result.input.header("x-flag"),
166 Some(&Value::String("new".into()))
167 );
168 assert_eq!(result.input.header("X-Flag"), None);
169 assert_eq!(result.input.headers.len(), 1);
170 }
171
172 #[tokio::test]
173 async fn test_set_header_preserves_body() {
174 let exchange = Exchange::new(Message::new("body content"));
175
176 let processor = SetHeader::new(IdentityProcessor, "header", Value::Bool(true));
177
178 let result = processor.oneshot(exchange).await.unwrap();
179 assert_eq!(result.input.body.as_text(), Some("body content"));
180 assert_eq!(result.input.header("header"), Some(&Value::Bool(true)));
181 }
182
183 #[tokio::test]
184 async fn test_set_header_layer_composes() {
185 use tower::ServiceBuilder;
186
187 let svc = ServiceBuilder::new()
188 .layer(super::SetHeaderLayer::new(
189 "env",
190 Value::String("test".into()),
191 ))
192 .service(IdentityProcessor);
193
194 let exchange = Exchange::new(Message::default());
195 let result = svc.oneshot(exchange).await.unwrap();
196 assert_eq!(
197 result.input.header("env"),
198 Some(&Value::String("test".into()))
199 );
200 }
201
202 #[tokio::test]
205 async fn test_set_header_if_absent_adds_when_missing() {
206 let exchange = Exchange::new(Message::default());
207
208 let processor =
209 SetHeaderIfAbsent::new(IdentityProcessor, "source", Value::String("timer".into()));
210
211 let result = processor.oneshot(exchange).await.unwrap();
212 assert_eq!(
213 result.input.header("source"),
214 Some(&Value::String("timer".into()))
215 );
216 }
217
218 #[tokio::test]
219 async fn test_set_header_if_absent_preserves_existing() {
220 let mut exchange = Exchange::new(Message::default());
221 exchange
222 .input
223 .set_header("key", Value::String("old".into()));
224
225 let processor =
226 SetHeaderIfAbsent::new(IdentityProcessor, "key", Value::String("new".into()));
227
228 let result = processor.oneshot(exchange).await.unwrap();
229 assert_eq!(
230 result.input.header("key"),
231 Some(&Value::String("old".into()))
232 );
233 }
234
235 #[tokio::test]
236 async fn set_header_if_absent_respects_different_case() {
237 let mut exchange = Exchange::new(Message::default());
238 exchange
239 .input
240 .set_header("X-Flag", Value::String("old".into()));
241
242 let processor =
243 SetHeaderIfAbsent::new(IdentityProcessor, "x-flag", Value::String("new".into()));
244
245 let result = processor.oneshot(exchange).await.unwrap();
246 assert_eq!(
247 result.input.header_ic("x-flag"),
248 Some(&Value::String("old".into()))
249 );
250 assert_eq!(result.input.header("x-flag"), None);
251 assert_eq!(result.input.headers.len(), 1);
252 }
253
254 #[tokio::test]
255 async fn set_header_if_absent_sees_raw_map_different_case() {
256 let mut exchange = Exchange::new(Message::default());
257 exchange
258 .input
259 .headers
260 .insert("X-Flag".into(), Value::String("old".into()));
261
262 let processor =
263 SetHeaderIfAbsent::new(IdentityProcessor, "x-flag", Value::String("new".into()));
264
265 let result = processor.oneshot(exchange).await.unwrap();
266 assert_eq!(
267 result.input.header_ic("x-flag"),
268 Some(&Value::String("old".into()))
269 );
270 assert_eq!(result.input.header("x-flag"), None);
271 assert_eq!(result.input.headers.len(), 1);
272 }
273
274 #[tokio::test]
275 async fn test_set_header_if_absent_preserves_body() {
276 let exchange = Exchange::new(Message::new("body content"));
277
278 let processor = SetHeaderIfAbsent::new(IdentityProcessor, "header", Value::Bool(true));
279
280 let result = processor.oneshot(exchange).await.unwrap();
281 assert_eq!(result.input.body.as_text(), Some("body content"));
282 assert_eq!(result.input.header("header"), Some(&Value::Bool(true)));
283 }
284}