Compare commits

...

4 commits

Author SHA1 Message Date
3a3eaed348
Refactor address header tests to use checkAddrHeader
Consolidate repeated header validation logic into the `checkAddrHeader` helper function. This refactoring improves code readability and maintainability by reducing redundancy and potential for errors.
2024-10-26 02:07:44 +02:00
e08d36d0b8
Refactor address header validation in tests
Replace repeated address header validation code with a helper function `checkAddrHeader` to reduce redundancy and improve readability. Also, add new test cases for `ToFromString` to handle valid addresses with and without empty fields.
2024-10-25 20:21:17 +02:00
c99b6c3f14
Fix ToFromString to handle and trim empty addresses
Previously, the ToFromString function split email addresses by commas but did not handle empty addresses or trim whitespace. Now, it trims each address and ignores any empty entries to ensure only valid addresses are processed. This prevents potential errors stemming from malformed input.
2024-10-25 19:54:41 +02:00
03da20fc39
Add unit tests for Msg_ToIgnoreInvalid behavior
Introduces new test cases to verify the Msg_ToIgnoreInvalid function handles various scenarios correctly. These tests check for behavior with valid addresses, invalid addresses, and a mix of both.
2024-10-25 19:43:05 +02:00
2 changed files with 128 additions and 453 deletions

11
msg.go
View file

