From 215c9d513693cee83ce48c8b4a2f3654fb049c3a Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sun, 16 Jul 2017 22:16:01 +0200 Subject: [PATCH] feat(transport): Allow specifying a response for stub transport --- lettre/src/stub/error.rs | 37 ---------------------------------- lettre/src/stub/mod.rs | 37 +++++++++++++++++++++++++++------- lettre/tests/transport_stub.rs | 16 ++++++++++++--- 3 files changed, 43 insertions(+), 47 deletions(-) delete mode 100644 lettre/src/stub/error.rs diff --git a/lettre/src/stub/error.rs b/lettre/src/stub/error.rs deleted file mode 100644 index 277f1e0..0000000 --- a/lettre/src/stub/error.rs +++ /dev/null @@ -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) - } -} diff --git a/lettre/src/stub/mod.rs b/lettre/src/stub/mod.rs index 97ee062..d91521b 100644 --- a/lettre/src/stub/mod.rs +++ b/lettre/src/stub/mod.rs @@ -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 for StubEmailTransport { fn send(&mut self, email: T) -> StubResult { @@ -44,7 +63,11 @@ impl EmailTransport 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) { diff --git a/lettre/tests/transport_stub.rs b/lettre/tests/transport_stub.rs index 9751ccb..396fdc9 100644 --- a/lettre/tests/transport_stub.rs +++ b/lettre/tests/transport_stub.rs @@ -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()); + }