-
Notifications
You must be signed in to change notification settings - Fork 13
/
message_test.go
305 lines (250 loc) · 9.29 KB
/
message_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package mailout
import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/SchumacherFM/mailout/maillog"
"github.com/caddyserver/caddy"
"github.com/stretchr/testify/assert"
)
func testMessageServer(t *testing.T, caddyFile string, buf *bytes.Buffer, expectedMsgCount int) *httptest.Server {
c := caddy.NewTestController("http", caddyFile)
mc, err := parse(c)
if err != nil {
t.Fatal(err)
}
if err := mc.loadTemplate(); err != nil {
t.Fatal(err)
}
if err := mc.loadPGPKeys(); err != nil {
t.Fatal(err)
}
assert.Exactly(t, expectedMsgCount, mc.messageCount, "\nCaddyFile %s\n", caddyFile)
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
msg := newMessage(mc, r).build()
if _, err := msg.WriteTo(buf); err != nil {
t.Fatal(err)
}
}))
}
func testDoPost(t *testing.T, url string, data url.Values) *http.Response {
hClient := &http.Client{}
//hClient.Timeout = time.Millisecond
resp, err := hClient.PostForm(url, data)
if err != nil {
t.Fatal(err)
}
if 200 != resp.StatusCode {
t.Fatalf("Want StatusCode 200, Have %d", resp.StatusCode)
}
return resp
}
func TestMessagePlainTextAllFormFields(t *testing.T) {
const caddyFile = `mailout {
subject "Email from {{ .Form.Get \"firstname\" }} {{.Form.Get \"lastname\"}}"
body testdata/mail_plainTextMessage.txt
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 1)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Ken")
data.Set("lastname", "Thompson")
data.Set("email", "[email protected]")
data.Set("name", "Ken Thompson")
testDoPost(t, srv.URL, data)
assert.Len(t, buf.String(), 424) // whenever you change the template, change also here
assert.Contains(t, buf.String(), "Email [email protected]")
assert.Contains(t, buf.String(), `From: "Ken Thompson" <[email protected]>`)
assert.Contains(t, buf.String(), "Subject: Email from Ken Thompson")
assert.Contains(t, buf.String(), "Cc: [email protected], [email protected]")
//t.Log(buf.String())
}
func TestMessagePlainTextWithOutFormInputName(t *testing.T) {
const caddyFile = `mailout {
subject "Email from {{ .Form.Get \"firstname\" }} {{.Form.Get \"lastname\"}}"
body testdata/mail_plainTextMessage.txt
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 1)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Ken")
data.Set("lastname", "Thompson")
data.Set("email", "[email protected]")
testDoPost(t, srv.URL, data)
assert.Len(t, buf.String(), 359) // whenever you change the template, change also here
assert.Contains(t, buf.String(), "Email [email protected]")
assert.Contains(t, buf.String(), "Subject: Email from Ken Thompson")
assert.Contains(t, buf.String(), `From: [email protected]`)
}
func TestMessageHTML(t *testing.T) {
const caddyFile = `mailout {
subject " HTML Email via {{ .Form.Get \"firstname\" }} {{.Form.Get \"lastname\"}}"
body testdata/mail_tpl.html
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 1)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Ken")
data.Set("lastname", "Thompson")
data.Set("email", "[email protected]")
data.Set("name", "Ken S. Thompson")
testDoPost(t, srv.URL, data)
assert.True(t, buf.Len() > 10000) // whenever you change the template, change also here
assert.Contains(t, buf.String(), "<h3>Thank you for contacting us, Ken Thompson</h3>")
assert.Contains(t, buf.String(), "<h3>Sir Ken S. Thompson")
assert.Contains(t, buf.String(), "Subject: =?UTF-8?q?=EF=A3=BF_HTML_Email_via_Ken_Thompson?=")
assert.NotContains(t, buf.String(), "Bcc: [email protected]")
}
func TestMessagePlainPGPSingleKey(t *testing.T) {
const caddyFile = `mailout {
cc "[email protected]"
subject "Encrypted contact 🔑"
body testdata/mail_plainTextMessage.txt
[email protected] testdata/B06469EE_nopw.pub.asc
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 2)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Ken")
data.Set("lastname", "Thompson")
data.Set("email", "[email protected]")
data.Set("name", "Ken Thompson")
testDoPost(t, srv.URL, data)
assert.Len(t, buf.String(), 2710) // whenever you change the template, change also here
assert.Contains(t, buf.String(), "Subject: =?UTF-8?q?Encrypted_contact_=F0=9F=94=91?=")
assert.Contains(t, buf.String(), "Cc: [email protected]")
assert.Exactly(t, 1, bytes.Count(buf.Bytes(), maillog.MultiMessageSeparator))
assert.Contains(t, buf.String(), `This shows the content of a text template.`)
//t.Log(buf.String())
}
func TestMessagePlainPGPMultipleKey(t *testing.T) {
const caddyFile = `mailout {
subject "Encrypted contact"
body testdata/mail_plainTextMessage.txt
[email protected] testdata/B06469EE_nopw.pub.asc
[email protected] testdata/6AD0EE9E_nopw.pub.asc
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 3)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Ken")
data.Set("lastname", "Thompson")
data.Set("email", "[email protected]")
data.Set("name", "Ken Thompson")
testDoPost(t, srv.URL, data)
assert.Len(t, buf.String(), 4957) // whenever you change the template, change also here
assert.Exactly(t, 3, bytes.Count(buf.Bytes(), []byte("Subject: Encrypted contact")))
assert.Exactly(t, 2, bytes.Count(buf.Bytes(), []byte(`Content-Disposition: inline; filename="encrypted.gpg"`)))
assert.NotContains(t, buf.String(), "Cc: [email protected]")
assert.Exactly(t, 2, bytes.Count(buf.Bytes(), maillog.MultiMessageSeparator))
assert.Contains(t, buf.String(), `This shows the content of a text template.`)
assert.Contains(t, buf.String(), `To: [email protected]`)
assert.Contains(t, buf.String(), `To: [email protected]`)
assert.Contains(t, buf.String(), `Cc: [email protected]`)
//t.Log(buf.String())
}
func TestMessagePlainText_FromEmailName(t *testing.T) {
const caddyFile = `mailout {
from_email [email protected]
from_name "Gold Marie"
subject "Email from {{ .Form.Get \"firstname\" }} {{.Form.Get \"lastname\"}}"
body testdata/mail_plainTextMessage.txt
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 1)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Marie")
data.Set("lastname", "Pech")
data.Set("email", "[email protected]")
data.Set("name", "Pech Marie")
testDoPost(t, srv.URL, data)
assert.Len(t, buf.String(), 373) // whenever you change the template, change also here
assert.Contains(t, buf.String(), "Email [email protected]")
assert.Contains(t, buf.String(), `From: "Gold Marie" <[email protected]>`)
assert.Contains(t, buf.String(), "Subject: Email from Marie Pech")
//t.Log(buf.String())
}
func TestMessagePlainText_FromEmailOnly(t *testing.T) {
const caddyFile = `mailout {
from_email [email protected]
subject "Email from {{ .Form.Get \"firstname\" }} {{.Form.Get \"lastname\"}}"
body testdata/mail_plainTextMessage.txt
}`
buf := new(bytes.Buffer)
srv := testMessageServer(t, caddyFile, buf, 1)
defer srv.Close()
data := make(url.Values)
data.Set("firstname", "Marie")
data.Set("lastname", "Pech")
data.Set("email", "[email protected]")
data.Set("name", "Pech Marie")
testDoPost(t, srv.URL, data)
assert.Len(t, buf.String(), 358) // whenever you change the template, change also here
assert.Contains(t, buf.String(), "Email [email protected]")
assert.Contains(t, buf.String(), `From: [email protected]`)
assert.Contains(t, buf.String(), "Subject: Email from Marie Pech")
//t.Log(buf.String())
}
// 0.4.ms per PGP message
// BenchmarkMessagePlainPGP-4 3000 405413 ns/op 37530 B/op 176 allocs/op
func BenchmarkMessagePlainPGP(b *testing.B) {
const caddyFile = `mailout {
cc "[email protected]"
subject "Encrypted contact 🔑"
body testdata/mail_plainTextMessage.txt
publickey testdata/B06469EE_nopw.pub.asc
}`
c := caddy.NewTestController("http", caddyFile)
mc, err := parse(c)
if err != nil {
b.Fatal(err)
}
if err := mc.loadTemplate(); err != nil {
b.Fatal(err)
}
if err := mc.loadPGPKeys(); err != nil {
b.Fatal(err)
}
data := make(url.Values)
data.Set("firstname", "Ken")
data.Set("lastname", "Thompson")
data.Set("email", "[email protected]")
data.Set("name", "Ken Thompson")
req, err := http.NewRequest("POST", "/mailout", nil)
if err != nil {
b.Fatal(err)
}
req.PostForm = data
buf := new(bytes.Buffer)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
msg := newMessage(mc, req).build()
if _, err := msg.WriteTo(buf); err != nil {
b.Fatal(err)
}
buf.Reset()
}
}