-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmail_test.go
150 lines (124 loc) · 4.38 KB
/
mail_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
package enmime
import (
"bufio"
"bytes"
"fmt"
"github.com/stretchrcom/testify/assert"
"net/mail"
"os"
"path/filepath"
"testing"
)
func TestIdentifySinglePart(t *testing.T) {
msg := readMessage("non-mime.raw")
assert.False(t, IsMultipartMessage(msg), "Failed to identify non-multipart message")
}
func TestIdentifyMultiPart(t *testing.T) {
msg := readMessage("html-mime-inline.raw")
assert.True(t, IsMultipartMessage(msg), "Failed to identify multipart MIME message")
}
func TestParseNonMime(t *testing.T) {
msg := readMessage("non-mime.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse non-MIME: %v", err)
}
assert.Contains(t, mime.Text, "This is a test mailing")
}
func TestParseMimeTree(t *testing.T) {
msg := readMessage("attachment.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.NotNil(t, mime.Root, "Message should have a root node")
}
func TestParseInlineText(t *testing.T) {
msg := readMessage("html-mime-inline.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Equal(t, mime.Text, "Test of text section")
}
func TestParseQuotedPrintable(t *testing.T) {
msg := readMessage("quoted-printable.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Contains(t, mime.Text, "Phasellus sit amet arcu")
}
func TestParseQuotedPrintableMime(t *testing.T) {
msg := readMessage("quoted-printable-mime.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Contains(t, mime.Text, "Nullam venenatis ante")
}
func TestParseInlineHtml(t *testing.T) {
msg := readMessage("html-mime-inline.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Contains(t, mime.Html, "<html>")
assert.Contains(t, mime.Html, "Test of HTML section")
}
func TestParseAttachment(t *testing.T) {
msg := readMessage("attachment.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Contains(t, mime.Text, "A text section")
assert.Equal(t, mime.Html, "", "Html attachment is not for display")
assert.Equal(t, len(mime.Inlines), 0, "Should have no inlines")
assert.Equal(t, len(mime.Attachments), 1, "Should have a single attachment")
assert.Equal(t, mime.Attachments[0].FileName(), "test.html", "Attachment should have correct filename")
assert.Contains(t, string(mime.Attachments[0].Content()), "<html>",
"Attachment should have correct content")
//for _, a := range mime.Attachments {
// fmt.Printf("%v %v\n", a.ContentType(), a.Disposition())
//}
}
func TestParseInline(t *testing.T) {
msg := readMessage("html-mime-inline.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Contains(t, mime.Text, "Test of text section", "Should have text section")
assert.Contains(t, mime.Html, ">Test of HTML section<", "Should have html section")
assert.Equal(t, len(mime.Inlines), 1, "Should have one inline")
assert.Equal(t, len(mime.Attachments), 0, "Should have no attachments")
assert.Equal(t, mime.Inlines[0].FileName(), "favicon.png", "Inline should have correct filename")
assert.True(t, bytes.HasPrefix(mime.Inlines[0].Content(), []byte{0x89, 'P', 'N', 'G'}),
"Content should be PNG image")
}
func TestParseNestedHeaders(t *testing.T) {
msg := readMessage("html-mime-inline.raw")
mime, err := ParseMIMEBody(msg)
if err != nil {
t.Fatalf("Failed to parse MIME: %v", err)
}
assert.Equal(t, len(mime.Inlines), 1, "Should have one inline")
assert.Equal(t, mime.Inlines[0].FileName(), "favicon.png", "Inline should have correct filename")
assert.Equal(t, mime.Inlines[0].Header().Get("Content-Id"), "<8B8481A2-25CA-4886-9B5A-8EB9115DD064@skynet>", "Inline should have a Content-Id header")
}
// readMessage is a test utility function to fetch a mail.Message object.
func readMessage(filename string) *mail.Message {
// Open test email for parsing
raw, err := os.Open(filepath.Join("test-data", "mail", filename))
if err != nil {
panic(fmt.Sprintf("Failed to open test data: %v", err))
}
// Parse email into a mail.Message object like we do
reader := bufio.NewReader(raw)
msg, err := mail.ReadMessage(reader)
if err != nil {
panic(fmt.Sprintf("Failed to read message: %v", err))
}
return msg
}