Fixed a panic when a Message contains a nil header

Fixes #41
Fixes #42
This commit is contained in:
Alexandre Cesaro 2015-10-22 10:58:06 +02:00
parent c80f25c521
commit 33430073ec
2 changed files with 23 additions and 14 deletions

View File

@ -477,6 +477,22 @@ func TestBase64LineLength(t *testing.T) {
testMessage(t, m, 0, want) 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) { func testMessage(t *testing.T, m *Message, bCount int, want *message) {
err := Send(stubSendMail(t, bCount, want), m) err := Send(stubSendMail(t, bCount, want), m)
if err != nil { if err != nil {

View File

@ -154,23 +154,16 @@ func (w *messageWriter) writeString(s string) {
w.n += int64(n) 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) { func (w *messageWriter) writeHeader(k string, v ...string) {
w.writeString(k) w.writeString(k)
w.writeString(": ") 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") w.writeString("\r\n")
} }