ping.rs

 1use minidom::Element;
 2
 3use error::Error;
 4
 5use ns;
 6
 7#[derive(Debug, Clone)]
 8pub struct Ping;
 9
10pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
11    if !root.is("ping", ns::PING) {
12        return Err(Error::ParseError("This is not a ping element."));
13    }
14
15    for _ in root.children() {
16        return Err(Error::ParseError("Unknown child in ping element."));
17    }
18    Ok(Ping {  })
19}
20
21pub fn serialise_ping() -> Element {
22    Element::builder("ping").ns(ns::PING).build()
23}
24
25#[cfg(test)]
26mod tests {
27    use minidom::Element;
28    use error::Error;
29    use ping;
30
31    #[test]
32    fn test_simple() {
33        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
34        ping::parse_ping(&elem).unwrap();
35    }
36
37    #[test]
38    fn test_invalid() {
39        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
40        let error = ping::parse_ping(&elem).unwrap_err();
41        let message = match error {
42            Error::ParseError(string) => string,
43            _ => panic!(),
44        };
45        assert_eq!(message, "Unknown child in ping element.");
46    }
47
48    #[test]
49    #[ignore]
50    fn test_invalid_attribute() {
51        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
52        let error = ping::parse_ping(&elem).unwrap_err();
53        let message = match error {
54            Error::ParseError(string) => string,
55            _ => panic!(),
56        };
57        assert_eq!(message, "Unknown attribute in ping element.");
58    }
59}