-
Notifications
You must be signed in to change notification settings - Fork 6
/
sendmail_test.go
111 lines (99 loc) · 2.63 KB
/
sendmail_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package sendmail_test
import (
"bytes"
"encoding/base64"
"reflect"
"strings"
"testing"
"github.com/n0madic/sendmail"
"github.com/n0madic/sendmail/test"
)
type testData struct {
initial sendmail.Config
expected sendmail.Config
}
var testConfigs = []*testData{
{
initial: sendmail.Config{
Sender: "sender@localhost",
Recipients: []string{"recipient@localhost"},
Subject: "subject",
Body: []byte("TEST"),
PortSMTP: test.PortSMTP,
},
expected: sendmail.Config{
Sender: "sender@localhost",
Recipients: []string{"recipient@localhost"},
Subject: "subject",
Body: []byte("TEST"),
},
},
{
initial: sendmail.Config{
Sender: "",
Recipients: []string{},
Subject: "",
Body: []byte(`From: sender@localhost
To: recipient@localhost
Subject: subject
TEST`,
),
PortSMTP: test.PortSMTP,
},
expected: sendmail.Config{
Sender: "sender@localhost",
Recipients: []string{"recipient@localhost"},
Subject: "subject",
Body: []byte("TEST"),
},
},
}
func TestNewEnvelope(t *testing.T) {
for _, config := range testConfigs {
envelope, err := sendmail.NewEnvelope(&config.initial)
if err != nil {
t.Error(err)
return
}
if envelope.Header["From"][0] != config.expected.Sender {
t.Error("Expected", config.expected.Sender, "got", envelope.Header["From"][0])
}
if !reflect.DeepEqual(envelope.Header["To"], config.expected.Recipients) {
t.Error("Expected", config.expected.Recipients, "got", envelope.Header["To"])
}
subject := []byte(envelope.Header["Subject"][0])
if bytes.Contains(subject, []byte("=?UTF-8?B?")) {
subject, err = base64.StdEncoding.DecodeString(
strings.Replace(envelope.Header["Subject"][0], "=?UTF-8?B?", "", 1),
)
if err != nil {
t.Error(err)
return
}
}
if string(subject) != config.expected.Subject {
t.Error("Expected", config.expected.Subject, "got", subject)
}
buf := new(bytes.Buffer)
buf.ReadFrom(envelope.Body)
if !reflect.DeepEqual(bytes.TrimSuffix(buf.Bytes(), []byte("\r\n")), config.expected.Body) {
t.Error("Expected", config.expected.Body, "got", bytes.TrimSpace(buf.Bytes()))
}
}
}
func TestGenerateMessage(t *testing.T) {
expectedMessage := "From: sender@localhost\r\nSubject: =?UTF-8?B?c3ViamVjdA==\r\nTo: recipient@localhost\r\n\r\nTEST\r\n"
envelope, err := sendmail.NewEnvelope(&testConfigs[0].initial)
if err != nil {
t.Error(err)
return
}
message, err := envelope.GenerateMessage()
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(message, []byte(expectedMessage)) {
t.Errorf("EXPECTED:\n%s\nGOT:\n%s", expectedMessage, message)
}
}