myc-core 8.3.0+beta

Provide base features of the Mycelium project as s and Use-cases.
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use super::{
    http::HttpMethod, http_secret::HttpSecret, security_group::SecurityGroup,
    service::Service,
};

use http::{uri::PathAndQuery, Uri};
use mycelium_base::{
    dtos::Parent,
    utils::errors::{dto_err, execution_err, MappedErrors},
};
use serde::{Deserialize, Serialize};
use utoipa::{ToResponse, ToSchema};
use uuid::Uuid;

fn default_service() -> Parent<Service, Uuid> {
    Parent::Id(Uuid::nil())
}

#[derive(
    Debug, Clone, Deserialize, Serialize, ToSchema, ToResponse, PartialEq, Eq,
)]
#[serde(rename_all = "camelCase")]
pub struct Route {
    /// The route id
    pub id: Option<Uuid>,

    /// The route service
    #[serde(default = "default_service")]
    pub service: Parent<Service, Uuid>,

    /// The route name
    #[serde(alias = "group")]
    pub security_group: SecurityGroup,

    /// The route description
    pub methods: Vec<HttpMethod>,

    /// The route url
    pub path: String,

    /// The route secret name if it exists
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secret_name: Option<String>,

    /// The route without tls
    ///
    /// This field should be evaluated if the route should request a secret to
    /// be send to the downstream service, if the route is not secure.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accept_insecure_routing: Option<bool>,

    /// Callbacks
    ///
    /// A vector of callback names to execute.
    ///
    /// Example:
    ///
    /// ```json
    /// ["my_callback", "my_callback_2"]
    /// ```
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callbacks: Option<Vec<String>>,
}

impl Route {
    pub fn new(
        id: Option<Uuid>,
        service: Service,
        group: SecurityGroup,
        methods: Vec<HttpMethod>,
        path: String,
        secret_name: Option<String>,
        accept_insecure_routing: Option<bool>,
        callbacks: Option<Vec<String>>,
    ) -> Self {
        Self {
            id: match id {
                Some(id) => Some(id),
                None => Some(Uuid::new_v3(
                    &Uuid::NAMESPACE_DNS,
                    format!(
                        "{service_name}-{path}-{methods}",
                        service_name = service.name,
                        path = path,
                        methods = methods
                            .iter()
                            .map(|m| m.to_string())
                            .collect::<Vec<String>>()
                            .join("-")
                    )
                    .as_bytes(),
                )),
            },
            service: Parent::Record(service),
            security_group: group,
            methods,
            path,
            secret_name,
            accept_insecure_routing,
            callbacks,
        }
    }

    /// Check if a method is allowed
    ///
    /// Rules:
    /// - If the methods list includes None, nothing is allowed
    /// - If the methods list includes All, all methods are allowed
    /// - If the methods list includes Read, all read methods are allowed, like
    ///   GET, HEAD, OPTIONS, TRACE.
    /// - If the methods list includes Write, all write methods are allowed,
    ///   like POST, PUT, PATCH, DELETE.
    ///
    /// Users can declared a combination of read and write methods to simplify
    /// the declaration, like:
    /// - ["GET", "POST"]
    /// - ["READ", "POST"]
    /// - ["WRITE", "GET"]
    ///
    pub async fn allow_method(&self, method: HttpMethod) -> Option<HttpMethod> {
        //
        // Check if the methods list includes None. If so, return None
        //
        if self.methods.contains(&HttpMethod::None) {
            return None;
        }

        //
        // Check if the methods list includes All. If so, return the method
        //
        if self.methods.contains(&HttpMethod::All) {
            return Some(method);
        }

        //
        // Check if the method is a read method and the methods list includes
        // Read. If so, return the method
        //
        if method.is_read_method() && self.methods.contains(&HttpMethod::Read) {
            return Some(method);
        }

        //
        // Check if the method is a write method and the methods list includes
        // Write. If so, return the method
        //
        if method.is_write_method() && self.methods.contains(&HttpMethod::Write)
        {
            return Some(method);
        }

        //
        // Check for a specific method in the methods list
        //
        match self.methods.contains(&method) {
            true => Some(method),
            false => None,
        }
    }

