From 33430073ec0d91e51a29ef3c9237b5517424b07b Mon Sep 17 00:00:00 2001 From: Alexandre Cesaro Date: Thu, 22 Oct 2015 10:58:06 +0200 Subject: [PATCH] Fixed a panic when a Message contains a nil header Fixes #41 Fixes #42 --- message_test.go | 16 ++++++++++++++++ writeto.go | 21 +++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/message_test.go b/message_test.go index fdd9ff9..1876a7e 100644 --- a/message_test.go +++ b/message_test.go @@ -477,6 +477,22 @@ func TestBase64LineLength(t *testing.T) { testMessage(t, m, 0, want) } +func TestEmptyHeader(t *testing.T) { + m := NewMessage() + m.SetHeaders(map[string][]string{ + "From": {"from@example.com"}, + "X-Empty": nil, + }) + + want := &message{ + from: "from@example.com", + content: "From: from@example.com\r\n" + + "X-Empty: \r\n", + } + + testMessage(t, m, 0, want) +} + func testMessage(t *testing.T, m *Message, bCount int, want *message) { err := Send(stubSendMail(t, bCount, want), m) if err != nil { diff --git a/writeto.go b/writeto.go index 57a1dd7..0031bde 100644 --- a/writeto.go +++ b/writeto.go @@ -154,23 +154,16 @@ func (w *messageWriter) writeString(s string) { w.n += int64(n) } -func (w *messageWriter) writeStrings(a []string, sep string) { - if len(a) > 0 { - w.writeString(a[0]) - if len(a) == 1 { - return - } - } - for _, s := range a[1:] { - w.writeString(sep) - w.writeString(s) - } -} - func (w *messageWriter) writeHeader(k string, v ...string) { w.writeString(k) w.writeString(": ") - w.writeStrings(v, ", ") + if len(v) > 0 { + w.writeString(v[0]) + for _, s := range v[1:] { + w.writeString(", ") + w.writeString(s) + } + } w.writeString("\r\n") }