From 98322139678d8834354b5402ec01ac65bab603c9 Mon Sep 17 00:00:00 2001 From: mrhid6 Date: Mon, 14 Sep 2026 09:37:01 +0000 Subject: [PATCH] test: let the fake SMTP server hand back the DATA it received --- mail/sender_test.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mail/sender_test.go b/mail/sender_test.go index 8c179be..1d5e4c7 100644 --- a/mail/sender_test.go +++ b/mail/sender_test.go @@ -21,6 +21,15 @@ import ( // line(s) to write back. Anything not listed gets a plain 250 OK. func fakeSMTP(t *testing.T, script map[string]string) string { t.Helper() + port, _ := fakeSMTPData(t, script) + return port +} + +// fakeSMTPData is fakeSMTP that also hands back the raw DATA the client sent, +// once the body's terminating dot arrives. +func fakeSMTPData(t *testing.T, script map[string]string) (string, <-chan []byte) { + t.Helper() + got := make(chan []byte, 1) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) @@ -41,15 +50,21 @@ func fakeSMTP(t *testing.T, script map[string]string) string { } writeLine("220 fake.example ESMTP") inData := false + var data bytes.Buffer for { line, err := r.ReadString('\n') if err != nil { return } + raw := line line = strings.TrimRight(line, "\r\n") if inData { + if line != "." { + data.WriteString(raw) + } if line == "." { inData = false + got <- data.Bytes() if reply, ok := script["DATA_BODY"]; ok { if reply == "CLOSE" { return @@ -102,7 +117,7 @@ func fakeSMTP(t *testing.T, script map[string]string) string { host, port, _ := net.SplitHostPort(ln.Addr().String()) _ = host - return port + return port, got } func testMsg() message {