-
Notifications
You must be signed in to change notification settings - Fork 4
/
util_test.go
75 lines (66 loc) · 1.72 KB
/
util_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
package pgpmail
import (
"bytes"
"text/template"
)
type TestData struct {
From string
To string
Subject string
Body string
MultipartType string
MultipartExtra string
Parts []string
Preamble string
}
const defaultFrom = "[email protected]"
const defaultTo = "[email protected], [email protected]"
const defaultSubject = "Test Message"
func (tdata *TestData) Message() *Message {
r := NewReader(tdata.String())
m, err := r.ReadMessage()
if err != nil {
panic("error parsing message from template: " + err.Error())
}
return m
}
func (tdata *TestData) String() string {
if tdata.From == "" {
tdata.From = defaultFrom
}
if tdata.To == "" {
tdata.To = defaultTo
}
if tdata.Subject == "" {
tdata.Subject = defaultSubject
}
t := createTemplate(tdata)
b := new(bytes.Buffer)
t.Execute(b, tdata)
return insertCR(b.String())
}
func createTemplate(tdata *TestData) *template.Template {
if len(tdata.Parts) > 0 && tdata.MultipartType != "" {
return template.Must(template.New("multipart").Parse(multipartTemplate))
}
return template.Must(template.New("simple").Parse(simpleTemplate))
}
var multipartTemplate = `From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
Mime-Version: 1.0
Content-Type: multipart/{{.MultipartType}}; boundary=001a11c167c8f1cb3104f8f4c019{{if .MultipartExtra}}; {{.MultipartExtra}}{{end}}
Content-Transfer-Encoding: 7bit
{{if .Preamble}}
{{.Preamble}}{{end}}{{range $part := .Parts}}
--001a11c167c8f1cb3104f8f4c019
{{$part}}{{end}}
--001a11c167c8f1cb3104f8f4c019--`
var simpleTemplate = `From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
{{.Body}}`