forwarding.rs

 1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7#[deny(missing_docs)]
 8
 9use delay::Delay;
10use message::Message;
11
12generate_element!(
13    /// Contains a forwarded stanza, either standalone or part of another
14    /// extension (such as carbons).
15    Forwarded, "forwarded", FORWARD,
16    children: [
17        /// When the stanza originally got sent.
18        delay: Option<Delay> = ("delay", DELAY) => Delay,
19
20        // XXX: really?  Option?
21        /// The stanza being forwarded.
22        stanza: Option<Message> = ("message", DEFAULT_NS) => Message
23
24        // TODO: also handle the two other stanza possibilities.
25    ]
26);
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use try_from::TryFrom;
32    use minidom::Element;
33    use error::Error;
34
35    #[test]
36    fn test_simple() {
37        let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
38        Forwarded::try_from(elem).unwrap();
39    }
40
41    #[test]
42    fn test_invalid_child() {
43        let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><coucou/></forwarded>".parse().unwrap();
44        let error = Forwarded::try_from(elem).unwrap_err();
45        let message = match error {
46            Error::ParseError(string) => string,
47            _ => panic!(),
48        };
49        assert_eq!(message, "Unknown child in forwarded element.");
50    }
51
52    #[test]
53    fn test_serialise() {
54        let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
55        let forwarded = Forwarded { delay: None, stanza: None };
56        let elem2 = forwarded.into();
57        assert_eq!(elem, elem2);
58    }
59}