Merge pull request #168 from amousset/allow-chosing-stub-response
feat(transport): Allow specifying a response for stub transport
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
//! Error and result type for file transport
|
||||
|
||||
use self::Error::*;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
/// An enum of all error kinds.
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// Internal client error
|
||||
Client(&'static str),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
|
||||
fmt.write_str(self.description())
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Client(_) => "an unknown error occured",
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&StdError> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for Error {
|
||||
fn from(string: &'static str) -> Error {
|
||||
Client(string)
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
//! "Hello world"
|
||||
//! );
|
||||
//!
|
||||
//! let mut sender = StubEmailTransport;
|
||||
//! let mut sender = StubEmailTransport::new_positive();
|
||||
//! let result = sender.send(email);
|
||||
//! assert!(result.is_ok());
|
||||
//! ```
|
||||
@@ -25,15 +25,34 @@
|
||||
|
||||
use EmailTransport;
|
||||
use SendableEmail;
|
||||
use smtp::response::{Code, Response};
|
||||
use smtp::error::{Error, SmtpResult};
|
||||
use std::str::FromStr;
|
||||
|
||||
pub mod error;
|
||||
|
||||
/// This transport does nothing except logging the message envelope
|
||||
/// This transport logs the message envelope and returns the given response
|
||||
#[derive(Debug)]
|
||||
pub struct StubEmailTransport;
|
||||
pub struct StubEmailTransport {
|
||||
response: Response,
|
||||
}
|
||||
|
||||
impl StubEmailTransport {
|
||||
/// Creates a new transport that always returns the given response
|
||||
pub fn new(response: Response) -> 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()])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// SMTP result type
|
||||
pub type StubResult = Result<(), error::Error>;
|
||||
pub type StubResult = SmtpResult;
|
||||
|
||||
impl EmailTransport<StubResult> for StubEmailTransport {
|
||||
fn send<T: SendableEmail>(&mut self, email: T) -> StubResult {
|
||||
@@ -44,7 +63,11 @@ impl EmailTransport<StubResult> for StubEmailTransport {
|
||||
email.from(),
|
||||
email.to()
|
||||
);
|
||||
Ok(())
|
||||
if self.response.is_positive() {
|
||||
Ok(self.response.clone())
|
||||
} else {
|
||||
Err(Error::from(self.response.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
fn close(&mut self) {
|
||||
|
||||
@@ -2,10 +2,16 @@ extern crate lettre;
|
||||
|
||||
use lettre::{EmailTransport, SimpleSendableEmail};
|
||||
use lettre::stub::StubEmailTransport;
|
||||
use lettre::smtp::response::{Code, Response};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[test]
|
||||
fn stub_transport() {
|
||||
let mut sender = StubEmailTransport;
|
||||
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 email = SimpleSendableEmail::new(
|
||||
"user@localhost",
|
||||
vec!["root@localhost"],
|
||||
@@ -13,6 +19,10 @@ fn stub_transport() {
|
||||
"Hello stub",
|
||||
);
|
||||
|
||||
let result = sender.send(email);
|
||||
assert!(result.is_ok());
|
||||
let result_ok = sender_ok.send(email.clone()).unwrap();
|
||||
let result_ko = sender_ko.send(email);
|
||||
|
||||
assert_eq!(result_ok, response_ok);
|
||||
assert!(result_ko.is_err());
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user