From 2f1af34b2d77a542d4c7962765f0c0a96ead90aa Mon Sep 17 00:00:00 2001 From: Miquel Ruiz Date: Mon, 11 Jul 2016 22:44:46 +0100 Subject: [PATCH 1/3] Support for UID COPY & tests --- src/client.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 915ab5d..2a1e8ce 100644 --- a/src/client.rs +++ b/src/client.rs @@ -208,8 +208,12 @@ impl Client { self.run_command_and_check_ok(&format!("COPY {} {}", sequence_set, mailbox_name).to_string()) } + pub fn uid_copy(&mut self, uid_set: &str, mailbox_name: &str) -> Result<()> { + self.run_command_and_check_ok(&format!("UID COPY {} {}", uid_set, mailbox_name)) + } + /// The LIST command returns a subset of names from the complete set - /// of all names available to the client. + /// of all names available to the client. pub fn list(&mut self, reference_name: &str, mailbox_search_pattern: &str) -> Result> { self.run_command_and_parse(&format!("LIST {} {}", reference_name, mailbox_search_pattern)) } @@ -570,4 +574,30 @@ mod tests { } assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid store command"); } + + #[test] + fn copy() { + generic_copy(false) + } + + #[test] + fn uid_copy() { + generic_copy(true) + } + + fn generic_copy(uid: bool) { + let response = b"a1 OK COPY completed\r\n".to_vec(); + let sequence_set = "2:4"; + let mailbox = "MEETING"; + let uid_cmd = if uid { " UID " } else { " " }; + let command = format!("a1{}COPY {} {}\r\n", uid_cmd, sequence_set, mailbox); + let mock_stream = MockStream::new(response); + let mut client = Client::new(mock_stream); + if uid { + client.uid_copy(sequence_set, mailbox).unwrap(); + } else { + client.copy(sequence_set, mailbox).unwrap(); + } + assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid copy command"); + } } From eb0b4d9b50e1dc78c72a9dc93f5decd10f62e053 Mon Sep 17 00:00:00 2001 From: Miquel Ruiz Date: Mon, 11 Jul 2016 22:48:03 +0100 Subject: [PATCH 2/3] Support for UID FETCH & tests --- src/client.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index 2a1e8ce..b547104 100644 --- a/src/client.rs +++ b/src/client.rs @@ -132,6 +132,10 @@ impl Client { self.run_command_and_read_response(&format!("FETCH {} {}", sequence_set, query).to_string()) } + pub fn uid_fetch(&mut self, uid_set: &str, query: &str) -> Result> { + self.run_command_and_read_response(&format!("UID FETCH {} {}", uid_set, query).to_string()) + } + /// Noop always succeeds, and it does nothing. pub fn noop(&mut self) -> Result<()> { self.run_command_and_check_ok("NOOP") @@ -388,13 +392,27 @@ mod tests { #[test] fn fetch() { + generic_fetch(false) + } + + #[test] + fn uid_fetch() { + generic_fetch(true) + } + + fn generic_fetch(uid: bool) { let response = b"a1 OK FETCH completed\r\n".to_vec(); let sequence_set = "1"; let query = "BODY[]"; - let command = format!("a1 FETCH {} {}\r\n", sequence_set, query); + let uid_cmd = if uid { " UID " } else { " " }; + let command = format!("a1{}FETCH {} {}\r\n", uid_cmd, sequence_set, query); let mock_stream = MockStream::new(response); let mut client = Client::new(mock_stream); - client.fetch(sequence_set, query).unwrap(); + if uid { + client.uid_fetch(sequence_set, query).unwrap(); + } else { + client.fetch(sequence_set, query).unwrap(); + } assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid fetch command"); } From 6e872bb3409465b0a69b829e2e276bd9a6eb8f12 Mon Sep 17 00:00:00 2001 From: Miquel Ruiz Date: Tue, 12 Jul 2016 10:37:09 +0100 Subject: [PATCH 3/3] Refactor tests to better support UID commands --- src/client.rs | 127 +++++++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/src/client.rs b/src/client.rs index b547104..1d98c94 100644 --- a/src/client.rs +++ b/src/client.rs @@ -390,32 +390,6 @@ mod tests { assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid rename command"); } - #[test] - fn fetch() { - generic_fetch(false) - } - - #[test] - fn uid_fetch() { - generic_fetch(true) - } - - fn generic_fetch(uid: bool) { - let response = b"a1 OK FETCH completed\r\n".to_vec(); - let sequence_set = "1"; - let query = "BODY[]"; - let uid_cmd = if uid { " UID " } else { " " }; - let command = format!("a1{}FETCH {} {}\r\n", uid_cmd, sequence_set, query); - let mock_stream = MockStream::new(response); - let mut client = Client::new(mock_stream); - if uid { - client.uid_fetch(sequence_set, query).unwrap(); - } else { - client.fetch(sequence_set, query).unwrap(); - } - assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid fetch command"); - } - #[test] fn subscribe() { let response = b"a1 OK SUBSCRIBE completed\r\n".to_vec(); @@ -566,56 +540,91 @@ mod tests { #[test] fn store() { - generic_store(false); + generic_store(" ", |mut c, set, query| c.store(set, query)); } #[test] fn uid_store() { - generic_store(true); + generic_store(" UID ", |mut c, set, query| c.uid_store(set, query)); } - fn generic_store(uid: bool) { - let response = b"* 2 FETCH (FLAGS (\\Deleted \\Seen))\r\n\ + fn generic_store(prefix: &str, op: F) + where F: FnOnce(&mut Client, &str, &str) -> Result { + + let res = "* 2 FETCH (FLAGS (\\Deleted \\Seen))\r\n\ * 3 FETCH (FLAGS (\\Deleted))\r\n\ * 4 FETCH (FLAGS (\\Deleted \\Flagged \\Seen))\r\n\ - a1 OK STORE completed\r\n".to_vec(); - let sequence_set = "2:4"; - let query = "+FLAGS (\\Deleted)"; - let uid_cmd = if uid { " UID " } else { " " }; - let command = format!("a1{}STORE {} {}\r\n", uid_cmd, sequence_set, query); - let mock_stream = MockStream::new(response); - let mut client = Client::new(mock_stream); - if uid { - client.uid_store(sequence_set, query).unwrap(); - } else { - client.store(sequence_set, query).unwrap(); - } - assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid store command"); + a1 OK STORE completed\r\n"; + + generic_with_uid( + res, + "STORE", + "2.4", + "+FLAGS (\\Deleted)", + prefix, + op, + ); } #[test] fn copy() { - generic_copy(false) + generic_copy(" ", |mut c, set, query| c.copy(set, query)) } #[test] fn uid_copy() { - generic_copy(true) + generic_copy(" UID ", |mut c, set, query| c.uid_copy(set, query)) } - fn generic_copy(uid: bool) { - let response = b"a1 OK COPY completed\r\n".to_vec(); - let sequence_set = "2:4"; - let mailbox = "MEETING"; - let uid_cmd = if uid { " UID " } else { " " }; - let command = format!("a1{}COPY {} {}\r\n", uid_cmd, sequence_set, mailbox); - let mock_stream = MockStream::new(response); - let mut client = Client::new(mock_stream); - if uid { - client.uid_copy(sequence_set, mailbox).unwrap(); - } else { - client.copy(sequence_set, mailbox).unwrap(); - } - assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid copy command"); + fn generic_copy(prefix: &str, op: F) + where F: FnOnce(&mut Client, &str, &str) -> Result { + + generic_with_uid( + "OK COPY completed\r\n", + "COPY", + "2:4", + "MEETING", + prefix, + op, + ); + } + + #[test] + fn fetch() { + generic_fetch(" ", |mut c, seq, query| c.fetch(seq, query)) + } + + #[test] + fn uid_fetch() { + generic_fetch(" UID ", |mut c, seq, query| c.uid_fetch(seq, query)) + } + + fn generic_fetch(prefix: &str, op: F) + where F: FnOnce(&mut Client, &str, &str) -> Result { + + generic_with_uid( + "OK FETCH completed\r\n", + "FETCH", + "1", + "BODY[]", + prefix, + op + ); + } + + fn generic_with_uid( + res: &str, + cmd: &str, + seq: &str, + query: &str, + prefix: &str, + op: F) where F: FnOnce(&mut Client, &str, &str) -> Result, + { + + let resp = format!("a1 {}\r\n", res).as_bytes().to_vec(); + let line = format!("a1{}{} {} {}\r\n", prefix, cmd, seq, query); + let mut client = Client::new(MockStream::new(resp)); + let _ = op(&mut client, seq, query); + assert!(client.stream.written_buf == line.as_bytes().to_vec(), "Invalid command"); } }