Add tests on response

This commit is contained in:
Alexis Mousset
2015-06-28 20:46:05 +02:00
parent 3195959de8
commit e855e37182
2 changed files with 34 additions and 8 deletions

View File

@@ -17,9 +17,9 @@ use uuid::Uuid;
use sendable_email::SendableEmail; use sendable_email::SendableEmail;
/// Converts an adress or an address with an alias to an `Address` /// Converts an adress or an address with an alias to a `Address`
pub trait ToHeader { pub trait ToHeader {
/// Converts to an `Header` struct /// Converts to a `Header` struct
fn to_header(&self) -> Header; fn to_header(&self) -> Header;
} }
@@ -36,9 +36,9 @@ impl<'a> ToHeader for (&'a str, &'a str) {
} }
} }
/// Converts an adress or an address with an alias to an `Mailbox` /// Converts an adress or an address with an alias to a `Mailbox`
pub trait ToMailbox { pub trait ToMailbox {
/// Converts to an `Mailbox` struct /// Converts to a `Mailbox` struct
fn to_mailbox(&self) -> Mailbox; fn to_mailbox(&self) -> Mailbox;
} }

View File

@@ -7,7 +7,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
//! SMTP response, containing a mandatory return code, and an optional text message //! SMTP response, containing a mandatory return code and an optional text message
use std::str::FromStr; use std::str::FromStr;
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter, Result};
@@ -163,7 +163,7 @@ impl Response {
format!("{}{}{}", self.severity, self.category, self.detail) format!("{}{}{}", self.severity, self.category, self.detail)
} }
/// Checls code equality /// Tests code equality
pub fn has_code(&self, code: u16) -> bool { pub fn has_code(&self, code: u16) -> bool {
self.code() == format!("{}", code) self.code() == format!("{}", code)
} }
@@ -172,9 +172,11 @@ impl Response {
pub fn first_word(&self) -> Option<String> { pub fn first_word(&self) -> Option<String> {
match self.message.is_empty() { match self.message.is_empty() {
true => None, true => None,
false => Some(self.message[0].split(" ").next().unwrap().to_string()), false => match self.message[0].split_whitespace().next() {
Some(word) => Some(word.to_string()),
None => None,
}
} }
} }
} }
@@ -273,6 +275,12 @@ mod test {
1, 1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()] vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).severity(), Severity::PositiveCompletion); ).severity(), Severity::PositiveCompletion);
assert_eq!(Response::new(
"5".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).severity(), Severity::PermanentNegativeCompletion);
} }
#[test] #[test]
@@ -341,5 +349,23 @@ mod test {
1, 1,
vec![] vec![]
).first_word(), None); ).first_word(), None);
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec![" ".to_string()]
).first_word(), None);
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec![" ".to_string()]
).first_word(), None);
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["".to_string()]
).first_word(), None);
} }
} }