mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
Compare commits
4 commits
8b6a7927ef
...
3a3eaed348
Author | SHA1 | Date | |
---|---|---|---|
3a3eaed348 | |||
e08d36d0b8 | |||
c99b6c3f14 | |||
03da20fc39 |
2 changed files with 128 additions and 453 deletions
11
msg.go
11
msg.go
|
@ -753,7 +753,16 @@ 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 {
|
||||
return m.To(strings.Split(rcpts, ",")...)
|
||||
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...)
|
||||
}
|
||||
|
||||
// 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,7 +5,6 @@
|
|||
package mail
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
@ -121,8 +120,10 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
/*
|
||||
//go:embed README.md
|
||||
var efs embed.FS
|
||||
*/
|
||||
|
||||
func TestNewMsg(t *testing.T) {
|
||||
t.Run("create new message", func(t *testing.T) {
|
||||
|
@ -626,26 +627,7 @@ 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -659,26 +641,7 @@ 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -693,26 +656,8 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"toni.tester@example.com")); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1,
|
||||
"toni.tester@example.com", "Toni Tester")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -732,38 +677,8 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeader", 1, 2, "tina.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -776,26 +691,7 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("SetAddrHeader with addrHeader map is nil", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -807,26 +703,7 @@ func TestMsg_SetAddrHeader(t *testing.T) {
|
|||
"tina.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set address header, err: %s", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("SetAddrHeader with invalid address", func(t *testing.T) {
|
||||
for _, tt := range addrHeaderTests {
|
||||
|
@ -852,26 +729,8 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 1,
|
||||
"toni.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -883,26 +742,8 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "<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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 1,
|
||||
"toni.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -920,38 +761,10 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com",
|
||||
"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)
|
||||
}
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 2,
|
||||
"toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 1, 2,
|
||||
"tina.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -968,39 +781,11 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.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)
|
||||
}
|
||||
"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", "")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -1011,26 +796,7 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
|
|||
}
|
||||
message.addrHeader = nil
|
||||
message.SetAddrHeaderIgnoreInvalid(HeaderFrom, "toni.tester@example.com", "tina.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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "SetAddrHeaderIgnoreInvalid", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("SetAddrHeaderIgnoreInvalid with invalid addresses only", func(t *testing.T) {
|
||||
for _, tt := range addrHeaderTests {
|
||||
|
@ -1062,24 +828,7 @@ 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderEnvelopeFrom, "EnvelopeFrom", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("EnvelopeFrom with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1110,25 +859,7 @@ 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)
|
||||
}
|
||||
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")
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderEnvelopeFrom, "FromFormat", 0, 1, "toni.tester@example.com", "Toni Tester")
|
||||
})
|
||||
t.Run("EnvelopeFromFormat with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1159,24 +890,7 @@ func TestMsg_From(t *testing.T) {
|
|||
if err := message.From("toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set From: %s", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "From", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("From with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1224,25 +938,7 @@ 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)
|
||||
}
|
||||
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")
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderFrom, "FromFormat", 0, 1, "toni.tester@example.com", "Toni Tester")
|
||||
})
|
||||
t.Run("FromFormat with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1273,24 +969,7 @@ func TestMsg_To(t *testing.T) {
|
|||
if err := message.To("toni.tester@example.com"); err != nil {
|
||||
t.Fatalf("failed to set To: %s", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "To", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
t.Run("To with multiple valid addresses", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1300,35 +979,8 @@ 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "To", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "To", 1, 2, "tina.tester@example.com", "")
|
||||
})
|
||||
t.Run("To with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1379,35 +1031,8 @@ 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddTo", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "AddTo", 1, 2, "tina.tester@example.com", "")
|
||||
})
|
||||
t.Run("AddTo with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1420,13 +1045,7 @@ func TestMsg_AddTo(t *testing.T) {
|
|||
if err := message.AddTo("invalid"); err == nil {
|
||||
t.Errorf("AddTo should fail with invalid address")
|
||||
}
|
||||
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))
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddTo", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1442,36 +1061,8 @@ 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)
|
||||
}
|
||||
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")
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddToFormat", 0, 2, "toni.tester@example.com", "")
|
||||
checkAddrHeader(t, message, HeaderTo, "AddToFormat", 1, 2, "tina.tester@example.com", "Tina Tester")
|
||||
})
|
||||
t.Run("AddToFormat with invalid address", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -1484,18 +1075,93 @@ func TestMsg_AddToFormat(t *testing.T) {
|
|||
if err := message.AddToFormat("Invalid", "invalid"); err == nil {
|
||||
t.Errorf("AddToFormat should fail with invalid address")
|
||||
}
|
||||
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))
|
||||
}
|
||||
checkAddrHeader(t, message, HeaderTo, "AddToFormat", 0, 1, "toni.tester@example.com", "")
|
||||
})
|
||||
}
|
||||
|
||||
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