Skip to main content

camel_component_http/
bundle.rs

1use std::sync::Arc;
2
3use camel_component_api::{CamelError, ComponentBundle, ComponentRegistrar};
4
5use crate::{HttpComponent, HttpsComponent, config::HttpConfig};
6
7pub struct HttpBundle {
8    config: HttpConfig,
9}
10
11impl ComponentBundle for HttpBundle {
12    fn config_key() -> &'static str {
13        "http"
14    }
15
16    fn from_toml(value: toml::Value) -> Result<Self, CamelError> {
17        let config: HttpConfig = value
18            .try_into()
19            .map_err(|e: toml::de::Error| CamelError::Config(e.to_string()))?;
20        Ok(Self { config })
21    }
22
23    fn register_all(self, ctx: &mut dyn ComponentRegistrar) {
24        ctx.register_component_dyn(Arc::new(HttpComponent::with_config(self.config.clone())));
25        ctx.register_component_dyn(Arc::new(HttpsComponent::with_config(self.config)));
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    struct TestRegistrar {
34        schemes: Vec<String>,
35    }
36
37    impl camel_component_api::ComponentRegistrar for TestRegistrar {
38        fn register_component_dyn(
39            &mut self,
40            component: std::sync::Arc<dyn camel_component_api::Component>,
41        ) {
42            self.schemes.push(component.scheme().to_string());
43        }
44    }
45
46    #[test]
47    fn http_bundle_from_toml_empty_uses_defaults() {
48        let value: toml::Value = toml::from_str("").unwrap();
49        let result = HttpBundle::from_toml(value);
50        assert!(
51            result.is_ok(),
52            "empty HTTP toml must use defaults: {:?}",
53            result.err()
54        );
55    }
56
57    #[test]
58    fn http_bundle_from_toml_custom_timeout() {
59        let value: toml::Value = toml::from_str("connect_timeout_ms = 1000").unwrap();
60        let bundle = HttpBundle::from_toml(value).unwrap();
61        assert_eq!(bundle.config.connect_timeout_ms, 1000);
62    }
63
64    #[test]
65    fn http_bundle_from_toml_returns_error_on_invalid_config() {
66        let mut table = toml::map::Map::new();
67        table.insert(
68            "connect_timeout_ms".to_string(),
69            toml::Value::String("not-a-number".to_string()),
70        );
71
72        let result = HttpBundle::from_toml(toml::Value::Table(table));
73        assert!(result.is_err(), "expected Err on malformed config");
74        let err_msg = match result {
75            Err(err) => err.to_string(),
76            Ok(_) => panic!("expected Err on malformed config"),
77        };
78        assert!(
79            err_msg.contains("Configuration error"),
80            "expected CamelError::Config, got: {err_msg}"
81        );
82    }
83
84    #[test]
85    fn http_bundle_registers_expected_schemes() {
86        let bundle = HttpBundle::from_toml(toml::Value::Table(toml::map::Map::new())).unwrap();
87        let mut registrar = TestRegistrar { schemes: vec![] };
88
89        bundle.register_all(&mut registrar);
90
91        assert_eq!(registrar.schemes, vec!["http", "https"]);
92    }
93}