Compare commits

..

24 commits

Author SHA1 Message Date
e808e0b972
Add comprehensive unit tests for email address getters
Introduced unit tests for GetFrom, GetFromString, GetTo, GetToString, GetCc, GetCcString, GetBcc, and GetBccString methods. These tests cover scenarios with single, multiple, and no addresses, ensuring correct functionality of email address retrieval methods.
2024-10-26 23:20:23 +02:00
22f56a0143
Add tests for GetAddrHeaderString in msg_test.go
Add unit tests for the GetAddrHeaderString function to verify correct behavior for various header types and address counts. These tests ensure the function correctly handles single and multiple addresses, as well as scenarios with no addresses.
2024-10-26 22:57:47 +02:00
d16ae61f64
Add test for GetAddrHeader with multiple valid addresses
Introduce a new test to validate the behavior of GetAddrHeader for handling multiple addresses (to, cc, bcc). This test ensures that the function correctly processes and returns the expected addresses in various headers.
2024-10-26 22:53:30 +02:00
5e5bcef696
Add tests for Msg_GetAddrHeader
Introduce unit tests for the GetAddrHeader function in the Msg struct. These tests cover various scenarios, including valid addresses and handling of headers like "from," "to," "cc," and "bcc."
2024-10-26 22:50:31 +02:00
7b600534ea
Add tests for Msg.GetRecipients method
Implemented comprehensive tests for the Msg.GetRecipients method, covering various scenarios such as recipients in "to", "cc", "bcc" fields and combinations of these. Ensured correct handling of each case and validation against expected recipient counts and specific addresses.
2024-10-26 22:31:13 +02:00
842d4373f2
Add missing newline at EOF
Ensured the file ends with a newline to comply with best practices. This change improves code readability and consistency with other files in the repository.
2024-10-26 22:14:56 +02:00
5c2831c331
Add tests for MDN address formatting and GetSender logic
Implemented tests for `RequestMDNToFormat`, `RequestMDNAddTo`, and `RequestMDNAddToFormat` methods to ensure proper address handling. Added comprehensive `GetSender` method tests to verify sender retrieval in various scenarios. This enhances coverage and robustness of email message handling functionality.
2024-10-26 22:13:51 +02:00
4fe9022815
Refactor RequestMDNAddTo
Simplify the address handling for the "Disposition-Notification-To" header in msg.go. The code now directly reassigns the previously fetched addresses and ensures appending the new address effectively. This improves code readability and correctness in updating the header.
2024-10-26 22:13:37 +02:00
4f97cd8261
Initialize genHeader in RequestMDNTo method
Add checks to initialize `genHeader` if it is nil in the RequestMDNTo method to prevent potential nil map assignment errors. The newly implemented tests showed that this method was never actually working and the old test was inefficient to identify this.
2024-10-26 21:33:43 +02:00
9e51dba82a
Add new tests for Msg methods
Introduce tests for SetImportance, SetOrganization, SetUserAgent, and IsDelivered methods in the Msg type. These additions ensure the correct behavior and robustness of the email handling functionality.
2024-10-26 21:07:48 +02:00
cf117d320b
Add nil check in testMessage helper function
Added a conditional check to ensure 'NewMsg()' does not return nil in the 'testMessage' helper function. This update will prevent potential nil pointer dereferences and improve the robustness of the test.
2024-10-26 21:07:04 +02:00
42c63791ef
Move delivery status update after writer close check
Relocated the `isDelivered` flag update to occur after the writer's close method validation. This ensures that the message delivery status is only marked as true if no errors arise during the writer close process. This showed up in the new test cases that covered errors on closing the DATA channel. This can already happen before the first byte is sent to the server. Therefore isDelivered should be set after a successful close.
2024-10-26 21:06:21 +02:00
f5279cd584
Refactor character encoding test cases formatting
Reformatted the test cases for European umlaut characters for consistency and readability. This change does not affect the execution or output of the tests.
2024-10-26 20:22:04 +02:00
ef3f103c30
Add additional tests for setting message headers
Introduce tests for SetMessageIDWithValue, SetDate, and SetDateWithValue functions. Enhance coverage and validation for these methods by comparing generated headers and parsed dates.
2024-10-26 20:08:16 +02:00
ae15a12ce5
Refactor SetDate to use SetDateWithValue
Replaces direct time formatting in SetDate with a call to SetDateWithValue, improving code reusability and readability. The new approach centralizes date formatting logic in one method.
2024-10-26 19:57:02 +02:00
ea5b02bfdd
Add tests for SetMessageID and GetMessageID methods
Introduce unit tests to ensure SetMessageID generates unique IDs and GetMessageID correctly formats various input IDs. Additionally, verify GetMessageID returns an empty string when no ID is set.
2024-10-26 19:30:48 +02:00
591425bb99
Add test for European umlaut characters and remove others
Added a test case for European umlaut characters in msg_test.go. Removed redundant test cases related to address handlers (To, Cc, Bcc) and other helper methods to streamline the code.
2024-10-26 19:10:25 +02:00
007286fc5e
Add unit tests for Msg.Subject() method
Introduced unit tests to verify the encoding of different character sets in the Msg.Subject() method. Tests include cases for Latin characters, Japanese, Simplified Chinese, Cyrillic, and Emoji to ensure proper encoding handling.
2024-10-26 19:03:31 +02:00
5b602be818
Add tests for ReplyToFormat method
Implement new tests for the Msg.ReplyToFormat method to ensure it correctly handles both valid and invalid email addresses. These tests validate the ReplyTo header formatting and error handling.
2024-10-26 18:46:46 +02:00
96d45c26bc
Add tests for Msg.ReplyTo function
Introduce unit tests for the `ReplyTo` method in the `Msg` struct to validate both correct handling of valid email addresses and proper failure responses for invalid ones. Further, refactor and add helper functions to check generated headers in messages.
2024-10-26 18:40:53 +02:00
953a4b4df1
Add Bcc-related unit tests to msg_test.go
Introduce comprehensive unit tests for Bcc functionalities, including validation of single and multiple addresses, various invalid input scenarios, and RFC5322 compliance checks. Additionally, implement tests for AddBcc, AddBccFormat, BccIgnoreInvalid, and BccFromString methods to ensure robust handling of different Bcc cases.
2024-10-26 18:29:51 +02:00
f079ea09eb
Improve BccFromString to handle spaces and empty addresses
Trim spaces from email addresses and skip empty addresses in BccFromString method. This ensures that the BCC list only includes valid, non-empty email addresses, enhancing email sending reliability.
2024-10-26 18:29:26 +02:00
03cb09c3bd
Add tests for Cc, AddCc, AddCcFormat, CcIgnoreInvalid, and CcFromString
This commit introduces comprehensive tests for various "Cc" related methods in the `Msg` struct. It includes test cases for valid and invalid email addresses for methods: `Cc`, `AddCc`, `AddCcFormat`, `CcIgnoreInvalid`, and `CcFromString`, ensuring robust handling of different scenarios and edge cases.
2024-10-26 18:22:01 +02:00
855d7f0867
Refine CcFromString to handle spaces and empty addresses.
This change ensures that email addresses with leading or trailing spaces are trimmed, and empty addresses resulting from multiple commas are ignored. This helps prevent potential errors with malformed email addresses in the "Cc" field.
2024-10-26 18:21:50 +02:00
4 changed files with 1984 additions and 791 deletions

