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 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    use crate::error::Error;
24    use minidom::Element;
25    use try_from::TryFrom;
26
27    #[test]
28    fn test_size() {
29        assert_size!(Ping, 0);
30    }
31
32    #[test]
33    fn test_simple() {
34        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
35        Ping::try_from(elem).unwrap();
36    }
37
38    #[test]
39    fn test_serialise() {
40        let elem1 = Element::from(Ping);
41        let elem2: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
42        assert_eq!(elem1, elem2);
43    }
44
45    #[test]
46    fn test_invalid() {
47        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>"
48            .parse()
49            .unwrap();
50        let error = Ping::try_from(elem).unwrap_err();
51        let message = match error {
52            Error::ParseError(string) => string,
53            _ => panic!(),
54        };
55        assert_eq!(message, "Unknown child in ping element.");
56    }
57
58    #[test]
59    fn test_invalid_attribute() {
60        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
61        let error = Ping::try_from(elem).unwrap_err();
62        let message = match error {
63            Error::ParseError(string) => string,
64            _ => panic!(),
65        };
66        assert_eq!(message, "Unknown attribute in ping element.");
67    }
68}