~asimpson/mailpreview-cli

3c7fad06a86001071d7ce7d204fcb38c37d9a5b0 — Adam Simpson 1 year, 7 months ago 90634c0 master
test: add unit test for every logic branch

In doing this I realized I've account for mixed content-types so I
removed the todo comment.
3 files changed, 85 insertions(+), 3 deletions(-)

M Cargo.lock
M Cargo.toml
M src/main.rs
M Cargo.lock => Cargo.lock +1 -1
@@ 59,7 59,7 @@ dependencies = [

[[package]]
name = "mailpreview-cli"
version = "0.2.1"
version = "0.2.2"
dependencies = [
 "mailparse",
]

M Cargo.toml => Cargo.toml +1 -1
@@ 1,6 1,6 @@
[package]
name = "mailpreview-cli"
version = "0.2.1"
version = "0.2.2"
authors = ["Adam Simpson <adam@adamsimpson.net"]
edition = "2018"


M src/main.rs => src/main.rs +83 -1
@@ 22,7 22,6 @@ fn return_body(mail: ParsedMail, format: String) -> Result<String, MailParseErro
    if !mail.subparts.is_empty() {
        for m in mail.subparts.iter() {
            if m.ctype.mimetype == "multipart/related" {
                // TODO account for mixed
                for i in m.subparts.iter() {
                    if i.ctype.mimetype == "multipart/alternative" {
                        body = return_body_from_alternative(i, &format)?;


@@ 57,6 56,89 @@ mod tests {
    use super::*;

    #[test]
    fn multipart_alt() {
        let body = "Content-Type: multipart/mixed;
	boundary=MIXED

--MIXED
Content-Type: multipart/related;
	boundary=RELATED

--RELATED
Content-Type: multipart/alternative;
	boundary=ALT

--ALT
Content-Type: text/plain

plain!
--ALT
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

html!
--ALT--

--RELATED--

--MIXED--
";
        let mail = parse_mail(body.as_bytes()).unwrap();
        let body = return_body(mail, "text/plain".to_string()).unwrap();
        assert_eq!(body.trim(), "plain!");
    }

    #[test]
    fn related_format() {
        let body = "Content-Type: multipart/alternative; boundary=ALT

--ALT
Content-Type: text/plain;

plain!
--ALT
Content-Type: text/html

html!
--ALT--
";
        let mail = parse_mail(body.as_bytes()).unwrap();
        let body = return_body(mail, "text/plain".to_string()).unwrap();
        assert_eq!(body.trim(), "plain!");
    }

    #[test]
    fn alt_alt() {
        let body = "Content-Type: multipart/mixed; boundary=MIXED

--MIXED
Content-Type: multipart/alternative; boundary=ALT

--ALT
Content-Type: text/plain;

plain!
--ALT
Content-Type: text/html

html!
--ALT
Content-Type: text/calendar

calendar!
--ALT--
--MIXED
Content-Type: application/ics

ics!
--MIXED--
";
        let mail = parse_mail(body.as_bytes()).unwrap();
        let body = return_body(mail, "text/plain".to_string()).unwrap();
        assert_eq!(body.trim(), "plain!");
    }

    #[test]
    fn simple() {
        let body = "Content-Type: text/plain