ping.rs

 1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2// Copyright (c) 2017 Maxime “pep” Buquet <pep@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 crate::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,
14    "ping",
15    PING
16);
17
18impl IqGetPayload for Ping {}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    #[cfg(not(feature = "disable-validation"))]
24    use crate::util::error::Error;
25    use crate::Element;
26    use std::convert::TryFrom;
27
28    #[test]
29    fn test_size() {
30        assert_size!(Ping, 0);
31    }
32
33    #[test]
34    fn test_simple() {
35        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
36        Ping::try_from(elem).unwrap();
37    }
38
39    #[test]
40    fn test_serialise() {
41        let elem1 = Element::from(Ping);
42        let elem2: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
43        assert_eq!(elem1, elem2);
44    }
45
46    #[cfg(not(feature = "disable-validation"))]
47    #[test]
48    fn test_invalid() {
49        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>"
50            .parse()
51            .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 child in ping element.");
58    }
59
60    #[cfg(not(feature = "disable-validation"))]
61    #[test]
62    fn test_invalid_attribute() {
63        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
64        let error = Ping::try_from(elem).unwrap_err();
65        let message = match error {
66            Error::ParseError(string) => string,
67            _ => panic!(),
68        };
69        assert_eq!(message, "Unknown attribute in ping element.");
70    }
71}