-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1915,6 +1915,76 @@ func TestMsg_IsDelivered(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestMsg_RequestMDNTo(t *testing.T) { | ||
t.Run("RequestMDNTo with valid address", func(t *testing.T) { | ||
message := NewMsg() | ||
if message == nil { | ||
t.Fatal("message is nil") | ||
} | ||
if err := message.RequestMDNTo("[email protected]"); err != nil { | ||
t.Fatalf("failed to set RequestMDNTo: %s", err) | ||
} | ||
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 1, "<[email protected]>") | ||
}) | ||
t.Run("RequestMDNTo with valid address and nil-genHeader", func(t *testing.T) { | ||
message := NewMsg() | ||
if message == nil { | ||
t.Fatal("message is nil") | ||
} | ||
message.genHeader = nil | ||
if err := message.RequestMDNTo("[email protected]"); err != nil { | ||
t.Fatalf("failed to set RequestMDNTo: %s", err) | ||
} | ||
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 1, "<[email protected]>") | ||
}) | ||
t.Run("RequestMDNTo with multiple valid addresses", func(t *testing.T) { | ||
message := NewMsg() | ||
if message == nil { | ||
t.Fatal("message is nil") | ||
} | ||
if err := message.RequestMDNTo("[email protected]", "[email protected]"); err != nil { | ||
t.Fatalf("failed to set RequestMDNTo: %s", err) | ||
} | ||
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 2, "<[email protected]>") | ||
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 1, 2, "<[email protected]>") | ||
}) | ||
t.Run("RequestMDNTo with invalid address", func(t *testing.T) { | ||
message := NewMsg() | ||
if message == nil { | ||
t.Fatal("message is nil") | ||
} | ||
if err := message.RequestMDNTo("invalid"); err == nil { | ||
t.Fatalf("RequestMDNTo should fail with invalid address") | ||
} | ||
}) | ||
t.Run("RequestMDNTo with empty string should fail", func(t *testing.T) { | ||
message := NewMsg() | ||
if message == nil { | ||
t.Fatal("message is nil") | ||
} | ||
if err := message.RequestMDNTo(""); err == nil { | ||
t.Fatalf("RequestMDNTo should fail with invalid address") | ||
} | ||
}) | ||
t.Run("RequestMDNTo with different RFC5322 addresses", func(t *testing.T) { | ||
message := NewMsg() | ||
if message == nil { | ||
t.Fatal("message is nil") | ||
} | ||
for _, tt := range rfc5322Test { | ||
t.Run(tt.value, func(t *testing.T) { | ||
err := message.RequestMDNTo(tt.value) | ||
if err != nil && tt.valid { | ||
t.Errorf("RequestMDNTo on address %s should succeed, but failed with: %s", tt.value, err) | ||
} | ||
if err == nil && !tt.valid { | ||
t.Errorf("RequestMDNTo on address %s should fail, but succeeded", tt.value) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
// checkAddrHeader verifies the correctness of an AddrHeader in a Msg based on the provided criteria. | ||
// It checks whether the AddrHeader contains the correct address, name, and number of fields. | ||
func checkAddrHeader(t *testing.T, message *Msg, header AddrHeader, fn string, field, wantFields int, | ||
|