This commit is contained in:
Phil Booth 2018-11-15 06:52:52 +00:00
Родитель 67320d2830
Коммит 3e27866763
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 36FBB106F9C32516
2 изменённых файлов: 59 добавлений и 46 удалений

Просмотреть файл

@ -6,7 +6,7 @@
use std::{boxed::Box, collections::HashMap, convert::TryFrom};
use emailmessage::{header::ContentType, Message, MessageBuilder, MultiPart, SinglePart};
use emailmessage::{header::ContentType, Mailbox, Message, MessageBuilder, MultiPart, SinglePart};
use self::{
mock::MockProvider as Mock, sendgrid::SendgridProvider as Sendgrid, ses::SesProvider as Ses,
@ -40,16 +40,17 @@ fn build_multipart_mime<'a>(
body_text: &'a str,
body_html: Option<&'a str>,
) -> AppResult<Message<MultiPart<&'a str>>> {
let mut message =
Message::builder()
.from(sender.parse().map_err(|_| {
AppErrorKind::InvalidPayload(format!("`from` address \"{}\"", sender))
})?)
.to(to
.parse()
.map_err(|_| AppErrorKind::InvalidPayload(format!("`to` address \"{}\"", to)))?)
.subject(subject)
.mime_1_0();
let sender: Mailbox = sender
.parse()
.map_err(|_| AppErrorKind::InvalidPayload(format!("`from` address \"{}\"", sender)))?;
let mut message = Message::builder()
.sender(sender.clone())
.from(sender)
.to(to
.parse()
.map_err(|_| AppErrorKind::InvalidPayload(format!("`to` address \"{}\"", to)))?)
.subject(subject)
.mime_1_0();
if cc.len() > 0 {
for address in cc.iter() {

Просмотреть файл

@ -8,19 +8,28 @@ use super::*;
#[test]
fn build_mime_without_optional_data() {
let message =
build_multipart_mime("a@a.com", "b@b.com", &[], None, "subject", "body", None).unwrap();
let message = build_multipart_mime(
"Wibble Blee <a@a.com>",
"b@b.com",
&[],
None,
"subject",
"body",
None,
)
.unwrap();
let message: Vec<String> = format!("{}", message)
.split("\r\n")
.map(|s| s.to_owned())
.collect();
assert_eq!("From: a@a.com", &message[0]);
assert_eq!("To: b@b.com", &message[1]);
assert_eq!("Subject: subject", &message[2]);
assert_eq!("MIME-Version: 1.0", &message[3]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[10]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[11]);
assert_eq!("body", &message[13]);
assert_eq!("Sender: Wibble Blee <a@a.com>", &message[0]);
assert_eq!("From: Wibble Blee <a@a.com>", &message[1]);
assert_eq!("To: b@b.com", &message[2]);
assert_eq!("Subject: subject", &message[3]);
assert_eq!("MIME-Version: 1.0", &message[4]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[11]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[12]);
assert_eq!("body", &message[14]);
}
#[test]
@ -39,14 +48,15 @@ fn build_mime_with_cc_headers() {
.split("\r\n")
.map(|s| s.to_owned())
.collect();
assert_eq!("From: a@a.com", &message[0]);
assert_eq!("To: b@b.com", &message[1]);
assert_eq!("Subject: subject", &message[2]);
assert_eq!("MIME-Version: 1.0", &message[3]);
assert_eq!("Cc: c@c.com, d@d.com", &message[4]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[11]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[12]);
assert_eq!("body", &message[14]);
assert_eq!("Sender: a@a.com", &message[0]);
assert_eq!("From: a@a.com", &message[1]);
assert_eq!("To: b@b.com", &message[2]);
assert_eq!("Subject: subject", &message[3]);
assert_eq!("MIME-Version: 1.0", &message[4]);
assert_eq!("Cc: c@c.com, d@d.com", &message[5]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[12]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[13]);
assert_eq!("body", &message[15]);
}
#[test]
@ -68,14 +78,15 @@ fn build_mime_with_custom_headers() {
.split("\r\n")
.map(|s| s.to_owned())
.collect();
assert_eq!("From: a@a.com", &message[0]);
assert_eq!("To: b@b.com", &message[1]);
assert_eq!("Subject: subject", &message[2]);
assert_eq!("MIME-Version: 1.0", &message[3]);
assert_eq!("X-Device-Id: baz", &message[4]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[11]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[12]);
assert_eq!("body", &message[14]);
assert_eq!("Sender: a@a.com", &message[0]);
assert_eq!("From: a@a.com", &message[1]);
assert_eq!("To: b@b.com", &message[2]);
assert_eq!("Subject: subject", &message[3]);
assert_eq!("MIME-Version: 1.0", &message[4]);
assert_eq!("X-Device-Id: baz", &message[5]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[12]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[13]);
assert_eq!("body", &message[15]);
}
#[test]
@ -94,16 +105,17 @@ fn build_mime_with_body_html() {
.split("\r\n")
.map(|s| s.to_owned())
.collect();
assert_eq!("From: a@a.com", &message[0]);
assert_eq!("To: b@b.com", &message[1]);
assert_eq!("Subject: subject", &message[2]);
assert_eq!("MIME-Version: 1.0", &message[3]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[10]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[11]);
assert_eq!("body", &message[13]);
assert_eq!("Content-Transfer-Encoding: base64", &message[18]);
assert_eq!("Content-Type: text/html; charset=utf-8", &message[19]);
assert_eq!("PHA+Ym9keTwvcD4=", &message[21]);
assert_eq!("Sender: a@a.com", &message[0]);
assert_eq!("From: a@a.com", &message[1]);
assert_eq!("To: b@b.com", &message[2]);
assert_eq!("Subject: subject", &message[3]);
assert_eq!("MIME-Version: 1.0", &message[4]);
assert_eq!("Content-Transfer-Encoding: quoted-printable", &message[11]);
assert_eq!("Content-Type: text/plain; charset=utf-8", &message[12]);
assert_eq!("body", &message[14]);
assert_eq!("Content-Transfer-Encoding: base64", &message[19]);
assert_eq!("Content-Type: text/html; charset=utf-8", &message[20]);
assert_eq!("PHA+Ym9keTwvcD4=", &message[22]);
}
#[test]