    /// Build a actix_web::http::Uri from itself.
    pub async fn build_uri(&self) -> Result<Uri, MappedErrors> {
        let service = match self.service {
            Parent::Record(ref service) => service,
            Parent::Id(_) => {
                return execution_err(
                    "Unexpected error on build URI: service not found",
                )
                .as_error()
            }
        };

        let host = service.to_owned().host.choose_host();
        let path_parts = host.split("/").collect::<Vec<&str>>();
        let domain = path_parts[0];

        match Uri::builder()
            .scheme(service.protocol.to_string().as_str())
            .authority(domain)
            .path_and_query(self.path.as_str())
            .build()
        {
            Err(err) => {
                execution_err(format!("Unexpected error on build URI: {}", err))
                    .as_error()
            }
            Ok(res) => Ok(res),
        }
    }

    /// Extend a Uri from a base Uri (deprecated).
    ///
    /// This function extends a Uri from a base Uri. It is deprecated because it
    /// is not used anymore.
    ///
    #[deprecated(since = "8.2.2-beta.2", note = "Use build_uri instead")]
    pub async fn extend_uri(
        uri: Uri,
        extension: PathAndQuery,
    ) -> Result<Uri, MappedErrors> {
        // Build the extended path
        let path = uri.path().to_owned() + extension.path();

        // Build parameters vector
        let params: &str = &vec![uri.query(), extension.query()]
            .into_iter()
            .filter_map(|p| p.map(|res| res))
            .collect::<Vec<&str>>()
            .join("&")
            .to_owned();

        // Join path with params if it exists
        let path_and_query = match params.chars().count() {
            0 => path,
            _ => path + "?" + params,
        };

        match Uri::builder()
            .scheme(uri.scheme().unwrap().to_string().as_str())
            .authority(uri.authority().unwrap().as_str())
            .path_and_query(path_and_query)
            .build()
        {
            Err(err) => {
                execution_err(format!("Unexpected error on build URI: {}", err))
                    .as_error()
            }
            Ok(res) => Ok(res),
        }
    }

    pub async fn solve_secret(
        &self,
    ) -> Result<Option<HttpSecret>, MappedErrors> {
        if let Some(secret_name) = &self.secret_name {
            match self.service.to_owned() {
                Parent::Id(_) => {
                    return dto_err(format!(
                        "Unable to solve secret (invalid service object): {secret_name}",
                        secret_name = secret_name
                    ))
                    .as_error();
                }
                Parent::Record(service) => match service.secrets {
                    Some(secret) => {
                        match secret.iter().find(|s| s.name == *secret_name) {
                            Some(secret) => {
                                let secret_resolver = &secret.secret;
                                let secret = secret_resolver
                                    .async_get_or_error()
                                    .await?;

                                return Ok(Some(secret));
                            }
                            None => {
                                return dto_err(format!(
                                    "Unable to solve secret (secret not available): {secret_name}",
                                    secret_name = secret_name
                                ))
                                .as_error();
                            }
                        }
                    }
                    None => {
                        return dto_err(format!(
                            "Unable to solve secret (service secrets is empty): {secret_name}",
                            secret_name = secret_name
                        ))
                        .as_error();
                    }
                },
            };
        }

        Ok(None)
    }

    pub fn get_service_id(&self) -> Uuid {
        match self.service.to_owned() {
            Parent::Id(id) => id,
            Parent::Record(record) => record.id,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::dtos::{
        health_check_info::HealthStatus,
        http::Protocol,
        service::{ServiceHost, ServiceSecret},
    };
    use mycelium_base::dtos::Parent;

    fn create_test_route(methods: Vec<HttpMethod>) -> Route {
        let service = Service {
            id: Uuid::new_v4(),
            name: "test-service".to_string(),
            host: ServiceHost::Host("localhost:8080".to_string()),
            protocol: Protocol::Http,
            routes: vec![],
            health_status: HealthStatus::Unknown,
            health_check_path: "/health".to_string(),
            discoverable: None,
            service_type: None,
            is_context_api: None,
            capabilities: None,
            description: None,
            openapi_path: None,
            secrets: None,
            allowed_sources: None,
            proxy_address: None,
        };

        Route {
            id: Some(Uuid::new_v4()),
            service: Parent::Record(service),
            security_group: SecurityGroup::Public,
            methods,
            path: "/test".to_string(),
            secret_name: None,
            accept_insecure_routing: None,
            callbacks: None,
        }
    }

    #[tokio::test]
    async fn test_allow_method_with_none_returns_none() {
        let route = create_test_route(vec![HttpMethod::None]);
        let result = route.allow_method(HttpMethod::Get).await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_none_and_other_methods_returns_none() {
        let route = create_test_route(vec![HttpMethod::None, HttpMethod::Get]);
        let result = route.allow_method(HttpMethod::Get).await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_all_returns_method() {
        let route = create_test_route(vec![HttpMethod::All]);
        let result = route.allow_method(HttpMethod::Get).await;
        assert_eq!(result, Some(HttpMethod::Get));
    }

    #[tokio::test]
    async fn test_allow_method_with_all_returns_any_method() {
        let route = create_test_route(vec![HttpMethod::All]);
        assert_eq!(
            route.allow_method(HttpMethod::Post).await,
            Some(HttpMethod::Post)
        );
        assert_eq!(
            route.allow_method(HttpMethod::Put).await,
            Some(HttpMethod::Put)
        );
        assert_eq!(
            route.allow_method(HttpMethod::Delete).await,
            Some(HttpMethod::Delete)
        );
        assert_eq!(
            route.allow_method(HttpMethod::Connect).await,
            Some(HttpMethod::Connect)
        );
    }

    #[tokio::test]
    async fn test_allow_method_with_read_allows_get() {
        let route = create_test_route(vec![HttpMethod::Read]);
        let result = route.allow_method(HttpMethod::Get).await;
        assert_eq!(result, Some(HttpMethod::Get));
    }

    #[tokio::test]
    async fn test_allow_method_with_read_allows_head() {
        let route = create_test_route(vec![HttpMethod::Read]);
        let result = route.allow_method(HttpMethod::Head).await;
        assert_eq!(result, Some(HttpMethod::Head));
    }

    #[tokio::test]
    async fn test_allow_method_with_read_allows_options() {
        let route = create_test_route(vec![HttpMethod::Read]);
        let result = route.allow_method(HttpMethod::Options).await;
        assert_eq!(result, Some(HttpMethod::Options));
    }

    #[tokio::test]
    async fn test_allow_method_with_read_allows_trace() {
        let route = create_test_route(vec![HttpMethod::Read]);
        let result = route.allow_method(HttpMethod::Trace).await;
        assert_eq!(result, Some(HttpMethod::Trace));
    }

    #[tokio::test]
    async fn test_allow_method_with_read_denies_post() {
        let route = create_test_route(vec![HttpMethod::Read]);
        let result = route.allow_method(HttpMethod::Post).await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_read_denies_write_methods() {
        let route = create_test_route(vec![HttpMethod::Read]);
        assert_eq!(route.allow_method(HttpMethod::Post).await, None);
        assert_eq!(route.allow_method(HttpMethod::Put).await, None);
        assert_eq!(route.allow_method(HttpMethod::Patch).await, None);
        assert_eq!(route.allow_method(HttpMethod::Delete).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_write_allows_post() {
        let route = create_test_route(vec![HttpMethod::Write]);
        let result = route.allow_method(HttpMethod::Post).await;
        assert_eq!(result, Some(HttpMethod::Post));
    }

    #[tokio::test]
    async fn test_allow_method_with_write_allows_put() {
        let route = create_test_route(vec![HttpMethod::Write]);
        let result = route.allow_method(HttpMethod::Put).await;
        assert_eq!(result, Some(HttpMethod::Put));
    }

    #[tokio::test]
    async fn test_allow_method_with_write_allows_patch() {
        let route = create_test_route(vec![HttpMethod::Write]);
        let result = route.allow_method(HttpMethod::Patch).await;
        assert_eq!(result, Some(HttpMethod::Patch));
    }

    #[tokio::test]
    async fn test_allow_method_with_write_allows_delete() {
        let route = create_test_route(vec![HttpMethod::Write]);
        let result = route.allow_method(HttpMethod::Delete).await;
        assert_eq!(result, Some(HttpMethod::Delete));
    }

    #[tokio::test]
    async fn test_allow_method_with_write_denies_get() {
        let route = create_test_route(vec![HttpMethod::Write]);
        let result = route.allow_method(HttpMethod::Get).await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_write_denies_read_methods() {
        let route = create_test_route(vec![HttpMethod::Write]);
        assert_eq!(route.allow_method(HttpMethod::Get).await, None);
        assert_eq!(route.allow_method(HttpMethod::Head).await, None);
        assert_eq!(route.allow_method(HttpMethod::Options).await, None);
        assert_eq!(route.allow_method(HttpMethod::Trace).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_specific_method_allows_it() {
        let route = create_test_route(vec![HttpMethod::Get]);
        let result = route.allow_method(HttpMethod::Get).await;
        assert_eq!(result, Some(HttpMethod::Get));
    }

    #[tokio::test]
    async fn test_allow_method_with_specific_method_denies_others() {
        let route = create_test_route(vec![HttpMethod::Get]);
        assert_eq!(route.allow_method(HttpMethod::Post).await, None);
        assert_eq!(route.allow_method(HttpMethod::Put).await, None);
        assert_eq!(route.allow_method(HttpMethod::Delete).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_multiple_specific_methods() {
        let route = create_test_route(vec![HttpMethod::Get, HttpMethod::Post]);
        assert_eq!(
            route.allow_method(HttpMethod::Get).await,
            Some(HttpMethod::Get)
        );
        assert_eq!(
            route.allow_method(HttpMethod::Post).await,
            Some(HttpMethod::Post)
        );
        assert_eq!(route.allow_method(HttpMethod::Put).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_read_and_specific_method() {
        let route = create_test_route(vec![HttpMethod::Read, HttpMethod::Post]);
        // Read methods should be allowed
        assert_eq!(
            route.allow_method(HttpMethod::Get).await,
            Some(HttpMethod::Get)
        );
        assert_eq!(
            route.allow_method(HttpMethod::Head).await,
            Some(HttpMethod::Head)
        );
        // Specific write method should be allowed
        assert_eq!(
            route.allow_method(HttpMethod::Post).await,
            Some(HttpMethod::Post)
        );
        // Other write methods should not be allowed
        assert_eq!(route.allow_method(HttpMethod::Put).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_write_and_specific_method() {
        let route = create_test_route(vec![HttpMethod::Write, HttpMethod::Get]);
        // Write methods should be allowed
        assert_eq!(
            route.allow_method(HttpMethod::Post).await,
            Some(HttpMethod::Post)
        );
        assert_eq!(
            route.allow_method(HttpMethod::Put).await,
            Some(HttpMethod::Put)
        );
        // Specific read method should be allowed
        assert_eq!(
            route.allow_method(HttpMethod::Get).await,
            Some(HttpMethod::Get)
        );
        // Other read methods should not be allowed
        assert_eq!(route.allow_method(HttpMethod::Head).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_connect_not_read_or_write() {
        let route =
            create_test_route(vec![HttpMethod::Read, HttpMethod::Write]);
        // Connect is neither read nor write, so it should not be allowed
        assert_eq!(route.allow_method(HttpMethod::Connect).await, None);
    }

    #[tokio::test]
    async fn test_allow_method_with_connect_specific() {
        let route = create_test_route(vec![HttpMethod::Connect]);
        // Connect should be allowed if explicitly specified
        assert_eq!(
            route.allow_method(HttpMethod::Connect).await,
            Some(HttpMethod::Connect)
        );
    }

    #[tokio::test]
    async fn test_allow_method_empty_methods_list() {
        let route = create_test_route(vec![]);
        assert_eq!(route.allow_method(HttpMethod::Get).await, None);
        assert_eq!(route.allow_method(HttpMethod::Post).await, None);
    }

    // ? -----------------------------------------------------------------------
    // ? Tests for solve_secret method
    // ? -----------------------------------------------------------------------

    fn create_test_route_with_secret_name(
        secret_name: Option<String>,
        service: Parent<Service, Uuid>,
    ) -> Route {
        Route {
            id: Some(Uuid::new_v4()),
            service,
            security_group: SecurityGroup::Public,
            methods: vec![HttpMethod::Get],
            path: "/test".to_string(),
            secret_name,
            accept_insecure_routing: None,
            callbacks: None,
        }
    }

    fn create_test_service_with_secrets(
        secrets: Option<Vec<ServiceSecret>>,
    ) -> Service {
        Service {
            id: Uuid::new_v4(),
            name: "test-service".to_string(),
            host: ServiceHost::Host("localhost:8080".to_string()),
            protocol: Protocol::Http,
            routes: vec![],
            health_status: HealthStatus::Unknown,
            health_check_path: "/health".to_string(),
            discoverable: None,
            service_type: None,
            is_context_api: None,
            capabilities: None,
            description: None,
            openapi_path: None,
            secrets,
            allowed_sources: None,
            proxy_address: None,
        }
    }

    #[tokio::test]
    async fn test_solve_secret_without_secret_name_returns_none() {
        let service = create_test_service_with_secrets(None);
        let route =
            create_test_route_with_secret_name(None, Parent::Record(service));
        let result = route.solve_secret().await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), None);
    }

    #[tokio::test]
    async fn test_solve_secret_with_service_id_returns_error() {
        let route = create_test_route_with_secret_name(
            Some("test-secret".to_string()),
            Parent::Id(Uuid::new_v4()),
        );
        let result = route.solve_secret().await;
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error
            .to_string()
            .contains("Unable to solve secret (invalid service object)"));
        assert!(error.to_string().contains("test-secret"));
    }

    #[tokio::test]
    async fn test_solve_secret_with_empty_secrets_returns_error() {
        let service = create_test_service_with_secrets(None);
        let route = create_test_route_with_secret_name(
            Some("test-secret".to_string()),
            Parent::Record(service),
        );
        let result = route.solve_secret().await;
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error
            .to_string()
            .contains("Unable to solve secret (service secrets is empty)"));
        assert!(error.to_string().contains("test-secret"));
    }

    #[tokio::test]
    async fn test_solve_secret_with_secret_not_found_returns_error() {
        use myc_config::secret_resolver::SecretResolver;

        let secret = HttpSecret::AuthorizationHeader {
            header_name: Some("Authorization".to_string()),
            prefix: Some("Bearer".to_string()),
            token: "token123".to_string(),
        };

        let service_secrets = vec![ServiceSecret {
            name: "other-secret".to_string(),
            secret: SecretResolver::Value(secret),
        }];

        let service = create_test_service_with_secrets(Some(service_secrets));
        let route = create_test_route_with_secret_name(
            Some("test-secret".to_string()),
            Parent::Record(service),
        );
        let result = route.solve_secret().await;
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error
            .to_string()
            .contains("Unable to solve secret (secret not available)"));
        assert!(error.to_string().contains("test-secret"));
    }

    #[tokio::test]
    async fn test_solve_secret_with_secret_found_returns_secret() {
        use myc_config::secret_resolver::SecretResolver;

        let secret = HttpSecret::AuthorizationHeader {
            header_name: Some("Authorization".to_string()),
            prefix: Some("Bearer".to_string()),
            token: "token123".to_string(),
        };

        let service_secrets = vec![ServiceSecret {
            name: "test-secret".to_string(),
            secret: SecretResolver::Value(secret.clone()),
        }];

        let service = create_test_service_with_secrets(Some(service_secrets));
        let route = create_test_route_with_secret_name(
            Some("test-secret".to_string()),
            Parent::Record(service),
        );
        let result = route.solve_secret().await;
        assert!(result.is_ok());
        let resolved_secret = result.unwrap();
        assert!(resolved_secret.is_some());
        assert_eq!(resolved_secret.unwrap(), secret);
    }

    #[tokio::test]
    async fn test_solve_secret_with_multiple_secrets_finds_correct_one() {
        use myc_config::secret_resolver::SecretResolver;

        let secret1 = HttpSecret::AuthorizationHeader {
            header_name: Some("Authorization".to_string()),
            prefix: Some("Bearer".to_string()),
            token: "token1".to_string(),
        };

        let secret2 = HttpSecret::QueryParameter {
            name: "api_key".to_string(),
            token: "key123".to_string(),
        };

        let service_secrets = vec![
            ServiceSecret {
                name: "secret1".to_string(),
                secret: SecretResolver::Value(secret1),
            },
            ServiceSecret {
                name: "secret2".to_string(),
                secret: SecretResolver::Value(secret2.clone()),
            },
        ];

        let service = create_test_service_with_secrets(Some(service_secrets));
        let route = create_test_route_with_secret_name(
            Some("secret2".to_string()),
            Parent::Record(service),
        );
        let result = route.solve_secret().await;
        assert!(result.is_ok());
        let resolved_secret = result.unwrap();
        assert!(resolved_secret.is_some());
        assert_eq!(resolved_secret.unwrap(), secret2);
    }

    #[tokio::test]
    async fn test_solve_secret_with_query_parameter_secret() {
        use myc_config::secret_resolver::SecretResolver;

        let secret = HttpSecret::QueryParameter {
            name: "api_key".to_string(),
            token: "key123".to_string(),
        };

        let service_secrets = vec![ServiceSecret {
            name: "test-secret".to_string(),
            secret: SecretResolver::Value(secret.clone()),
        }];

        let service = create_test_service_with_secrets(Some(service_secrets));
        let route = create_test_route_with_secret_name(
            Some("test-secret".to_string()),
            Parent::Record(service),
        );
        let result = route.solve_secret().await;
        assert!(result.is_ok());
        let resolved_secret = result.unwrap();
        assert!(resolved_secret.is_some());
        assert_eq!(resolved_secret.unwrap(), secret);
    }
}