ultisuite-backend/internal/mail/imap/parse_test.go
R3D347HR4Y cd0a80f5e8 huhu
2026-05-25 13:52:27 +02:00

76 lines
2.0 KiB
Go

package imap
import (
"strings"
"testing"
)
func TestParseBody_multipartAlternativeBase64(t *testing.T) {
raw := buildMultipartMessage(t, "alternative", []mimePart{
{
contentType: "text/plain; charset=utf-8",
body: []byte("SAMSUNG\nRéserver aux professionnels"),
transferEnc: "base64",
},
{
contentType: "text/html; charset=utf-8",
body: []byte("<p>SAMSUNG</p><p>Réserver aux professionnels</p>"),
transferEnc: "base64",
},
})
text, html := parseBody(raw)
if !strings.Contains(text, "SAMSUNG") {
t.Fatalf("text = %q, want decoded plain text", text)
}
if !strings.Contains(text, "professionnels") {
t.Fatalf("text = %q, want utf-8 decoded content", text)
}
if !strings.Contains(html, "<p>SAMSUNG</p>") {
t.Fatalf("html = %q, want decoded html", html)
}
if looksLikeRawMIME(text) || looksLikeRawMIME(html) {
t.Fatal("parseBody returned raw MIME")
}
}
func TestParseBody_headerlessMultipartBase64(t *testing.T) {
withHeaders := buildMultipartMessage(t, "alternative", []mimePart{
{
contentType: "text/plain; charset=utf-8",
body: []byte("Hello MIME"),
transferEnc: "base64",
},
})
// Drop RFC822 headers — simulates IMAP body fetch without outer Content-Type.
idx := strings.Index(string(withHeaders), "\r\n\r\n")
if idx < 0 {
t.Fatal("missing header/body separator")
}
raw := withHeaders[idx+4:]
text, _ := parseBody(raw)
if text != "Hello MIME" {
t.Fatalf("text = %q, want Hello MIME", text)
}
}
func TestParseBody_singlePartBase64(t *testing.T) {
var b strings.Builder
b.WriteString("From: a@b.com\r\n")
b.WriteString("To: c@d.com\r\n")
b.WriteString("Subject: test\r\n")
b.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
b.WriteString("Content-Transfer-Encoding: base64\r\n")
b.WriteString("\r\n")
b.WriteString("SGVsbG8gYmFzZTY0") // "Hello base64"
text, html := parseBody([]byte(b.String()))
if text != "Hello base64" {
t.Fatalf("text = %q, want Hello base64", text)
}
if html != "" {
t.Fatalf("html = %q, want empty", html)
}
}