@ -753,7 +753,16 @@ func (m *Msg) ToIgnoreInvalid(rcpts ...string) {
// References: // References:
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3 // - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3
func (m *Msg) ToFromString(rcpts string) error { 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. // Cc sets one or more "CC" (carbon copy) addresses in the mail body for the Msg.

View file

@ -5,7 +5,6 @@
package mail package mail
import ( import (
"embed"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
@ -121,8 +120,10 @@ var (
} }
) )
/*
//go:embed README.md //go:embed README.md
var efs embed.FS var efs embed.FS
*/
func TestNewMsg(t *testing.T) { func TestNewMsg(t *testing.T) {
t.Run("create new message", func(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 { if err := message.SetAddrHeader(tt.header, "toni.tester@example.com"); err != nil {
t.Fatalf("failed to set address header, err: %s", err) t.Fatalf("failed to set address header, err: %s", err)
} }
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -659,26 +641,7 @@ func TestMsg_SetAddrHeader(t *testing.T) {
if err := message.SetAddrHeader(tt.header, "<toni.tester@example.com>"); err != nil { if err := message.SetAddrHeader(tt.header, "<toni.tester@example.com>"); err != nil {
t.Fatalf("failed to set address header, err: %s", err) t.Fatalf("failed to set address header, err: %s", err)
} }
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -693,26 +656,8 @@ func TestMsg_SetAddrHeader(t *testing.T) {
"toni.tester@example.com")); err != nil { "toni.tester@example.com")); err != nil {
t.Fatalf("failed to set address header, err: %s", err) t.Fatalf("failed to set address header, err: %s", err)
} }
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 1,
if !ok { "toni.tester@example.com", "Toni Tester")
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)
}
}) })
} }
}) })
@ -732,38 +677,8 @@ func TestMsg_SetAddrHeader(t *testing.T) {
"tina.tester@example.com"); err != nil { "tina.tester@example.com"); err != nil {
t.Fatalf("failed to set address header, err: %s", err) t.Fatalf("failed to set address header, err: %s", err)
} }
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeader", 0, 2, "toni.tester@example.com", "")
if !ok { checkAddrHeader(t, message, tt.header, "SetAddrHeader", 1, 2, "tina.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -776,26 +691,7 @@ func TestMsg_SetAddrHeader(t *testing.T) {
"tina.tester@example.com"); err != nil { "tina.tester@example.com"); err != nil {
t.Fatalf("failed to set address header, err: %s", err) t.Fatalf("failed to set address header, err: %s", err)
} }
addresses, ok := message.addrHeader[HeaderFrom] checkAddrHeader(t, message, HeaderFrom, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
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) { t.Run("SetAddrHeader with addrHeader map is nil", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -807,26 +703,7 @@ func TestMsg_SetAddrHeader(t *testing.T) {
"tina.tester@example.com"); err != nil { "tina.tester@example.com"); err != nil {
t.Fatalf("failed to set address header, err: %s", err) t.Fatalf("failed to set address header, err: %s", err)
} }
addresses, ok := message.addrHeader[HeaderFrom] checkAddrHeader(t, message, HeaderFrom, "SetAddrHeader", 0, 1, "toni.tester@example.com", "")
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) { t.Run("SetAddrHeader with invalid address", func(t *testing.T) {
for _, tt := range addrHeaderTests { for _, tt := range addrHeaderTests {
@ -852,26 +729,8 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
t.Fatal("message is nil") t.Fatal("message is nil")
} }
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com") message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com")
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 1,
if !ok { "toni.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -883,26 +742,8 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
t.Fatal("message is nil") t.Fatal("message is nil")
} }
message.SetAddrHeaderIgnoreInvalid(tt.header, "<toni.tester@example.com>") message.SetAddrHeaderIgnoreInvalid(tt.header, "<toni.tester@example.com>")
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 1,
if !ok { "toni.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -920,38 +761,10 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
} }
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com", message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com",
"tina.tester@example.com") "tina.tester@example.com")
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 2,
if !ok { "toni.tester@example.com", "")
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header) checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 1, 2,
} "tina.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -968,39 +781,11 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
t.Fatal("message is nil") t.Fatal("message is nil")
} }
message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com", message.SetAddrHeaderIgnoreInvalid(tt.header, "toni.tester@example.com",
"invalid", "valid@address.tld") "invalid", "tina.tester@example.com")
addresses, ok := message.addrHeader[tt.header] checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 0, 2,
if !ok { "toni.tester@example.com", "")
t.Fatalf("failed to set address header, addrHeader field for %s is not set", tt.header) checkAddrHeader(t, message, tt.header, "SetAddrHeaderIgnoreInvalid", 1, 2,
} "tina.tester@example.com", "")
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)
}
}) })
} }
}) })
@ -1011,26 +796,7 @@ func TestMsg_SetAddrHeaderIgnoreInvalid(t *testing.T) {
} }
message.addrHeader = nil message.addrHeader = nil
message.SetAddrHeaderIgnoreInvalid(HeaderFrom, "toni.tester@example.com", "tina.tester@example.com") message.SetAddrHeaderIgnoreInvalid(HeaderFrom, "toni.tester@example.com", "tina.tester@example.com")
addresses, ok := message.addrHeader[HeaderFrom] checkAddrHeader(t, message, HeaderFrom, "SetAddrHeaderIgnoreInvalid", 0, 1, "toni.tester@example.com", "")
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) { t.Run("SetAddrHeaderIgnoreInvalid with invalid addresses only", func(t *testing.T) {
for _, tt := range addrHeaderTests { for _, tt := range addrHeaderTests {
@ -1062,24 +828,7 @@ func TestMsg_EnvelopeFrom(t *testing.T) {
if err := message.EnvelopeFrom("toni.tester@example.com"); err != nil { if err := message.EnvelopeFrom("toni.tester@example.com"); err != nil {
t.Fatalf("failed to set envelope from: %s", err) t.Fatalf("failed to set envelope from: %s", err)
} }
addresses, ok := message.addrHeader[HeaderEnvelopeFrom] checkAddrHeader(t, message, HeaderEnvelopeFrom, "EnvelopeFrom", 0, 1, "toni.tester@example.com", "")
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) { t.Run("EnvelopeFrom with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1110,25 +859,7 @@ func TestMsg_EnvelopeFromFormat(t *testing.T) {
if err := message.EnvelopeFromFormat("Toni Tester", "toni.tester@example.com"); err != nil { if err := message.EnvelopeFromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
t.Fatalf("failed to set envelope From: %s", err) t.Fatalf("failed to set envelope From: %s", err)
} }
addresses, ok := message.addrHeader[HeaderEnvelopeFrom] checkAddrHeader(t, message, HeaderEnvelopeFrom, "FromFormat", 0, 1, "toni.tester@example.com", "Toni Tester")
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) { t.Run("EnvelopeFromFormat with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1159,24 +890,7 @@ func TestMsg_From(t *testing.T) {
if err := message.From("toni.tester@example.com"); err != nil { if err := message.From("toni.tester@example.com"); err != nil {
t.Fatalf("failed to set From: %s", err) t.Fatalf("failed to set From: %s", err)
} }
addresses, ok := message.addrHeader[HeaderFrom] checkAddrHeader(t, message, HeaderFrom, "From", 0, 1, "toni.tester@example.com", "")
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) { t.Run("From with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1224,25 +938,7 @@ func TestMsg_FromFormat(t *testing.T) {
if err := message.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil { if err := message.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
t.Fatalf("failed to set From: %s", err) t.Fatalf("failed to set From: %s", err)
} }
addresses, ok := message.addrHeader[HeaderFrom] checkAddrHeader(t, message, HeaderFrom, "FromFormat", 0, 1, "toni.tester@example.com", "Toni Tester")
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) { t.Run("FromFormat with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1273,24 +969,7 @@ func TestMsg_To(t *testing.T) {
if err := message.To("toni.tester@example.com"); err != nil { if err := message.To("toni.tester@example.com"); err != nil {
t.Fatalf("failed to set To: %s", err) t.Fatalf("failed to set To: %s", err)
} }
addresses, ok := message.addrHeader[HeaderTo] checkAddrHeader(t, message, HeaderTo, "To", 0, 1, "toni.tester@example.com", "")
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) { t.Run("To with multiple valid addresses", func(t *testing.T) {
message := NewMsg() 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 { if err := message.To("toni.tester@example.com", "tina.tester@example.com"); err != nil {
t.Fatalf("failed to set To: %s", err) t.Fatalf("failed to set To: %s", err)
} }
addresses, ok := message.addrHeader[HeaderTo] checkAddrHeader(t, message, HeaderTo, "To", 0, 2, "toni.tester@example.com", "")
if !ok { checkAddrHeader(t, message, HeaderTo, "To", 1, 2, "tina.tester@example.com", "")
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) { t.Run("To with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1379,35 +1031,8 @@ func TestMsg_AddTo(t *testing.T) {
if err := message.AddTo("tina.tester@example.com"); err != nil { if err := message.AddTo("tina.tester@example.com"); err != nil {
t.Fatalf("failed to set additional To: %s", err) t.Fatalf("failed to set additional To: %s", err)
} }
addresses, ok := message.addrHeader[HeaderTo] checkAddrHeader(t, message, HeaderTo, "AddTo", 0, 2, "toni.tester@example.com", "")
if !ok { checkAddrHeader(t, message, HeaderTo, "AddTo", 1, 2, "tina.tester@example.com", "")
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) { t.Run("AddTo with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1420,13 +1045,7 @@ func TestMsg_AddTo(t *testing.T) {
if err := message.AddTo("invalid"); err == nil { if err := message.AddTo("invalid"); err == nil {
t.Errorf("AddTo should fail with invalid address") t.Errorf("AddTo should fail with invalid address")
} }
addresses, ok := message.addrHeader[HeaderTo] checkAddrHeader(t, message, HeaderTo, "AddTo", 0, 1, "toni.tester@example.com", "")
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))
}
}) })
} }
@ -1442,36 +1061,8 @@ func TestMsg_AddToFormat(t *testing.T) {
if err := message.AddToFormat("Tina Tester", "tina.tester@example.com"); err != nil { if err := message.AddToFormat("Tina Tester", "tina.tester@example.com"); err != nil {
t.Fatalf("failed to set additional To: %s", err) t.Fatalf("failed to set additional To: %s", err)
} }
addresses, ok := message.addrHeader[HeaderTo] checkAddrHeader(t, message, HeaderTo, "AddToFormat", 0, 2, "toni.tester@example.com", "")
if !ok { checkAddrHeader(t, message, HeaderTo, "AddToFormat", 1, 2, "tina.tester@example.com", "Tina Tester")
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) { t.Run("AddToFormat with invalid address", func(t *testing.T) {
message := NewMsg() message := NewMsg()
@ -1484,18 +1075,93 @@ func TestMsg_AddToFormat(t *testing.T) {
if err := message.AddToFormat("Invalid", "invalid"); err == nil { if err := message.AddToFormat("Invalid", "invalid"); err == nil {
t.Errorf("AddToFormat should fail with invalid address") t.Errorf("AddToFormat should fail with invalid address")
} }
addresses, ok := message.addrHeader[HeaderTo] checkAddrHeader(t, message, HeaderTo, "AddToFormat", 0, 1, "toni.tester@example.com", "")
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) { 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)
}
} }
/* /*