Skip to main content

camel_component_mock/
expectations.rs

1//! Expectations recorded on a mock endpoint for batch-style assertion.
2
3use camel_matchers::CountBound;
4
5use crate::matcher::{BodyMatcher, HeaderMatcher};
6
7/// One ordered entry in the expected-body list.
8///
9/// The two setters share one insertion-ordered list: mixed sequences of
10/// [`crate::MockEndpointInner::expect_body`] and
11/// [`crate::MockEndpointInner::expect_body_matcher`] keep their slots.
12pub(crate) enum BodyExpectation {
13    /// Exact body equality.
14    Exact(camel_component_api::Body),
15    /// Matcher evaluation.
16    Matcher(BodyMatcher),
17}
18
19/// Expectations set on a mock endpoint for batch-style assertion.
20///
21/// Use [`crate::MockEndpointInner::expect_body`],
22/// [`crate::MockEndpointInner::expect_body_matcher`],
23/// [`crate::MockEndpointInner::expect_header`] and
24/// [`crate::MockEndpointInner::expect_header_matcher`] to populate
25/// expectations, then call [`crate::MockEndpointInner::assert_satisfied`]
26/// after exchanges have been received.
27pub struct MockExpectations {
28    pub(crate) expected_bodies: Vec<BodyExpectation>,
29    pub(crate) expected_headers: Vec<(String, serde_json::Value)>,
30    pub(crate) expected_header_regexes: Vec<(String, String)>,
31    pub(crate) expected_header_matchers: Vec<(String, HeaderMatcher)>,
32    /// Count bound (from the shared `camel_matchers` algebra) enforced by
33    /// [`crate::MockEndpointInner::assert_satisfied`]; `None` means no count
34    /// expectation. One bound per endpoint: a later setter replaces the
35    /// earlier one.
36    pub(crate) count_bound: Option<CountBound>,
37}
38
39impl Default for MockExpectations {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl MockExpectations {
46    /// Create an empty set of expectations.
47    pub fn new() -> Self {
48        Self {
49            expected_bodies: Vec::new(),
50            expected_headers: Vec::new(),
51            expected_header_regexes: Vec::new(),
52            expected_header_matchers: Vec::new(),
53            count_bound: 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 count bound enforced at assertion time. Replaces any
83    /// previously recorded bound (one bound per endpoint).
84    pub(crate) fn set_count_bound(&mut self, bound: CountBound) {
85        self.count_bound = Some(bound);
86    }
87}