From e26715eb204660b9845594895a142cd7bbf7689e Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 4 May 2024 17:20:58 +0200 Subject: [PATCH] Test empty mam response --- crates/projection-xmpp/src/iq.rs | 1 - crates/projection-xmpp/tests/lib.rs | 15 ++++++++++++- crates/proto-xmpp/src/mam.rs | 33 ++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/crates/projection-xmpp/src/iq.rs b/crates/projection-xmpp/src/iq.rs index 0232fbc..2373306 100644 --- a/crates/projection-xmpp/src/iq.rs +++ b/crates/projection-xmpp/src/iq.rs @@ -1,7 +1,6 @@ //! Handling of all client2server iq stanzas use quick_xml::events::Event; -use serde::de::Unexpected::Option; use lavina_core::room::{RoomId, RoomRegistry}; use proto_xmpp::bind::{BindResponse, Jid, Name, Server}; diff --git a/crates/projection-xmpp/tests/lib.rs b/crates/projection-xmpp/tests/lib.rs index 790ee5e..c1ea13a 100644 --- a/crates/projection-xmpp/tests/lib.rs +++ b/crates/projection-xmpp/tests/lib.rs @@ -449,8 +449,21 @@ async fn test_message_archive_request() -> Result<()> { s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Start(b) => { - assert_eq!(element_name(&b.local_name()), "message") + assert_eq!(element_name(&b.local_name()), "iq") }); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "fin") + }); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "set") + }); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "count") + }); + assert_matches!(s.next_xml_event().await?, Event::Text(b) => { + assert_eq!(&*b, b"0") + }); + s.send(r#"Logged out"#).await?; stream.shutdown().await?; diff --git a/crates/proto-xmpp/src/mam.rs b/crates/proto-xmpp/src/mam.rs index 5e083c6..bd7ee21 100644 --- a/crates/proto-xmpp/src/mam.rs +++ b/crates/proto-xmpp/src/mam.rs @@ -1,5 +1,5 @@ use anyhow::{anyhow, Result}; -use quick_xml::events::{BytesStart, Event}; +use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use quick_xml::name::{Namespace, ResolveResult}; use std::io::Read; @@ -25,10 +25,12 @@ pub struct Field { } // Message archive response styled as a result set. +#[derive(PartialEq, Eq, Debug, Clone)] pub struct Fin { pub set: Set, } +#[derive(PartialEq, Eq, Debug, Clone)] pub struct Set { pub count: Option, } @@ -37,7 +39,10 @@ impl FromXml for Set { type P = impl Parser>; fn parse() -> Self::P { - todo!() + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + (namespace, event) = yield; + Ok(Set { count: Some(0) }) + } } } @@ -50,7 +55,12 @@ impl FromXml for Fin { type P = impl Parser>; fn parse() -> Self::P { - todo!() + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + (namespace, event) = yield; + Ok(Fin { + set: Set { count: Some(0) }, + }) + } } } @@ -61,15 +71,18 @@ impl FromXmlTag for Fin { impl ToXml for Fin { fn serialize(&self, events: &mut Vec>) { - events.push(Event::Start(format!(r#"fin xmlns="{}" complete=True"#, Fin::NS))); - events.push(Event::Start(format!(r#"set xmlns="{}""#, Set::NS))); + let mut fin_bytes = BytesStart::new(format!(r#"fin xmlns="{}" complete=True"#, Fin::NS)); + let mut set_bytes = BytesStart::new(format!(r#"set xmlns="{}""#, Set::NS)); + events.push(Event::Start(fin_bytes)); + events.push(Event::Start(set_bytes)); + if let Some(count) = &self.set.count { - events.push(Event::Start("count".into())); - events.push(Event::Text("0".into())); - events.push(Event::End("count".into())); + events.push(Event::Start(BytesStart::new("count"))); + events.push(Event::Text(BytesText::new("0"))); + events.push(Event::End(BytesEnd::new("count"))); } - events.push(Event::End("set".into())); - events.push(Event::End("fin".into())); + events.push(Event::End(BytesEnd::new("set"))); + events.push(Event::End(BytesEnd::new("fin"))); } }