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 minidom::Element;
9
10use error::Error;
11
12use ns;
13
14#[derive(Debug, Clone)]
15pub struct Ping;
16
17pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
18 if !root.is("ping", ns::PING) {
19 return Err(Error::ParseError("This is not a ping element."));
20 }
21
22 for _ in root.children() {
23 return Err(Error::ParseError("Unknown child in ping element."));
24 }
25 Ok(Ping { })
26}
27
28pub fn serialise_ping() -> Element {
29 Element::builder("ping").ns(ns::PING).build()
30}
31
32#[cfg(test)]
33mod tests {
34 use minidom::Element;
35 use error::Error;
36 use ping;
37
38 #[test]
39 fn test_simple() {
40 let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
41 ping::parse_ping(&elem).unwrap();
42 }
43
44 #[test]
45 fn test_invalid() {
46 let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
47 let error = ping::parse_ping(&elem).unwrap_err();
48 let message = match error {
49 Error::ParseError(string) => string,
50 _ => panic!(),
51 };
52 assert_eq!(message, "Unknown child in ping element.");
53 }
54
55 #[test]
56 #[ignore]
57 fn test_invalid_attribute() {
58 let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
59 let error = ping::parse_ping(&elem).unwrap_err();
60 let message = match error {
61 Error::ParseError(string) => string,
62 _ => panic!(),
63 };
64 assert_eq!(message, "Unknown attribute in ping element.");
65 }
66}