Commit
+76 -1 +/-1 browse
1 | diff --git a/maitred/src/transport.rs b/maitred/src/transport.rs |
2 | index c5da76a..15e2430 100644 |
3 | --- a/maitred/src/transport.rs |
4 | +++ b/maitred/src/transport.rs |
5 | @@ -109,7 +109,7 @@ impl Decoder for Transport { |
6 | type Error = TransportError; |
7 | |
8 | fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
9 | - tracing::trace!("{}", String::from_utf8_lossy(src)); |
10 | + tracing::trace!("Decoding:\n{}", String::from_utf8_lossy(src)); |
11 | |
12 | if src.is_empty() { |
13 | tracing::debug!("Empty command received"); |
14 | @@ -194,3 +194,78 @@ impl Decoder for Transport { |
15 | } |
16 | } |
17 | } |
18 | + |
19 | + #[cfg(test)] |
20 | + mod test { |
21 | + |
22 | + use super::*; |
23 | + |
24 | + use bytes::BytesMut; |
25 | + |
26 | + #[test] |
27 | + fn sequential_commands() { |
28 | + let mut transport = Transport::default(); |
29 | + match transport.decode(&mut BytesMut::from("HELO example.org\r\n")) { |
30 | + Ok(Some(command)) => assert!(matches!(command, Command::Requests(_))), |
31 | + _ => panic!() |
32 | + }; |
33 | + match transport.decode(&mut BytesMut::from("DATA\r\n")) { |
34 | + Ok(Some(command)) => assert!(matches!(command, Command::Requests(_))), |
35 | + _ => panic!() |
36 | + }; |
37 | + match transport.decode(&mut BytesMut::from("Subject: Hello World\r\n")) { |
38 | + Ok(None) => {}, |
39 | + _ => panic!() |
40 | + }; |
41 | + match transport.decode(&mut BytesMut::from("AAAAAAABBBBBBCCCCCCC")) { |
42 | + Ok(None) => {}, |
43 | + _ => panic!() |
44 | + }; |
45 | + match transport.decode(&mut BytesMut::from("DDDDDDDEEEEEEEFFFFFF")) { |
46 | + Ok(None) => {}, |
47 | + _ => panic!() |
48 | + }; |
49 | + match transport.decode(&mut BytesMut::from("\r\n.\r\n")) { |
50 | + Ok(Some(Command::Payload(_))) => {}, |
51 | + _ => panic!() |
52 | + }; |
53 | + match transport.decode(&mut BytesMut::from("QUIT\r\n")) { |
54 | + Ok(Some(Command::Requests(_))) => {}, |
55 | + _ => panic!() |
56 | + }; |
57 | + } |
58 | + |
59 | + #[test] |
60 | + fn sequential_commands_pipeline() { |
61 | + let mut transport = Transport::default().pipelining(true); |
62 | + match transport.decode(&mut BytesMut::from("HELO example.org\r\n")) { |
63 | + Ok(Some(command)) => assert!(matches!(command, Command::Requests(_))), |
64 | + _ => panic!() |
65 | + }; |
66 | + match transport.decode(&mut BytesMut::from("DATA\r\n")) { |
67 | + Ok(Some(command)) => assert!(matches!(command, Command::Requests(_))), |
68 | + _ => panic!() |
69 | + }; |
70 | + match transport.decode(&mut BytesMut::from("Subject: Hello World\r\n")) { |
71 | + Ok(None) => {}, |
72 | + _ => panic!() |
73 | + }; |
74 | + match transport.decode(&mut BytesMut::from("AAAAAAABBBBBBCCCCCCC")) { |
75 | + Ok(None) => {}, |
76 | + _ => panic!() |
77 | + }; |
78 | + match transport.decode(&mut BytesMut::from("DDDDDDDEEEEEEEFFFFFF")) { |
79 | + Ok(None) => {}, |
80 | + _ => panic!() |
81 | + }; |
82 | + match transport.decode(&mut BytesMut::from("\r\n.\r\n")) { |
83 | + Ok(Some(Command::Payload(_))) => {}, |
84 | + _ => panic!() |
85 | + }; |
86 | + match transport.decode(&mut BytesMut::from("QUIT\r\n")) { |
87 | + Ok(Some(Command::Requests(_))) => {}, |
88 | + _ => panic!() |
89 | + }; |
90 | + } |
91 | + |
92 | + } |