test: let the fake SMTP server hand back the DATA it received
This commit is contained in:
+16
-1
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user