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 xso::{AsXml, FromXml};
9
10use crate::iq::IqGetPayload;
11use crate::ns;
12
13/// Represents a ping to the recipient, which must be answered with an
14/// empty `<iq/>` or with an error.
15#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
16#[xml(namespace = ns::PING, name = "ping")]
17pub struct Ping;
18
19impl IqGetPayload for Ping {}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24 use minidom::Element;
25 #[cfg(not(feature = "disable-validation"))]
26 use xso::error::{Error, FromElementError};
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 FromElementError::Invalid(Error::Other(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 FromElementError::Invalid(Error::Other(string)) => string,
67 _ => panic!(),
68 };
69 assert_eq!(message, "Unknown attribute in Ping element.");
70 }
71}