forked from mocktools/go-smtp-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helpers_test.go
139 lines (125 loc) · 2.95 KB
/
test_helpers_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package smtpmock
import (
"io"
"net"
"net/smtp"
"regexp"
"strings"
"time"
)
// Returns log message regex based on log level and message context
func loggerMessageRegex(logLevel, logMessage string) *regexp.Regexp {
regex, _ := newRegex(logLevel + `: \d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\.\d{6} ` + logMessage)
return regex
}
// Creates configuration with default settings
func createConfiguration() *configuration {
return newConfiguration(ConfigurationAttr{})
}
// Creates not empty message
func createNotEmptyMessage() *Message {
return &Message{
heloRequest: "a",
heloResponse: "b",
mailfromRequest: "c",
mailfromResponse: "d",
rcpttoRequestResponse: [][]string{{"request", "response"}},
dataRequest: "c",
dataResponse: "d",
msgRequest: "a",
msgResponse: "b",
rsetRequest: "a",
rsetResponse: "b",
helo: true,
mailfrom: true,
rcptto: true,
data: true,
msg: true,
rset: true,
}
}
// Creates array of bytes with message body
func messageBody(from, to string) []byte {
return []byte(
strings.Join(
[]string{
"From: " + from,
"To: " + to,
"Subject: Test message for: " + to,
"Content-Type: text/html; charset=utf-8;",
"Test message context",
},
"\r\n",
),
)
}
// Runs full smtp flow
func runFullFlow(client *smtp.Client) error {
var err error
var wc io.WriteCloser
sender, receiver1, receiver2, receiver3 := "user@molo.com", "user1@olo.com", "user2@olo.com", "user3@olo.com"
if err = client.Mail(sender); err != nil {
return err
}
if err = client.Rcpt(receiver1); err != nil {
return err
}
wc, err = client.Data()
if err != nil {
return err
}
_, err = wc.Write(messageBody(sender, receiver1))
if err != nil {
return err
}
err = wc.Close()
if err != nil {
return err
}
if err = client.Reset(); err != nil {
return err
}
if err = client.Mail(sender); err != nil {
return err
}
if err = client.Rcpt(receiver2); err != nil {
return err
}
if err = client.Rcpt(receiver3); err != nil {
return err
}
wc, err = client.Data()
if err != nil {
return err
}
_, err = wc.Write(messageBody(sender, receiver2))
if err != nil {
return err
}
err = wc.Close()
if err != nil {
return err
}
return nil
}
// Runs successful SMTP session with target host
func runSuccessfulSMTPSession(hostAddress string, portNumber int, fullFlow bool) error {
connection, _ := net.DialTimeout(networkProtocol, serverWithPortNumber(hostAddress, portNumber), time.Duration(2)*time.Second)
client, _ := smtp.NewClient(connection, hostAddress)
var err error
if err = client.Hello("olo.com"); err != nil {
return err
}
if fullFlow {
if err = runFullFlow(client); err != nil {
return err
}
}
if err = client.Quit(); err != nil {
return err
}
if err = client.Close(); err != nil {
return err
}
return nil
}