xmpp-rs-mirror/src/body.rs

69 lines
1.9 KiB
Rust
Raw Normal View History

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