1 parent
a238ff5e23
commit
93762fe9ef
65 files changed
+5420
-2792
No files matched your search
+25
-1
@@ -55,6 +55,10 @@ fn clone_error(err: &SmtpError) -> SmtpError {
|
||||
SmtpError::ConnectionFailed(msg) => SmtpError::ConnectionFailed(msg.clone()),
|
||||
SmtpError::AuthenticationFailed => SmtpError::AuthenticationFailed,
|
||||
SmtpError::SendFailed(msg) => SmtpError::SendFailed(msg.clone()),
|
||||
SmtpError::MessageTooLarge { actual, limit } => SmtpError::MessageTooLarge {
|
||||
actual: *actual,
|
||||
limit: *limit,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,10 +72,16 @@ impl SmtpClient for MockSmtpClient {
|
||||
if let Some(ref err) = *self.should_fail.lock().unwrap() {
|
||||
return Err(clone_error(err));
|
||||
}
|
||||
self.sent_messages.lock().unwrap().push(message.clone());
|
||||
let message_id = format!("<mock-{}>", uuid::Uuid::new_v4());
|
||||
let rfc822 = crate::smtp::builder::build_rfc822(message, &message_id)
|
||||
.map_err(SmtpError::SendFailed)?;
|
||||
if message.max_message_size_bytes > 0 && rfc822.len() > message.max_message_size_bytes {
|
||||
return Err(SmtpError::MessageTooLarge {
|
||||
actual: rfc822.len(),
|
||||
limit: message.max_message_size_bytes,
|
||||
});
|
||||
}
|
||||
self.sent_messages.lock().unwrap().push(message.clone());
|
||||
Ok(SmtpSendResult { message_id, rfc822 })
|
||||
}
|
||||
}
|
||||
@@ -90,6 +100,7 @@ mod tests {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
tls: true,
|
||||
tls_verify: true,
|
||||
email: "user@example.com".to_string(),
|
||||
password: "hunter2".to_string(),
|
||||
}
|
||||
@@ -114,6 +125,7 @@ mod tests {
|
||||
extra_headers: vec![],
|
||||
attachments: vec![],
|
||||
pgp: None,
|
||||
max_message_size_bytes: 50 * 1024 * 1024,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +139,16 @@ mod tests {
|
||||
assert!(!result.rfc822.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn mock_rejects_message_larger_than_final_mime_limit() {
|
||||
let mock = MockSmtpClient::new();
|
||||
let mut msg = test_message();
|
||||
msg.max_message_size_bytes = 1;
|
||||
let err = mock.send_message(&test_creds(), &msg).await.unwrap_err();
|
||||
assert!(matches!(err, SmtpError::MessageTooLarge { .. }));
|
||||
assert_eq!(mock.sent_count(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn mock_send_captures_message() {
|
||||
let mock = MockSmtpClient::new();
|
||||
@@ -152,6 +174,7 @@ mod tests {
|
||||
content_id: None,
|
||||
}],
|
||||
pgp: None,
|
||||
max_message_size_bytes: 50 * 1024 * 1024,
|
||||
};
|
||||
|
||||
let result = mock.send_message(&test_creds(), &msg).await;
|
||||
@@ -205,6 +228,7 @@ mod tests {
|
||||
host: "invalid.host.test".to_string(),
|
||||
port: 587,
|
||||
tls: true,
|
||||
tls_verify: true,
|
||||
email: "user@invalid.host.test".to_string(),
|
||||
password: "password".to_string(),
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user