xmpp-rs/src/body.rs

65 lines
1.8 KiB
Rust
Raw Normal View History

2017-04-19 23:21:23 +01:00
use minidom::Element;
use error::Error;
use ns;
2017-04-19 23:21:23 +01:00
pub type Body = String;
2017-04-19 23:21:23 +01:00
pub fn parse_body(root: &Element) -> Result<Body, Error> {
// TODO: also support components and servers.
if !root.is("body", ns::JABBER_CLIENT) {
return Err(Error::ParseError("This is not a body element."));
2017-04-19 23:21:23 +01:00
}
for _ in root.children() {
return Err(Error::ParseError("Unknown child in body element."));
}
Ok(root.text())
2017-04-19 23:21:23 +01:00
}
#[cfg(test)]
mod tests {
use minidom::Element;
use error::Error;
2017-04-20 00:43:17 +01:00
use body;
2017-04-19 23:21:23 +01:00
#[test]
fn test_simple() {
2017-04-20 00:43:17 +01:00
let elem: Element = "<body xmlns='jabber:client'/>".parse().unwrap();
body::parse_body(&elem).unwrap();
2017-04-19 23:21:23 +01:00
}
#[test]
fn test_invalid() {
2017-04-20 00:43:17 +01:00
let elem: Element = "<body xmlns='jabber:server'/>".parse().unwrap();
let error = body::parse_body(&elem).unwrap_err();
2017-04-19 23:21:23 +01:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
2017-04-20 00:43:17 +01:00
assert_eq!(message, "This is not a body element.");
2017-04-19 23:21:23 +01:00
}
#[test]
fn test_invalid_child() {
2017-04-20 00:43:17 +01:00
let elem: Element = "<body xmlns='jabber:client'><coucou/></body>".parse().unwrap();
let error = body::parse_body(&elem).unwrap_err();
2017-04-19 23:21:23 +01:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
2017-04-20 00:43:17 +01:00
assert_eq!(message, "Unknown child in body element.");
2017-04-19 23:21:23 +01:00
}
#[test]
#[ignore]
fn test_invalid_attribute() {
2017-04-20 00:43:17 +01:00
let elem: Element = "<body xmlns='jabber:client' coucou=''/>".parse().unwrap();
let error = body::parse_body(&elem).unwrap_err();
2017-04-19 23:21:23 +01:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
2017-04-20 00:43:17 +01:00
assert_eq!(message, "Unknown attribute in body element.");
2017-04-19 23:21:23 +01:00
}
}