mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
Compare commits
21 commits
eebbaa2513
...
8b6a7927ef
Author | SHA1 | Date | |
---|---|---|---|
8b6a7927ef | |||
1ea7b173c6 | |||
a7f81baa4b | |||
cb85a136c3 | |||
aa46b408ad | |||
5d85be068d | |||
1caa2cfb92 | |||
c8dbc9a735 | |||
08fe44c051 | |||
7d352bc58e | |||
9505f94e3d | |||
143e3b5b4f | |||
a2e9dbae11 | |||
69c5f43cbf | |||
425a190eb1 | |||
64aeb683ba | |||
1dba76948f | |||
120c2efd08 | |||
c4946af3ab | |||
64cfbf9e46 | |||
c58d52e49a |
5 changed files with 4569 additions and 3293 deletions
109
client_test.go
109
client_test.go
|
@ -1448,20 +1448,32 @@ func TestClient_SetSMTPAuthCustom(t *testing.T) {
|
|||
want string
|
||||
}{
|
||||
{"CRAM-MD5", smtp.CRAMMD5Auth("", ""), "*smtp.cramMD5Auth"},
|
||||
{"LOGIN", smtp.LoginAuth("", "", "", false),
|
||||
"*smtp.loginAuth"},
|
||||
{"LOGIN-NOENC", smtp.LoginAuth("", "", "", true),
|
||||
"*smtp.loginAuth"},
|
||||
{"PLAIN", smtp.PlainAuth("", "", "", "", false),
|
||||
"*smtp.plainAuth"},
|
||||
{"PLAIN-NOENC", smtp.PlainAuth("", "", "", "", true),
|
||||
"*smtp.plainAuth"},
|
||||
{
|
||||
"LOGIN", smtp.LoginAuth("", "", "", false),
|
||||
"*smtp.loginAuth",
|
||||
},
|
||||
{
|
||||
"LOGIN-NOENC", smtp.LoginAuth("", "", "", true),
|
||||
"*smtp.loginAuth",
|
||||
},
|
||||
{
|
||||
"PLAIN", smtp.PlainAuth("", "", "", "", false),
|
||||
"*smtp.plainAuth",
|
||||
},
|
||||
{
|
||||
"PLAIN-NOENC", smtp.PlainAuth("", "", "", "", true),
|
||||
"*smtp.plainAuth",
|
||||
},
|
||||
{"SCRAM-SHA-1", smtp.ScramSHA1Auth("", ""), "*smtp.scramAuth"},
|
||||
{"SCRAM-SHA-1-PLUS", smtp.ScramSHA1PlusAuth("", "", nil),
|
||||
"*smtp.scramAuth"},
|
||||
{
|
||||
"SCRAM-SHA-1-PLUS", smtp.ScramSHA1PlusAuth("", "", nil),
|
||||
"*smtp.scramAuth",
|
||||
},
|
||||
{"SCRAM-SHA-256", smtp.ScramSHA256Auth("", ""), "*smtp.scramAuth"},
|
||||
{"SCRAM-SHA-256-PLUS", smtp.ScramSHA256PlusAuth("", "", nil),
|
||||
"*smtp.scramAuth"},
|
||||
{
|
||||
"SCRAM-SHA-256-PLUS", smtp.ScramSHA256PlusAuth("", "", nil),
|
||||
"*smtp.scramAuth",
|
||||
},
|
||||
{"XOAUTH2", smtp.XOAuth2Auth("", ""), "*smtp.xoauth2Auth"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -1483,7 +1495,6 @@ func TestClient_SetSMTPAuthCustom(t *testing.T) {
|
|||
t.Errorf("failed to set custom SMTP auth, expected auth method type: %s, got: %s",
|
||||
tt.want, authType)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -1785,7 +1796,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("connect should fail on HELO", func(t *testing.T) {
|
||||
ctxFail, cancelFail := context.WithCancel(context.Background())
|
||||
ctxFail, cancelFail := context.WithCancel(ctx)
|
||||
defer cancelFail()
|
||||
PortAdder.Add(1)
|
||||
failServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -1820,7 +1831,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("connect with failing auth", func(t *testing.T) {
|
||||
ctxAuth, cancelAuth := context.WithCancel(context.Background())
|
||||
ctxAuth, cancelAuth := context.WithCancel(ctx)
|
||||
defer cancelAuth()
|
||||
PortAdder.Add(1)
|
||||
authServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -1850,7 +1861,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("connect with STARTTLS", func(t *testing.T) {
|
||||
ctxTLS, cancelTLS := context.WithCancel(context.Background())
|
||||
ctxTLS, cancelTLS := context.WithCancel(ctx)
|
||||
defer cancelTLS()
|
||||
PortAdder.Add(1)
|
||||
tlsServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -1880,7 +1891,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("connect with STARTTLS Opportunisticly", func(t *testing.T) {
|
||||
ctxTLS, cancelTLS := context.WithCancel(context.Background())
|
||||
ctxTLS, cancelTLS := context.WithCancel(ctx)
|
||||
defer cancelTLS()
|
||||
PortAdder.Add(1)
|
||||
tlsServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -1910,7 +1921,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("connect with STARTTLS but fail", func(t *testing.T) {
|
||||
ctxTLS, cancelTLS := context.WithCancel(context.Background())
|
||||
ctxTLS, cancelTLS := context.WithCancel(ctx)
|
||||
defer cancelTLS()
|
||||
PortAdder.Add(1)
|
||||
tlsServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -1941,7 +1952,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("want STARTTLS, but server does not support it", func(t *testing.T) {
|
||||
ctxTLS, cancelTLS := context.WithCancel(context.Background())
|
||||
ctxTLS, cancelTLS := context.WithCancel(ctx)
|
||||
defer cancelTLS()
|
||||
PortAdder.Add(1)
|
||||
tlsServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -1971,7 +1982,7 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("connect with SSL", func(t *testing.T) {
|
||||
ctxSSL, cancelSSL := context.WithCancel(context.Background())
|
||||
ctxSSL, cancelSSL := context.WithCancel(ctx)
|
||||
defer cancelSSL()
|
||||
PortAdder.Add(1)
|
||||
sslServerPort := int(TestServerPortBase + PortAdder.Load())
|
||||
|
@ -2305,7 +2316,6 @@ func TestClient_auth(t *testing.T) {
|
|||
if err := client.Close(); err != nil {
|
||||
t.Errorf("failed to close client connection: %s", err)
|
||||
}
|
||||
|
||||
})
|
||||
t.Run(tt.name+" should fail", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -2484,6 +2494,9 @@ func TestClient_Send(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2498,6 +2511,9 @@ func TestClient_Send(t *testing.T) {
|
|||
})
|
||||
t.Run("send with no connection should fail", func(t *testing.T) {
|
||||
client, err := NewClient(DefaultHost)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.Send(message); err == nil {
|
||||
t.Errorf("client should have failed to send email with no connection")
|
||||
}
|
||||
|
@ -2530,6 +2546,9 @@ func TestClient_Send(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2584,6 +2603,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2620,6 +2642,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2658,6 +2683,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2701,6 +2729,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2744,6 +2775,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2787,6 +2821,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS), WithDSN())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2823,6 +2860,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2867,6 +2907,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2911,6 +2954,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2954,6 +3000,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -2997,6 +3046,9 @@ func TestClient_sendSingleMsg(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -3040,6 +3092,9 @@ func TestClient_checkConn(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -3074,6 +3129,9 @@ func TestClient_checkConn(t *testing.T) {
|
|||
t.Cleanup(cancelDial)
|
||||
|
||||
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.DialWithContext(ctxDial); err != nil {
|
||||
t.Fatalf("failed to connect to test server: %s", err)
|
||||
}
|
||||
|
@ -3091,6 +3149,9 @@ func TestClient_checkConn(t *testing.T) {
|
|||
})
|
||||
t.Run("connection should fail on no connection", func(t *testing.T) {
|
||||
client, err := NewClient(DefaultHost)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new client: %s", err)
|
||||
}
|
||||
if err = client.checkConn(); err == nil {
|
||||
t.Errorf("client should have failed on connection check")
|
||||
}
|
||||
|
@ -3419,11 +3480,14 @@ func simpleSMTPServer(ctx context.Context, t *testing.T, props *serverProps) err
|
|||
if props.SSLListener {
|
||||
keypair, err := tls.X509KeyPair(localhostCert, localhostKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read TLS keypair: %s", err)
|
||||
return fmt.Errorf("failed to read TLS keypair: %w", err)
|
||||
}
|
||||
tlsConfig := &tls.Config{Certificates: []tls.Certificate{keypair}}
|
||||
listener, err = tls.Listen(TestServerProto, fmt.Sprintf("%s:%d", TestServerAddr, props.ListenPort),
|
||||
tlsConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create TLS listener: %s", err)
|
||||
}
|
||||
} else {
|
||||
listener, err = net.Listen(TestServerProto, fmt.Sprintf("%s:%d", TestServerAddr, props.ListenPort))
|
||||
}
|
||||
|
@ -3504,7 +3568,6 @@ func handleTestServerConnection(connection net.Conn, t *testing.T, props *server
|
|||
break
|
||||
}
|
||||
writeLine("250-localhost.localdomain\r\n" + props.FeatureSet)
|
||||
break
|
||||
case strings.HasPrefix(data, "MAIL FROM:"):
|
||||
if props.FailOnMailFrom {
|
||||
writeLine("500 5.5.2 Error: fail on MAIL FROM")
|
||||
|
|
|
@ -901,7 +901,8 @@ func TestEMLToMsgFromReader(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
"RFC5322 A1.1 example mail", exampleMailRFC5322A11, EncodingUSASCII,
|
||||
"Saying Hello"},
|
||||
"Saying Hello",
|
||||
},
|
||||
{
|
||||
"Plain text no encoding (7bit)", exampleMailPlain7Bit, EncodingUSASCII,
|
||||
"Example mail // plain text without encoding",
|
||||
|
@ -1158,7 +1159,6 @@ func TestEMLToMsgFromFile(t *testing.T) {
|
|||
t.Errorf("failed to parse EML string: want subject %s, got %s", "Saying Hello",
|
||||
gotSubject[0])
|
||||
}
|
||||
|
||||
})
|
||||
t.Run("EMLToMsgFromFile fails on file not found", func(t *testing.T) {
|
||||
if _, err := EMLToMsgFromFile("testdata/not-existing.eml"); err == nil {
|
||||
|
|
149
header_test.go
149
header_test.go
|
@ -8,69 +8,13 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
// TestImportance_StringFuncs tests the different string method of the Importance object
|
||||
func TestImportance_StringFuncs(t *testing.T) {
|
||||
tests := []struct {
|
||||
var (
|
||||
genHeaderTests = []struct {
|
||||
name string
|
||||
imp Importance
|
||||
wantns string
|
||||
xprio string
|
||||
header Header
|
||||
want string
|
||||
}{
|
||||
{"Importance: Non-Urgent", ImportanceNonUrgent, "0", "5", "non-urgent"},
|
||||
{"Importance: Low", ImportanceLow, "0", "5", "low"},
|
||||
{"Importance: Normal", ImportanceNormal, "", "", ""},
|
||||
{"Importance: High", ImportanceHigh, "1", "1", "high"},
|
||||
{"Importance: Urgent", ImportanceUrgent, "1", "1", "urgent"},
|
||||
{"Importance: Unknown", 9, "", "", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.imp.NumString() != tt.wantns {
|
||||
t.Errorf("wrong number string for Importance returned. Expected: %s, got: %s",
|
||||
tt.wantns, tt.imp.NumString())
|
||||
}
|
||||
if tt.imp.XPrioString() != tt.xprio {
|
||||
t.Errorf("wrong x-prio string for Importance returned. Expected: %s, got: %s",
|
||||
tt.xprio, tt.imp.XPrioString())
|
||||
}
|
||||
if tt.imp.String() != tt.want {
|
||||
t.Errorf("wrong string for Importance returned. Expected: %s, got: %s",
|
||||
tt.want, tt.imp.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAddrHeader_String tests the string method of the AddrHeader object
|
||||
func TestAddrHeader_String(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ah AddrHeader
|
||||
want string
|
||||
}{
|
||||
{"Address header: From", HeaderFrom, "From"},
|
||||
{"Address header: To", HeaderTo, "To"},
|
||||
{"Address header: Cc", HeaderCc, "Cc"},
|
||||
{"Address header: Bcc", HeaderBcc, "Bcc"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.ah.String() != tt.want {
|
||||
t.Errorf("wrong string for AddrHeader returned. Expected: %s, got: %s",
|
||||
tt.want, tt.ah.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestHeader_String tests the string method of the Header object
|
||||
func TestHeader_String(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
h Header
|
||||
want string
|
||||
}{
|
||||
{"Header: Content-Description", HeaderContentDescription, "Content-Description"},
|
||||
{"Header: Content-Disposition", HeaderContentDisposition, "Content-Disposition"},
|
||||
{"Header: Content-ID", HeaderContentID, "Content-ID"},
|
||||
{"Header: Content-Language", HeaderContentLang, "Content-Language"},
|
||||
|
@ -78,6 +22,10 @@ func TestHeader_String(t *testing.T) {
|
|||
{"Header: Content-Transfer-Encoding", HeaderContentTransferEnc, "Content-Transfer-Encoding"},
|
||||
{"Header: Content-Type", HeaderContentType, "Content-Type"},
|
||||
{"Header: Date", HeaderDate, "Date"},
|
||||
{
|
||||
"Header: Disposition-Notification-To", HeaderDispositionNotificationTo,
|
||||
"Disposition-Notification-To",
|
||||
},
|
||||
{"Header: Importance", HeaderImportance, "Importance"},
|
||||
{"Header: In-Reply-To", HeaderInReplyTo, "In-Reply-To"},
|
||||
{"Header: List-Unsubscribe", HeaderListUnsubscribe, "List-Unsubscribe"},
|
||||
|
@ -87,19 +35,90 @@ func TestHeader_String(t *testing.T) {
|
|||
{"Header: Organization", HeaderOrganization, "Organization"},
|
||||
{"Header: Precedence", HeaderPrecedence, "Precedence"},
|
||||
{"Header: Priority", HeaderPriority, "Priority"},
|
||||
{"Header: HeaderReferences", HeaderReferences, "References"},
|
||||
{"Header: References", HeaderReferences, "References"},
|
||||
{"Header: Reply-To", HeaderReplyTo, "Reply-To"},
|
||||
{"Header: Subject", HeaderSubject, "Subject"},
|
||||
{"Header: User-Agent", HeaderUserAgent, "User-Agent"},
|
||||
{"Header: X-Auto-Response-Suppress", HeaderXAutoResponseSuppress, "X-Auto-Response-Suppress"},
|
||||
{"Header: X-Mailer", HeaderXMailer, "X-Mailer"},
|
||||
{"Header: X-MSMail-Priority", HeaderXMSMailPriority, "X-MSMail-Priority"},
|
||||
{"Header: X-Priority", HeaderXPriority, "X-Priority"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
addrHeaderTests = []struct {
|
||||
name string
|
||||
header AddrHeader
|
||||
want string
|
||||
}{
|
||||
{"From", HeaderFrom, "From"},
|
||||
{"To", HeaderTo, "To"},
|
||||
{"Cc", HeaderCc, "Cc"},
|
||||
{"Bcc", HeaderBcc, "Bcc"},
|
||||
}
|
||||
)
|
||||
|
||||
func TestImportance_Stringer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
imp Importance
|
||||
wantnum string
|
||||
xprio string
|
||||
want string
|
||||
}{
|
||||
{"Non-Urgent", ImportanceNonUrgent, "0", "5", "non-urgent"},
|
||||
{"Low", ImportanceLow, "0", "5", "low"},
|
||||
{"Normal", ImportanceNormal, "", "", ""},
|
||||
{"High", ImportanceHigh, "1", "1", "high"},
|
||||
{"Urgent", ImportanceUrgent, "1", "1", "urgent"},
|
||||
{"Unknown", 9, "", "", ""},
|
||||
}
|
||||
t.Run("String", func(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.imp.String() != tt.want {
|
||||
t.Errorf("wrong string for Importance returned. Expected: %s, got: %s", tt.want, tt.imp.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("NumString", func(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.imp.NumString() != tt.wantnum {
|
||||
t.Errorf("wrong number string for Importance returned. Expected: %s, got: %s", tt.wantnum,
|
||||
tt.imp.NumString())
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("XPrioString", func(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.imp.XPrioString() != tt.xprio {
|
||||
t.Errorf("wrong x-prio string for Importance returned. Expected: %s, got: %s", tt.xprio,
|
||||
tt.imp.XPrioString())
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddrHeader_Stringer(t *testing.T) {
|
||||
for _, tt := range addrHeaderTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.h.String() != tt.want {
|
||||
t.Errorf("wrong string for Header returned. Expected: %s, got: %s",
|
||||
tt.want, tt.h.String())
|
||||
if tt.header.String() != tt.want {
|
||||
t.Errorf("wrong string for AddrHeader returned. Expected: %s, got: %s",
|
||||
tt.want, tt.header.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeader_Stringer(t *testing.T) {
|
||||
for _, tt := range genHeaderTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.header.String() != tt.want {
|
||||
t.Errorf("wrong string for Header returned. Expected: %s, got: %s",
|
||||
tt.want, tt.header.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
12
msg.go
12
msg.go
|
@ -583,6 +583,9 @@ func (m *Msg) SetAddrHeader(header AddrHeader, values ...string) error {
|
|||
// References:
|
||||
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.4
|
||||
func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) {
|
||||
if m.addrHeader == nil {
|
||||
m.addrHeader = make(map[AddrHeader][]*mail.Address)
|
||||
}
|
||||
var addresses []*mail.Address
|
||||
for _, addrVal := range values {
|
||||
address, err := mail.ParseAddress(m.encodeString(addrVal))
|
||||
|
@ -591,7 +594,14 @@ func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) {
|
|||
}
|
||||
addresses = append(addresses, address)
|
||||
}
|
||||
m.addrHeader[header] = addresses
|
||||
switch header {
|
||||
case HeaderFrom:
|
||||
if len(addresses) > 0 {
|
||||
m.addrHeader[header] = []*mail.Address{addresses[0]}
|
||||
}
|
||||
default:
|
||||
m.addrHeader[header] = addresses
|
||||
}
|
||||
}
|
||||
|
||||
// EnvelopeFrom sets the envelope from address for the Msg.
|
||||
|
|
7588
msg_test.go
7588
msg_test.go
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue