package mailview import ( "strings" "testing" ) func TestParseSimpleTextMessage(t *testing.T) { raw := "From: a@example.com\r\nTo: b@example.com\r\nSubject: hi\r\n\r\nhello there" m, err := Parse([]byte(raw)) if err != nil { t.Fatal(err) } if m.Header.From != "a@example.com" || m.Header.Subject != "hi" { t.Errorf("headers = %+v", m.Header) } if m.TextBody != "hello there" { t.Errorf("TextBody = %q", m.TextBody) } if m.HTMLBody != "" || len(m.Attachments) != 0 { t.Errorf("expected no HTML body or attachments, got HTMLBody=%q attachments=%d", m.HTMLBody, len(m.Attachments)) } } func TestParseMultipartAlternativeKeepsBothBodies(t *testing.T) { raw := "" + "From: a@example.com\r\nTo: b@example.com\r\nSubject: hi\r\n" + "Content-Type: multipart/alternative; boundary=\"B\"\r\n\r\n" + "--B\r\nContent-Type: text/plain\r\n\r\nplain version\r\n" + "--B\r\nContent-Type: text/html\r\n\r\n
html version
\r\n" + "--B--\r\n" m, err := Parse([]byte(raw)) if err != nil { t.Fatal(err) } if strings.TrimSpace(m.TextBody) != "plain version" { t.Errorf("TextBody = %q", m.TextBody) } if strings.TrimSpace(m.HTMLBody) != "html version
" { t.Errorf("HTMLBody = %q", m.HTMLBody) } } func TestParseAttachmentDecodesBase64(t *testing.T) { raw := "" + "From: a@example.com\r\nTo: b@example.com\r\nSubject: hi\r\n" + "Content-Type: multipart/mixed; boundary=\"B\"\r\n\r\n" + "--B\r\nContent-Type: text/plain\r\n\r\nsee attached\r\n" + "--B\r\nContent-Type: application/octet-stream\r\nContent-Disposition: attachment; filename=\"a.txt\"\r\n" + "Content-Transfer-Encoding: BASE64\r\n\r\nSGVsbG8sIHdvcmxkIQ==\r\n" + "--B--\r\n" m, err := Parse([]byte(raw)) if err != nil { t.Fatal(err) } if len(m.Attachments) != 1 { t.Fatalf("got %d attachments, want 1", len(m.Attachments)) } if got := string(m.Attachments[0].Data); got != "Hello, world!" { t.Errorf("attachment data = %q, want decoded base64", got) } if m.Attachments[0].Filename != "a.txt" { t.Errorf("filename = %q", m.Attachments[0].Filename) } } func TestParseNestedMultipartMixedWithAlternativeBody(t *testing.T) { raw := "" + "From: a@example.com\r\nTo: b@example.com\r\nSubject: hi\r\n" + "Content-Type: multipart/mixed; boundary=\"OUTER\"\r\n\r\n" + "--OUTER\r\nContent-Type: multipart/alternative; boundary=\"INNER\"\r\n\r\n" + "--INNER\r\nContent-Type: text/plain\r\n\r\nplain body\r\n" + "--INNER\r\nContent-Type: text/html\r\n\r\nhtml body
\r\n" + "--INNER--\r\n" + "--OUTER\r\nContent-Type: text/plain\r\nContent-Disposition: attachment; filename=\"notes.txt\"\r\n\r\n" + "attached notes\r\n" + "--OUTER--\r\n" m, err := Parse([]byte(raw)) if err != nil { t.Fatal(err) } if strings.TrimSpace(m.TextBody) != "plain body" { t.Errorf("TextBody = %q", m.TextBody) } if strings.TrimSpace(m.HTMLBody) != "html body
" { t.Errorf("HTMLBody = %q", m.HTMLBody) } if len(m.Attachments) != 1 || m.Attachments[0].Filename != "notes.txt" { t.Fatalf("attachments = %+v", m.Attachments) } }