mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 22:00:49 +01:00
Compare commits
No commits in common. "3a3eaed348c5fc2f42c5d424a8192704d5226fdf" and "8b6a7927efaabdb05102696b8925864a4d9e4f9e" have entirely different histories.
3a3eaed348
...
8b6a7927ef
2 changed files with 453 additions and 128 deletions
11
msg.go
11
msg.go
|
@ -753,16 +753,7 @@ func (m *Msg) ToIgnoreInvalid(rcpts ...string) {
|
|||
// References:
|
||||
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3
|
||||
func (m *Msg) ToFromString(rcpts string) error {
|
||||
src := strings.Split(rcpts, ",")
|
||||
var dst []string
|
||||
for _, address := range src {
|
||||
address = strings.TrimSpace(address)
|
||||
if address == "" {
|
||||
continue
|
||||
}
|
||||
dst = append(dst, address)
|
||||
}
|
||||
return m.To(dst...)
|
||||
return m.To(strings.Split(rcpts, ",")...)
|
||||
}
|
||||
|
||||
// Cc sets one or more "CC" (carbon copy) addresses in the mail body for the Msg.
|
||||
|
|
570
msg_test.go
570
msg_test.go
|
@ -5,6 +5,7 @@
|
|||
package mail
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
@ -120,10 +121,8 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
/*
|
||||
//go:embed README.md
|
||||
var efs embed.FS
|
||||
*/
|
||||
|
||||
func TestNewMsg(t *testing.T) {
|
||||
t.Run("create new message", func(t *testing.T) {
|
||||
|
@ -627,7 +626,26 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
if err := message.SetAddrHeader(tt.header, "toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 1",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -641,7 +659,26 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
if err := message.SetAddrHeader(tt.header, "<toni.tester@example.com>"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 1",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -656,8 +693,26 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"toni.tester@example.com")); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1,
|
||||
"toni.tester@example.com", "Toni Tester")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 1",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != `"Toni Tester" <toni.tester@example.com>` {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), `"Toni Tester" <toni.tester@example.com>`)
|
||||
}
|
||||
if addresses[0].Name != "Toni Tester" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be %s, "+
|
||||
"got: %s", tt.header, "Toni Tester", addresses[0].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -677,8 +732,38 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 1, 2, "tina.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 2",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
if addresses[1].Address != "tina.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[1].Address, "tina.tester@example.com")
|
||||
}
|
||||
if addresses[1].String() != "<tina.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[1].String(), "<tina.tester@example.com>")
|
||||
}
|
||||
if addresses[1].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[1].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -691,7 +776,26 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", HeaderFrom)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for From is: %d, want: 1",
|
||||
len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", HeaderFrom,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", HeaderFrom,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", HeaderFrom, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
t.Run("SetAddrHeader with addrHeader map is nil", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -703,7 +807,26 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", HeaderFrom)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for From is: %d, want: 1",
|
||||
len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", HeaderFrom,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", HeaderFrom,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", HeaderFrom, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
t.Run("SetAddrHeader with invalid address", func(t *testing.T) {
|
||||
for _, tt := range addrHeaderTests {
|
||||
|
@ -729,8 +852,26 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 1,
|
||||
"toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 1",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -742,8 +883,26 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "<toni.tester@example.com>")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 1,
|
||||
"toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 1",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -761,10 +920,38 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com",
|
||||
"tina.tester@example.com")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 2,
|
||||
"toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 1, 2,
|
||||
"tina.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 2",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
if addresses[1].Address != "tina.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[1].Address, "tina.tester@example.com")
|
||||
}
|
||||
if addresses[1].String() != "<tina.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[1].String(), "<tina.tester@example.com>")
|
||||
}
|
||||
if addresses[1].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[1].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -781,11 +968,39 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com",
|
||||
"invalid", "tina.tester@example.com")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 2,
|
||||
"toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 1, 2,
|
||||
"tina.tester@example.com", "")
|
||||
"invalid", "valid@address.tld")
|
||||
addresses, ok := message.addrHeader[tt.header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header)
|
||||
}
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for %s is %d, want: 2",
|
||||
tt.header, len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[0].Name)
|
||||
}
|
||||
if addresses[1].Address != "valid@address.tld" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[1].Address, "valid@address.tld")
|
||||
}
|
||||
if addresses[1].String() != "<valid@address.tld>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", tt.header,
|
||||
addresses[1].String(), "<valid@address.tld>")
|
||||
}
|
||||
if addresses[1].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", tt.header, addresses[1].Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -796,7 +1011,26 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
}
|
||||
message.addrHeader = nil
|
||||
message.SetAddrHeaderIgnoreInvalid(HeaderFrom, "toni.tester@example.com", "tina.tester@example.com")
|
||||
checkAddrHeader(t, message, HeaderFrom, "SetAddrHeaderIgnoreInvalid", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set address header, addrHeader field for %s is not set", HeaderFrom)
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set address header, addrHeader value count for From is: %d, want: 1",
|
||||
len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", HeaderFrom,
|
||||
addresses[0].Address, "toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set address header, addrHeader value for %s is %s, want: %s", HeaderFrom,
|
||||
addresses[0].String(), "<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set address header, addrHeader name for %s expected to be emtpy, "+
|
||||
"got: %s", HeaderFrom, addresses[0].Name)
|
||||
}
|
||||
})
|
||||
t.Run("SetAddrHeaderIgnoreInvalid with invalid addresses only", func(t *testing.T) {
|
||||
for _, tt := range addrHeaderTests {
|
||||
|
@ -828,7 +1062,24 @@ func TestMsg_EnvelopeFrom(t *testing.T) {
|
|||
if err := message.EnvelopeFrom("toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set envelope from: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderEnvelopeFrom, "EnvelopeFrom", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderEnvelopeFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set envelope from, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set envelope from, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set envelope from, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set envelope from, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set envelope from, addrHeader name is %s, want: empty", addresses[0].Name)
|
||||
}
|
||||
})
|
||||
t.Run("EnvelopeFrom with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -859,7 +1110,25 @@ func TestMsg_EnvelopeFromFormat(t *testing.T) {
|
|||
if err := message.EnvelopeFromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set envelope From: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderEnvelopeFrom, "FromFormat", 0, 1, "toni.tester@example.com", "Toni Tester")
|
||||
addresses, ok := message.addrHeader[HeaderEnvelopeFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set envelope From, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set envelope From, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set envelope From, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != `"Toni Tester" <toni.tester@example.com>` {
|
||||
t.Errorf("failed to set envelope From, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "Toni Tester" {
|
||||
t.Errorf("failed to set envelope From, addrHeader name is %s, want: %s", addresses[0].Name,
|
||||
"Toni Tester")
|
||||
}
|
||||
})
|
||||
t.Run("EnvelopeFromFormat with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -890,7 +1159,24 @@ func TestMsg_From(t *testing.T) {
|
|||
if err := message.From("toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set From: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "From", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set From, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set From, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set From, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set From, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set From, addrHeader name is %s, want: empty", addresses[0].Name)
|
||||
}
|
||||
})
|
||||
t.Run("From with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -938,7 +1224,25 @@ func TestMsg_FromFormat(t *testing.T) {
|
|||
if err := message.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set From: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "FromFormat", 0, 1, "toni.tester@example.com", "Toni Tester")
|
||||
addresses, ok := message.addrHeader[HeaderFrom]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set From, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set From, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set From, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != `"Toni Tester" <toni.tester@example.com>` {
|
||||
t.Errorf("failed to set From, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "Toni Tester" {
|
||||
t.Errorf("failed to set From, addrHeader name is %s, want: %s", addresses[0].Name,
|
||||
"Toni Tester")
|
||||
}
|
||||
})
|
||||
t.Run("FromFormat with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -969,7 +1273,24 @@ func TestMsg_To(t *testing.T) {
|
|||
if err := message.To("toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set To: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "To", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set To, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set To, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set To, addrHeader name is %s, want: empty", addresses[0].Name)
|
||||
}
|
||||
})
|
||||
t.Run("To with multiple valid addresses", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -979,8 +1300,35 @@ func TestMsg_To(t *testing.T) {
|
|||
if err := message.To("toni.tester@example.com", "tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set To: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "To", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "To", 1, 2, "tina.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set To, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("failed to set To, addrHeader value count is: %d, want: 2", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set To, addrHeader name is %s, want: empty", addresses[0].Name)
|
||||
}
|
||||
if addresses[1].Address != "tina.tester@example.com" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[1].Address,
|
||||
"tina.tester@example.com")
|
||||
}
|
||||
if addresses[1].String() != "<tina.tester@example.com>" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[1].String(),
|
||||
"<tina.tester@example.com>")
|
||||
}
|
||||
if addresses[1].Name != "" {
|
||||
t.Errorf("failed to set To, addrHeader name is %s, want: empty", addresses[1].Name)
|
||||
}
|
||||
})
|
||||
t.Run("To with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1031,8 +1379,35 @@ func TestMsg_AddTo(t *testing.T) {
|
|||
if err := message.AddTo("tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set additional To: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddTo", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "AddTo", 1, 2, "tina.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set To, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("failed to set additional To, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set To, addrHeader name is %s, want: empty", addresses[0].Name)
|
||||
}
|
||||
if addresses[1].Address != "tina.tester@example.com" {
|
||||
t.Errorf("failed to set additional To, addrHeader value is %s, want: %s", addresses[1].Address,
|
||||
"tina.tester@example.com")
|
||||
}
|
||||
if addresses[1].String() != "<tina.tester@example.com>" {
|
||||
t.Errorf("failed to set additional To, addrHeader value is %s, want: %s", addresses[1].String(),
|
||||
"<tina.tester@example.com>")
|
||||
}
|
||||
if addresses[1].Name != "" {
|
||||
t.Errorf("failed to set additional To, addrHeader name is %s, want: empty", addresses[1].Name)
|
||||
}
|
||||
})
|
||||
t.Run("AddTo with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1045,7 +1420,13 @@ func TestMsg_AddTo(t *testing.T) {
|
|||
if err := message.AddTo("invalid"); err == nil {
|
||||
t.Errorf("AddTo should fail with invalid address")
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddTo", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set To, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set To, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1061,8 +1442,36 @@ func TestMsg_AddToFormat(t *testing.T) {
|
|||
if err := message.AddToFormat("Tina Tester", "tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set additional To: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddToFormat", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "AddToFormat", 1, 2, "tina.tester@example.com", "Tina Tester")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set To, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 2 {
|
||||
t.Fatalf("failed to set additional To, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
if addresses[0].Address != "toni.tester@example.com" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].Address,
|
||||
"toni.tester@example.com")
|
||||
}
|
||||
if addresses[0].String() != "<toni.tester@example.com>" {
|
||||
t.Errorf("failed to set To, addrHeader value is %s, want: %s", addresses[0].String(),
|
||||
"<toni.tester@example.com>")
|
||||
}
|
||||
if addresses[0].Name != "" {
|
||||
t.Errorf("failed to set To, addrHeader name is %s, want: empty", addresses[0].Name)
|
||||
}
|
||||
if addresses[1].Address != "tina.tester@example.com" {
|
||||
t.Errorf("failed to set additional To, addrHeader value is %s, want: %s", addresses[1].Address,
|
||||
"tina.tester@example.com")
|
||||
}
|
||||
if addresses[1].String() != `"Tina Tester" <tina.tester@example.com>` {
|
||||
t.Errorf("failed to set additional To, addrHeader value is %s, want: %s", addresses[1].String(),
|
||||
"<tina.tester@example.com>")
|
||||
}
|
||||
if addresses[1].Name != "Tina Tester" {
|
||||
t.Errorf("failed to set additional To, addrHeader name is %s, want: %s", addresses[1].Name,
|
||||
"Tina Tester")
|
||||
}
|
||||
})
|
||||
t.Run("AddToFormat with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1075,93 +1484,18 @@ func TestMsg_AddToFormat(t *testing.T) {
|
|||
if err := message.AddToFormat("Invalid", "invalid"); err == nil {
|
||||
t.Errorf("AddToFormat should fail with invalid address")
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddToFormat", 0, 1, "toni.tester@example.com", "")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set To, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 1 {
|
||||
t.Fatalf("failed to set To, addrHeader value count is: %d, want: 1", len(addresses))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMsg_ToIgnoreInvalid(t *testing.T) {
|
||||
t.Run("ToIgnoreInvalid with valid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
message.ToIgnoreInvalid("toni.tester@example.com")
|
||||
checkAddrHeader(t, message, HeaderTo, "ToIgnoreInvalid", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("ToIgnoreInvalid with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
message.ToIgnoreInvalid("invalid")
|
||||
addresses, ok := message.addrHeader[HeaderTo]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set ToIgnoreInvalid, addrHeader field is not set")
|
||||
}
|
||||
if len(addresses) != 0 {
|
||||
t.Fatalf("failed to set ToIgnoreInvalid, addrHeader value count is: %d, want: 0", len(addresses))
|
||||
}
|
||||
})
|
||||
t.Run("ToIgnoreInvalid with valid and invalid addresses", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
message.ToIgnoreInvalid("toni.tester@example.com", "invalid", "tina.tester@example.com")
|
||||
checkAddrHeader(t, message, HeaderTo, "ToIgnoreInvalid", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "ToIgnoreInvalid", 1, 2, "tina.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
|
||||
func TestMsg_ToFromString(t *testing.T) {
|
||||
t.Run("ToFromString with valid addresses", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
if err := message.ToFromString(`toni.tester@example.com,<tina.tester@example.com>`); err != nil {
|
||||
t.Fatalf("failed to set ToFromString: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "ToFromString", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "ToFromString", 1, 2, "tina.tester@example.com", "")
|
||||
})
|
||||
t.Run("ToFromString with valid addresses and empty fields", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
if err := message.ToFromString(`toni.tester@example.com ,,<tina.tester@example.com>`); err != nil {
|
||||
t.Fatalf("failed to set ToFromString: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "ToFromString", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "ToFromString", 1, 2, "tina.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
|
||||
// checkAddrHeader validates a single email address in the AddrHeader of a message.
|
||||
func checkAddrHeader(t *testing.T, message *Msg, header AddrHeader, fn string, field, wantFields int,
|
||||
wantMail, wantName string) {
|
||||
t.Helper()
|
||||
addresses, ok := message.addrHeader[header]
|
||||
if !ok {
|
||||
t.Fatalf("failed to set %s, addrHeader field is not set", fn)
|
||||
}
|
||||
if len(addresses) != wantFields {
|
||||
t.Fatalf("failed to set %s, addrHeader value count is: %d, want: %d", fn, len(addresses), field)
|
||||
}
|
||||
if addresses[field].Address != wantMail {
|
||||
t.Errorf("failed to set %s, addrHeader value is %s, want: %s", fn, addresses[field].Address, wantMail)
|
||||
}
|
||||
wantString := fmt.Sprintf(`<%s>`, wantMail)
|
||||
if wantName != "" {
|
||||
wantString = fmt.Sprintf(`%q <%s>`, wantName, wantMail)
|
||||
}
|
||||
if addresses[field].String() != wantString {
|
||||
t.Errorf("failed to set %s, addrHeader value is %s, want: %s", fn, addresses[field].String(), wantString)
|
||||
}
|
||||
if addresses[field].Name != wantName {
|
||||
t.Errorf("failed to set %s, addrHeader name is %s, want: %s", fn, addresses[field].Name, wantName)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue