Skip to main content

camel_component_mock/
expectations.rs

1//! Expectations recorded on a mock endpoint for batch-style assertion.
2
3use crate::matcher::{BodyMatcher, HeaderMatcher};
4
5/// One ordered entry in the expected-body list.
6///
7/// The two setters share one insertion-ordered list: mixed sequences of
8/// [`crate::MockEndpointInner::expect_body`] and
9/// [`crate::MockEndpointInner::expect_body_matcher`] keep their slots.
10pub(crate) enum BodyExpectation {
11    /// Exact body equality.
12    Exact(camel_component_api::Body),
13    /// Matcher evaluation.
14    Matcher(BodyMatcher),
15}
16
17/// Expectations set on a mock endpoint for batch-style assertion.
18///
19/// Use [`crate::MockEndpointInner::expect_body`],
20/// [`crate::MockEndpointInner::expect_body_matcher`],
21/// [`crate::MockEndpointInner::expect_header`] and
22/// [`crate::MockEndpointInner::expect_header_matcher`] to populate
23/// expectations, then call [`crate::MockEndpointInner::assert_satisfied`]
24/// after exchanges have been received.
25pub struct MockExpectations {
26    pub(crate) expected_bodies: Vec<BodyExpectation>,
27    pub(crate) expected_headers: Vec<(String, serde_json::Value)>,
28    pub(crate) expected_header_regexes: Vec<(String, String)>,
29    pub(crate) expected_header_matchers: Vec<(String, HeaderMatcher)>,
30    /// Exact exchange-count expectation enforced by
31    /// [`crate::MockEndpointInner::assert_satisfied`].
32    pub(crate) expected_count: Option<usize>,
33    /// Minimum exchange-count expectation enforced by
34    /// [`crate::MockEndpointInner::assert_satisfied`].
35    pub(crate) minimum_count: Option<usize>,
36}
37
38impl Default for MockExpectations {
39    fn default() -> Self {
40        Self::new()
41    }
42}
43
44impl MockExpectations {
45    /// Create an empty set of expectations.
46    pub fn new() -> Self {
47        Self {
48            expected_bodies: Vec::new(),
49            expected_headers: Vec::new(),
50            expected_header_regexes: Vec::new(),
51            expected_header_matchers: Vec::new(),
52            expected_count: None,
53            minimum_count: None,
54        }
55    }
56
57    /// Add an expected body value.
58    pub fn push_body(&mut self, body: camel_component_api::Body) {
59        self.expected_bodies.push(BodyExpectation::Exact(body));
60    }
61
62    /// Add an expected body matcher.
63    pub fn push_body_matcher(&mut self, matcher: BodyMatcher) {
64        self.expected_bodies.push(BodyExpectation::Matcher(matcher));
65    }
66
67    /// Add an expected header key-value pair.
68    pub fn push_header(&mut self, key: String, value: serde_json::Value) {
69        self.expected_headers.push((key, value));
70    }
71
72    /// Add an expected header regex pattern.
73    pub fn push_header_regex(&mut self, key: String, pattern: String) {
74        self.expected_header_regexes.push((key, pattern));
75    }
76
77    /// Add an expected header matcher.
78    pub fn push_header_matcher(&mut self, key: String, matcher: HeaderMatcher) {
79        self.expected_header_matchers.push((key, matcher));
80    }
81
82    /// Set the exact expected exchange count.
83    pub(crate) fn set_expected_count(&mut self, n: usize) {
84        self.expected_count = Some(n);
85    }
86
87    /// Set the minimum expected exchange count.
88    pub(crate) fn set_minimum_count(&mut self, n: usize) {
89        self.minimum_count = Some(n);
90    }
91}