64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package parser
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseMultipartEmailWithAttachment(t *testing.T) {
|
|
raw := strings.Join([]string{
|
|
"Message-ID: <m1@example.com>",
|
|
"Subject: =?UTF-8?B?5rWL6K+V6YKu5Lu2?=",
|
|
"From: =?UTF-8?B?5byg5LiJ?= <sender@example.com>",
|
|
"To: receiver@example.com",
|
|
"Cc: cc@example.com",
|
|
"Date: Tue, 04 Jun 2024 12:00:00 +0800",
|
|
"MIME-Version: 1.0",
|
|
"Content-Type: multipart/mixed; boundary=mix",
|
|
"",
|
|
"--mix",
|
|
"Content-Type: multipart/alternative; boundary=alt",
|
|
"",
|
|
"--alt",
|
|
"Content-Type: text/plain; charset=utf-8",
|
|
"",
|
|
"plain body",
|
|
"--alt",
|
|
"Content-Type: text/html; charset=utf-8",
|
|
"",
|
|
"<p>html body</p>",
|
|
"--alt--",
|
|
"--mix",
|
|
"Content-Type: text/plain; name=\"note.txt\"",
|
|
"Content-Disposition: attachment; filename=\"note.txt\"",
|
|
"Content-Transfer-Encoding: base64",
|
|
"",
|
|
"YXR0YWNobWVudA==",
|
|
"--mix--",
|
|
"",
|
|
}, "\r\n")
|
|
|
|
msg, err := Parse([]byte(raw))
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if msg.Subject != "测试邮件" {
|
|
t.Fatalf("Subject = %q", msg.Subject)
|
|
}
|
|
if msg.SenderName != "张三" || msg.SenderEmail != "sender@example.com" {
|
|
t.Fatalf("sender = %q <%s>", msg.SenderName, msg.SenderEmail)
|
|
}
|
|
if strings.TrimSpace(msg.BodyText) != "plain body" {
|
|
t.Fatalf("BodyText = %q", msg.BodyText)
|
|
}
|
|
if !strings.Contains(msg.BodyHTML, "html body") {
|
|
t.Fatalf("BodyHTML = %q", msg.BodyHTML)
|
|
}
|
|
if len(msg.Attachments) != 1 {
|
|
t.Fatalf("attachments = %d", len(msg.Attachments))
|
|
}
|
|
if msg.Attachments[0].Filename != "note.txt" || string(msg.Attachments[0].Data) != "attachment" {
|
|
t.Fatalf("attachment = %#v", msg.Attachments[0])
|
|
}
|
|
}
|