Merge branch 'trim-whitespace' into 'master'

Fixes #11: Trim whitespace before feeding parser

Closes tokio-webhook2muc#2 and #11

See merge request xmpp-rs/tokio-xmpp!9
This commit is contained in:
Maxime Buquet 2019-09-08 14:14:52 +00:00
commit 6e72e1252c

View file

@ -229,7 +229,8 @@ impl Decoder for XMPPCodec {
};
let buf1 = buf1.as_ref().as_ref();
match from_utf8(buf1) {
Ok(s) => {
Ok(mut s) => {
s = s.trim();
if !s.is_empty() {
// println!("<< {}", s);
let mut buffer_queue = BufferQueue::new();
@ -508,4 +509,24 @@ mod tests {
&("<message><body>".to_owned() + &text + "</body></message>").as_bytes()
);
}
#[test]
fn test_lone_whitespace() {
let mut c = XMPPCodec::new();
let mut b = BytesMut::with_capacity(1024);
b.put(r"<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xmlns='jabber:client'>");
let r = c.decode(&mut b);
assert!(match r {
Ok(Some(Packet::StreamStart(_))) => true,
_ => false,
});
b.clear();
b.put(r" ");
let r = c.decode(&mut b);
assert!(match r {
Ok(None) => true,
_ => false,
});
}
}