Skip to content

Commit

Permalink
Automatically add Date header
Browse files Browse the repository at this point in the history
  • Loading branch information
nkovacs committed Jul 30, 2015
1 parent 936c31e commit b20e2b7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/textproto"
"path/filepath"
"strings"
"time"
)

// Message Lint: http://tools.ietf.org/tools/msglint/
Expand Down Expand Up @@ -178,6 +179,11 @@ func (m *Message) Bytes() ([]byte, error) {
header.Add("Subject", quotedSubject)
}

// Date
if _, ok := m.Headers["Date"]; !ok {
header.Add("Date", time.Now().UTC().Format(time.RFC822))
}

for k, v := range m.Headers {
header[k] = v
}
Expand Down
64 changes: 64 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,67 @@ func TestHtmlPlainAttachment(t *testing.T) {
func TestNoBody(t *testing.T) {
testMail(t, false, false, false)
}

func TestAutoDate(t *testing.T) {
startTime := time.Now()
m := &Message{}
m.SetFrom("Doman Sender <[email protected]>")
m.AddTo("First person <[email protected]>")

m.Body = "Test message"

b, err := m.Bytes()
expectNoError(err)

t.Logf("Bytes: \n%s", b)

byteReader := bytes.NewReader(b)
bufReader := bufio.NewReader(byteReader)
headerReader := textproto.NewReader(bufReader)
header, err := headerReader.ReadMIMEHeader()
expectNoError(err)

dates, ok := header["Date"]
Expect(ok).To(BeTrue(), "Date header not found")
Expect(dates).NotTo(BeEmpty(), "Date header is empty")
Expect(dates).To(HaveLen(1), "More than one Date header found")

parsedTime, err := time.Parse(time.RFC822, dates[0])

t.Logf("%v", parsedTime)

Expect(parsedTime.Before(time.Now().Add(1*time.Minute))).To(BeTrue(), "Time in Date header is too low")
Expect(parsedTime.After(startTime.Add(-1*time.Minute))).To(BeTrue(), "Time in Date header is too high")
}

func TestManualDate(t *testing.T) {
msgTime := time.Now().Add(-30 * time.Minute)

m := &Message{}
m.SetFrom("Doman Sender <[email protected]>")
m.AddTo("First person <[email protected]>")

m.Headers = mail.Header{}
m.Headers["Date"] = []string{msgTime.Format(time.RFC822)}
m.Body = "Test message"

b, err := m.Bytes()
expectNoError(err)

t.Logf("Bytes: \n%s", b)

byteReader := bytes.NewReader(b)
bufReader := bufio.NewReader(byteReader)
headerReader := textproto.NewReader(bufReader)
header, err := headerReader.ReadMIMEHeader()
expectNoError(err)

dates, ok := header["Date"]
Expect(ok).To(BeTrue(), "Date header not found")
Expect(dates).NotTo(BeEmpty(), "Date header is empty")
Expect(dates).To(HaveLen(1), "More than one Date header found")

parsedTime, err := time.Parse(time.RFC822, dates[0])

Expect(parsedTime.Equal(msgTime.Truncate(time.Minute))).To(BeTrue(), "Time in Date header is not what we specified")
}

0 comments on commit b20e2b7

Please sign in to comment.