68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
package smtpserver
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEnsureRequiredHeadersFixedOrder(t *testing.T) {
|
|
raw := "Subject: hi\r\nX-Custom: drop-me\r\n\r\nbody text"
|
|
out := ensureRequiredHeaders(raw, "msg123@host", []string{"rcpt@example.com"}, "from@example.com", nil)
|
|
|
|
headerBlock, body := splitHeadersBody(out)
|
|
var names []string
|
|
for _, line := range strings.Split(headerBlock, "\r\n") {
|
|
if line == "" {
|
|
continue
|
|
}
|
|
names = append(names, strings.SplitN(line, ":", 2)[0])
|
|
}
|
|
want := []string{"Message-ID", "Date", "MIME-Version", "To", "From", "Subject", "Content-Type", "Content-Transfer-Encoding"}
|
|
if len(names) != len(want) {
|
|
t.Fatalf("header names = %v, want %v", names, want)
|
|
}
|
|
for i := range want {
|
|
if names[i] != want[i] {
|
|
t.Errorf("header[%d] = %q, want %q", i, names[i], want[i])
|
|
}
|
|
}
|
|
if strings.Contains(headerBlock, "X-Custom") {
|
|
t.Error("unwhitelisted header X-Custom should have been dropped, not carried through")
|
|
}
|
|
if !strings.Contains(headerBlock, "To: rcpt@example.com") {
|
|
t.Error("missing To header should be synthesized from envelope recipients")
|
|
}
|
|
if body != "body text" {
|
|
t.Errorf("body = %q, want %q", body, "body text")
|
|
}
|
|
}
|
|
|
|
func TestExtractMessageIDDoesNotCrashOnMalformedHeader(t *testing.T) {
|
|
// No "@" in the Message-ID value — the fixed bug: Python's original crashes
|
|
// here (UnboundLocalError); the Go port must fall back to a generated ID.
|
|
raw := "Message-ID: not-an-id\r\nSubject: x\r\n\r\nbody"
|
|
id := extractMessageID(raw, "mail.example.com")
|
|
if id == "" {
|
|
t.Fatal("expected a generated fallback Message-ID, got empty string")
|
|
}
|
|
if !strings.HasSuffix(id, "@mail.example.com") {
|
|
t.Errorf("fallback Message-ID = %q, want suffix @mail.example.com", id)
|
|
}
|
|
}
|
|
|
|
func TestExtractMessageIDRehostsOnHostnameMismatch(t *testing.T) {
|
|
raw := "Message-ID: <abc123@other-host.com>\r\nSubject: x\r\n\r\nbody"
|
|
id := extractMessageID(raw, "mail.example.com")
|
|
if id != "abc123@mail.example.com" {
|
|
t.Errorf("id = %q, want rehosted to mail.example.com", id)
|
|
}
|
|
}
|
|
|
|
func TestExtractMessageIDKeepsMatchingHostname(t *testing.T) {
|
|
raw := "Message-ID: <abc123@mail.example.com>\r\nSubject: x\r\n\r\nbody"
|
|
id := extractMessageID(raw, "mail.example.com")
|
|
if id != "abc123@mail.example.com" {
|
|
t.Errorf("id = %q, want unchanged", id)
|
|
}
|
|
}
|