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!(
11    /// Represents a ping to the recipient, which must be answered with an
12    /// empty `<iq/>` or with an error.
13    Ping, "ping", PING
14);
15
16impl IqGetPayload for Ping {}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21    use try_from::TryFrom;
22    use minidom::Element;
23    use error::Error;
24
25    #[test]
26    fn test_simple() {
27        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
28        Ping::try_from(elem).unwrap();
29    }
30
31    #[test]
32    fn test_serialise() {
33        let elem1 = Element::from(Ping);
34        let elem2: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
35        assert_eq!(elem1, elem2);
36    }
37
38    #[test]
39    fn test_invalid() {
40        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
41        let error = Ping::try_from(elem).unwrap_err();
42        let message = match error {
43            Error::ParseError(string) => string,
44            _ => panic!(),
45        };
46        assert_eq!(message, "Unknown child in ping element.");
47    }
48
49    #[test]
50    fn test_invalid_attribute() {
51        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
52        let error = Ping::try_from(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}