expect_json/expect/ops/
expect_email.rs

1use crate::JsonType;
2use crate::expect_core::Context;
3use crate::expect_core::ExpectOp;
4use crate::expect_core::ExpectOpError;
5use crate::expect_core::ExpectOpResult;
6use crate::expect_core::expect_op;
7use core::str::FromStr;
8use email_address::EmailAddress;
9
10///
11/// Expects a valid email address string.
12///
13/// You can build these using the [`crate::expect::email`] function.
14///
15#[expect_op(internal, name = "email")]
16#[derive(Debug, Clone, Default, PartialEq)]
17pub struct ExpectEmail {
18    expected_domain: Option<String>,
19    expected_local_part: Option<String>,
20}
21
22impl ExpectEmail {
23    pub(crate) fn new() -> Self {
24        Self {
25            expected_domain: None,
26            expected_local_part: None,
27        }
28    }
29
30    ///
31    /// Expects the local part of the email address.
32    /// The 'local part' is the part before the '@' symbol.
33    /// i.e. the 'joe' in 'joe@example.com'.
34    ///
35    /// ```rust
36    /// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
37    /// #
38    /// # use axum::Router;
39    /// # use axum::extract::Json;
40    /// # use axum::routing::get;
41    /// # use axum_test::TestServer;
42    /// # use serde_json::json;
43    /// #
44    /// # let server = TestServer::new(Router::new())?;
45    /// #
46    /// use axum_test::expect_json;
47    ///
48    /// let server = TestServer::new(Router::new())?;
49    ///
50    /// server.get(&"/user")
51    ///     .await
52    ///     .assert_json(&json!({
53    ///         "name": "Joe",
54    ///         "email": expect_json::email().local_part("joe"),
55    ///     }));
56    /// #
57    /// # Ok(()) }
58    /// ```
59    ///
60    pub fn local_part<S>(mut self, local_part: S) -> Self
61    where
62        S: Into<String>,
63    {
64        self.expected_local_part = Some(local_part.into());
65        self
66    }
67
68    ///
69    /// Expects the domain part of the email address.
70    /// i.e. 'example.com' in 'joe@example.com'.
71    ///
72    /// ```rust
73    /// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
74    /// #
75    /// # use axum::Router;
76    /// # use axum::extract::Json;
77    /// # use axum::routing::get;
78    /// # use axum_test::TestServer;
79    /// # use serde_json::json;
80    /// #
81    /// # let server = TestServer::new(Router::new())?;
82    /// #
83    /// use axum_test::expect_json;
84    ///
85    /// let server = TestServer::new(Router::new())?;
86    ///
87    /// server.get(&"/user")
88    ///     .await
89    ///     .assert_json(&json!({
90    ///         "name": "Joe",
91    ///         "email": expect_json::email().domain("example.com"),
92    ///     }));
93    /// #
94    /// # Ok(()) }
95    /// ```
96    ///
97    pub fn domain<S>(mut self, domain: S) -> Self
98    where
99        S: Into<String>,
100    {
101        self.expected_domain = Some(domain.into());
102        self
103    }
104}
105
106impl ExpectOp for ExpectEmail {
107    fn on_string(&self, context: &mut Context, received: &str) -> ExpectOpResult<()> {
108        let email = EmailAddress::from_str(received).map_err(|e| {
109            let error_message = format!("Invalid email address, received '{received}'");
110            ExpectOpError::custom_error(self, context, error_message, e)
111        })?;
112
113        if let Some(expected_local_part) = &self.expected_local_part {
114            if email.local_part() != expected_local_part {
115                return Err(ExpectOpError::custom(
116                    self,
117                    context,
118                    format!(
119                        "Local part mismatch, expected '{expected_local_part}', received '{received}'"
120                    ),
121                ));
122            }
123        }
124
125        if let Some(expected_domain) = &self.expected_domain {
126            if email.domain() != expected_domain {
127                return Err(ExpectOpError::custom(
128                    self,
129                    context,
130                    format!("Domain mismatch, expected '{expected_domain}', received '{received}'"),
131                ));
132            }
133        }
134
135        Ok(())
136    }
137
138    fn debug_supported_types(&self) -> &'static [JsonType] {
139        &[JsonType::String]
140    }
141}
142
143#[cfg(test)]
144mod test_email {
145    use crate::expect;
146    use crate::expect_json_eq;
147    use pretty_assertions::assert_eq;
148    use serde_json::json;
149
150    #[test]
151    fn it_should_accept_valid_email() {
152        let left = json!("test@example.com");
153        let right = json!(expect::email());
154
155        let output = expect_json_eq(&left, &right);
156        assert!(output.is_ok());
157    }
158
159    #[test]
160    fn it_should_accept_valid_email_with_plus_sign() {
161        let left = json!("test+test@example.com");
162        let right = json!(expect::email());
163
164        let output = expect_json_eq(&left, &right);
165        assert!(output.is_ok());
166    }
167
168    #[test]
169    fn it_should_accept_email_inside_object() {
170        let left = json!({ "name": "Joe", "email": "test@example.com" });
171        let right = json!({
172            "name": "Joe",
173            "email": expect::email(),
174        });
175
176        let output = expect_json_eq(&left, &right);
177        assert!(output.is_ok());
178    }
179
180    #[test]
181    fn it_should_reject_invalid_email() {
182        let left = json!("🦊");
183        let right = json!(expect::email());
184
185        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
186        assert_eq!(
187            output,
188            r#"Json expect::email() error at root:
189    Invalid email address, received '🦊',
190    Missing separator character '@'."#
191        );
192    }
193}
194
195#[cfg(test)]
196mod test_local_part {
197    use crate::expect;
198    use crate::expect_json_eq;
199    use pretty_assertions::assert_eq;
200    use serde_json::json;
201
202    #[test]
203    fn it_should_accept_valid_local_part() {
204        let left = json!("test@example.com");
205        let right = json!(expect::email().local_part("test"));
206
207        let output = expect_json_eq(&left, &right);
208        assert!(output.is_ok());
209    }
210
211    #[test]
212    fn it_should_reject_invalid_local_part() {
213        let left = json!("test@example.com");
214        let right = json!(expect::email().local_part("🦊"));
215
216        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
217        assert_eq!(
218            output,
219            r#"Json expect::email() error at root:
220    Local part mismatch, expected '🦊', received 'test@example.com'"#
221        );
222    }
223}
224
225#[cfg(test)]
226mod test_domain {
227    use crate::expect;
228    use crate::expect_json_eq;
229    use pretty_assertions::assert_eq;
230    use serde_json::json;
231
232    #[test]
233    fn it_should_accept_valid_domain() {
234        let left = json!("test@example.com");
235        let right = json!(expect::email().domain("example.com"));
236
237        let output = expect_json_eq(&left, &right);
238        assert!(output.is_ok());
239    }
240
241    #[test]
242    fn it_should_reject_invalid_domain() {
243        let left = json!("test@example.com");
244        let right = json!(expect::email().domain("🦊.fox"));
245
246        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
247        assert_eq!(
248            output,
249            r#"Json expect::email() error at root:
250    Domain mismatch, expected '🦊.fox', received 'test@example.com'"#
251        );
252    }
253}