fix(transport-stub): Make Stub transport independent from SMTP
This commit is contained in:
@@ -25,33 +25,30 @@
|
||||
|
||||
use EmailTransport;
|
||||
use SendableEmail;
|
||||
use smtp::error::{Error, SmtpResult};
|
||||
use smtp::response::{Code, Response};
|
||||
use std::io::Read;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// This transport logs the message envelope and returns the given response
|
||||
#[derive(Debug)]
|
||||
pub struct StubEmailTransport {
|
||||
response: Response,
|
||||
response: StubResult,
|
||||
}
|
||||
|
||||
impl StubEmailTransport {
|
||||
/// Creates a new transport that always returns the given response
|
||||
pub fn new(response: Response) -> StubEmailTransport {
|
||||
pub fn new(response: StubResult) -> StubEmailTransport {
|
||||
StubEmailTransport { response: response }
|
||||
}
|
||||
|
||||
/// Creates a new transport that always returns a success response
|
||||
pub fn new_positive() -> StubEmailTransport {
|
||||
StubEmailTransport {
|
||||
response: Response::new(Code::from_str("200").unwrap(), vec!["ok".to_string()]),
|
||||
response: Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// SMTP result type
|
||||
pub type StubResult = SmtpResult;
|
||||
pub type StubResult = Result<(), ()>;
|
||||
|
||||
impl<'a, T: Read + 'a> EmailTransport<'a, T, StubResult> for StubEmailTransport {
|
||||
fn send<U: SendableEmail<'a, T>>(&mut self, email: &'a U) -> StubResult {
|
||||
@@ -62,10 +59,6 @@ impl<'a, T: Read + 'a> EmailTransport<'a, T, StubResult> for StubEmailTransport
|
||||
email.from(),
|
||||
email.to()
|
||||
);
|
||||
if self.response.is_positive() {
|
||||
Ok(self.response.clone())
|
||||
} else {
|
||||
Err(Error::from(self.response.clone()))
|
||||
}
|
||||
self.response
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
extern crate lettre;
|
||||
|
||||
use lettre::{EmailAddress, EmailTransport, SimpleSendableEmail};
|
||||
use lettre::smtp::response::{Code, Response};
|
||||
use lettre::stub::StubEmailTransport;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[test]
|
||||
fn stub_transport() {
|
||||
let mut sender_ok = StubEmailTransport::new_positive();
|
||||
let response_ok = Response::new(Code::from_str("200").unwrap(), vec!["ok".to_string()]);
|
||||
let response_ko = Response::new(Code::from_str("510").unwrap(), vec!["ko".to_string()]);
|
||||
let mut sender_ko = StubEmailTransport::new(response_ko);
|
||||
let mut sender_ko = StubEmailTransport::new(Err(()));
|
||||
|
||||
let email = SimpleSendableEmail::new(
|
||||
EmailAddress::new("user@localhost".to_string()),
|
||||
@@ -19,10 +15,6 @@ fn stub_transport() {
|
||||
"Hello stub".to_string(),
|
||||
);
|
||||
|
||||
let result_ok = sender_ok.send(&email).unwrap();
|
||||
let result_ko = sender_ko.send(&email);
|
||||
|
||||
assert_eq!(result_ok, response_ok);
|
||||
assert!(result_ko.is_err());
|
||||
|
||||
sender_ok.send(&email).unwrap();
|
||||
sender_ko.send(&email).unwrap_err();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user