Skip to content

Commit 4f97cd8

Browse files
author
Winni Neessen
committed
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.
1 parent 9e51dba commit 4f97cd8

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

msg.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,9 @@ func (m *Msg) IsDelivered() bool {
11871187
// References:
11881188
// - https://datatracker.ietf.org/doc/html/rfc8098
11891189
func (m *Msg) RequestMDNTo(rcpts ...string) error {
1190+
if m.genHeader == nil {
1191+
m.genHeader = make(map[Header][]string)
1192+
}
11901193
var addresses []string
11911194
for _, addrVal := range rcpts {
11921195
address, err := mail.ParseAddress(addrVal)
@@ -1195,9 +1198,7 @@ func (m *Msg) RequestMDNTo(rcpts ...string) error {
11951198
}
11961199
addresses = append(addresses, address.String())
11971200
}
1198-
if _, ok := m.genHeader[HeaderDispositionNotificationTo]; ok {
1199-
m.genHeader[HeaderDispositionNotificationTo] = addresses
1200-
}
1201+
m.genHeader[HeaderDispositionNotificationTo] = addresses
12011202
return nil
12021203
}
12031204

msg_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,76 @@ func TestMsg_IsDelivered(t *testing.T) {
19151915
})
19161916
}
19171917

1918+
func TestMsg_RequestMDNTo(t *testing.T) {
1919+
t.Run("RequestMDNTo with valid address", func(t *testing.T) {
1920+
message := NewMsg()
1921+
if message == nil {
1922+
t.Fatal("message is nil")
1923+
}
1924+
if err := message.RequestMDNTo("[email protected]"); err != nil {
1925+
t.Fatalf("failed to set RequestMDNTo: %s", err)
1926+
}
1927+
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 1, "<[email protected]>")
1928+
})
1929+
t.Run("RequestMDNTo with valid address and nil-genHeader", func(t *testing.T) {
1930+
message := NewMsg()
1931+
if message == nil {
1932+
t.Fatal("message is nil")
1933+
}
1934+
message.genHeader = nil
1935+
if err := message.RequestMDNTo("[email protected]"); err != nil {
1936+
t.Fatalf("failed to set RequestMDNTo: %s", err)
1937+
}
1938+
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 1, "<[email protected]>")
1939+
})
1940+
t.Run("RequestMDNTo with multiple valid addresses", func(t *testing.T) {
1941+
message := NewMsg()
1942+
if message == nil {
1943+
t.Fatal("message is nil")
1944+
}
1945+
if err := message.RequestMDNTo("[email protected]", "[email protected]"); err != nil {
1946+
t.Fatalf("failed to set RequestMDNTo: %s", err)
1947+
}
1948+
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 2, "<[email protected]>")
1949+
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 1, 2, "<[email protected]>")
1950+
})
1951+
t.Run("RequestMDNTo with invalid address", func(t *testing.T) {
1952+
message := NewMsg()
1953+
if message == nil {
1954+
t.Fatal("message is nil")
1955+
}
1956+
if err := message.RequestMDNTo("invalid"); err == nil {
1957+
t.Fatalf("RequestMDNTo should fail with invalid address")
1958+
}
1959+
})
1960+
t.Run("RequestMDNTo with empty string should fail", func(t *testing.T) {
1961+
message := NewMsg()
1962+
if message == nil {
1963+
t.Fatal("message is nil")
1964+
}
1965+
if err := message.RequestMDNTo(""); err == nil {
1966+
t.Fatalf("RequestMDNTo should fail with invalid address")
1967+
}
1968+
})
1969+
t.Run("RequestMDNTo with different RFC5322 addresses", func(t *testing.T) {
1970+
message := NewMsg()
1971+
if message == nil {
1972+
t.Fatal("message is nil")
1973+
}
1974+
for _, tt := range rfc5322Test {
1975+
t.Run(tt.value, func(t *testing.T) {
1976+
err := message.RequestMDNTo(tt.value)
1977+
if err != nil && tt.valid {
1978+
t.Errorf("RequestMDNTo on address %s should succeed, but failed with: %s", tt.value, err)
1979+
}
1980+
if err == nil && !tt.valid {
1981+
t.Errorf("RequestMDNTo on address %s should fail, but succeeded", tt.value)
1982+
}
1983+
})
1984+
}
1985+
})
1986+
}
1987+
19181988
// checkAddrHeader verifies the correctness of an AddrHeader in a Msg based on the provided criteria.
19191989
// It checks whether the AddrHeader contains the correct address, name, and number of fields.
19201990
func checkAddrHeader(t *testing.T, message *Msg, header AddrHeader, fn string, field, wantFields int,

0 commit comments

Comments
 (0)