ping.rs

 1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
 3//
 4// This Source Code Form is subject to the terms of the Mozilla Public
 5// License, v. 2.0. If a copy of the MPL was not distributed with this
 6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 7
 8use iq::IqGetPayload;
 9
10generate_empty_element!(Ping, "ping", PING);
11
12impl IqGetPayload for Ping {}
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use try_from::TryFrom;
18    use minidom::Element;
19    use error::Error;
20
21    #[test]
22    fn test_simple() {
23        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
24        Ping::try_from(elem).unwrap();
25    }
26
27    #[test]
28    fn test_serialise() {
29        let elem1 = Element::from(Ping);
30        let elem2: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
31        assert_eq!(elem1, elem2);
32    }
33
34    #[test]
35    fn test_invalid() {
36        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
37        let error = Ping::try_from(elem).unwrap_err();
38        let message = match error {
39            Error::ParseError(string) => string,
40            _ => panic!(),
41        };
42        assert_eq!(message, "Unknown child in ping element.");
43    }
44
45    #[test]
46    fn test_invalid_attribute() {
47        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
48        let error = Ping::try_from(elem).unwrap_err();
49        let message = match error {
50            Error::ParseError(string) => string,
51            _ => panic!(),
52        };
53        assert_eq!(message, "Unknown attribute in ping element.");
54    }
55}