View file

@ -1261,14 +1261,13 @@ func (c *Client) sendSingleMsg(message *Msg) error {
affectedMsg: message,
}
}
message.isDelivered = true
if err = writer.Close(); err != nil {
return &SendError{
Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err),
affectedMsg: message,
}
}
message.isDelivered = true
if err = c.Reset(); err != nil {
return &SendError{

View file

@ -3527,6 +3527,9 @@ func parseJSONLog(t *testing.T, buf *bytes.Buffer) logData {
func testMessage(t *testing.T) *Msg {
t.Helper()
message := NewMsg()
if message == nil {
t.Fatal("failed to create new message")
}
if err := message.From(TestSenderValid); err != nil {
t.Errorf("failed to set sender address: %s", err)
}

40
msg.go
View file

@ -847,7 +847,16 @@ func (m *Msg) CcIgnoreInvalid(rcpts ...string) {
// References:
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3
func (m *Msg) CcFromString(rcpts string) error {
return m.Cc(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.Cc(dst...)
}
// Bcc sets one or more "BCC" (blind carbon copy) addresses in the mail body for the Msg.
@ -933,7 +942,16 @@ func (m *Msg) BccIgnoreInvalid(rcpts ...string) {
// References:
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3
func (m *Msg) BccFromString(rcpts string) error {
return m.Bcc(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.Bcc(dst...)
}
// ReplyTo sets the "Reply-To" address for the Msg, specifying where replies should be sent.
@ -1069,8 +1087,7 @@ func (m *Msg) SetBulk() {
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.3
// - https://datatracker.ietf.org/doc/html/rfc1123
func (m *Msg) SetDate() {
now := time.Now().Format(time.RFC1123Z)
m.SetGenHeader(HeaderDate, now)
m.SetDateWithValue(time.Now())
}
// SetDateWithValue sets the "Date" header for the Msg using the provided time value in a valid RFC 1123 format.
@ -1170,6 +1187,9 @@ func (m *Msg) IsDelivered() bool {
// References:
// - https://datatracker.ietf.org/doc/html/rfc8098
func (m *Msg) RequestMDNTo(rcpts ...string) error {
if m.genHeader == nil {
m.genHeader = make(map[Header][]string)
}
var addresses []string
for _, addrVal := range rcpts {
address, err := mail.ParseAddress(addrVal)
@ -1178,9 +1198,7 @@ func (m *Msg) RequestMDNTo(rcpts ...string) error {
}
addresses = append(addresses, address.String())
}
if _, ok := m.genHeader[HeaderDispositionNotificationTo]; ok {
m.genHeader[HeaderDispositionNotificationTo] = addresses
}
m.genHeader[HeaderDispositionNotificationTo] = addresses
return nil
}
@ -1219,11 +1237,11 @@ func (m *Msg) RequestMDNAddTo(rcpt string) error {
return fmt.Errorf(errParseMailAddr, rcpt, err)
}
var addresses []string
addresses = append(addresses, m.genHeader[HeaderDispositionNotificationTo]...)
addresses = append(addresses, address.String())
if _, ok := m.genHeader[HeaderDispositionNotificationTo]; ok {
m.genHeader[HeaderDispositionNotificationTo] = addresses
if current, ok := m.genHeader[HeaderDispositionNotificationTo]; ok {
addresses = current
}
addresses = append(addresses, address.String())
m.genHeader[HeaderDispositionNotificationTo] = addresses
return nil
}

File diff suppressed because it is too large Load diff