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
|
package backendutil
import (
"bufio"
"io/ioutil"
"strings"
"testing"
"github.com/emersion/go-imap"
"github.com/emersion/go-message/textproto"
)
var bodyTests = []struct {
section string
body string
}{
{
section: "BODY[]",
body: testMailString,
},
{
section: "BODY[1.1]",
body: testTextBodyString,
},
{
section: "BODY[1.2]",
body: testHTMLBodyString,
},
{
section: "BODY[2]",
body: testAttachmentBodyString,
},
{
section: "BODY[HEADER]",
body: testHeaderString,
},
{
section: "BODY[HEADER.FIELDS (From To)]",
body: testHeaderFromToString,
},
{
section: "BODY[HEADER.FIELDS (FROM to)]",
body: testHeaderFromToString,
},
{
section: "BODY[HEADER.FIELDS.NOT (From To)]",
body: testHeaderNoFromToString,
},
{
section: "BODY[HEADER.FIELDS (Date)]",
body: testHeaderDateString,
},
{
section: "BODY[1.1.HEADER]",
body: testTextHeaderString,
},
{
section: "BODY[1.1.HEADER.FIELDS (Content-Type)]",
body: testTextContentTypeString,
},
{
section: "BODY[1.1.HEADER.FIELDS.NOT (Content-Type)]",
body: testTextNoContentTypeString,
},
{
section: "BODY[2.HEADER]",
body: testAttachmentHeaderString,
},
{
section: "BODY[2.MIME]",
body: testAttachmentHeaderString,
},
{
section: "BODY[TEXT]",
body: testBodyString,
},
{
section: "BODY[1.1.TEXT]",
body: testTextBodyString,
},
{
section: "BODY[2.TEXT]",
body: testAttachmentBodyString,
},
{
section: "BODY[2.1]",
body: "",
},
{
section: "BODY[3]",
body: "",
},
{
section: "BODY[2.TEXT]<0.9>",
body: testAttachmentBodyString[:9],
},
}
func TestFetchBodySection(t *testing.T) {
for _, test := range bodyTests {
test := test
t.Run(test.section, func(t *testing.T) {
bufferedBody := bufio.NewReader(strings.NewReader(testMailString))
header, err := textproto.ReadHeader(bufferedBody)
if err != nil {
t.Fatal("Expected no error while reading mail, got:", err)
}
section, err := imap.ParseBodySectionName(imap.FetchItem(test.section))
if err != nil {
t.Fatal("Expected no error while parsing body section name, got:", err)
}
r, err := FetchBodySection(header, bufferedBody, section)
if test.body == "" {
if err == nil {
t.Error("Expected an error while extracting non-existing body section")
}
} else {
if err != nil {
t.Fatal("Expected no error while extracting body section, got:", err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal("Expected no error while reading body section, got:", err)
}
if s := string(b); s != test.body {
t.Errorf("Expected body section %q to be \n%s\n but got \n%s", test.section, test.body, s)
}
}
})
}
}
func TestFetchBodySection_NonMultipart(t *testing.T) {
// https://tools.ietf.org/html/rfc3501#page-55:
// Every message has at least one part number. Non-[MIME-IMB]
// messages, and non-multipart [MIME-IMB] messages with no
// encapsulated message, only have a part 1.
testMsgHdr := "From: Mitsuha Miyamizu <mitsuha.miyamizu@example.org>\r\n" +
"To: Taki Tachibana <taki.tachibana@example.org>\r\n" +
"Subject: Your Name.\r\n" +
"Message-Id: 42@example.org\r\n" +
"\r\n"
testMsgBody := "That's not multipart message. Thought it should be possible to get this text using BODY[1]."
testMsg := testMsgHdr + testMsgBody
tests := []struct {
section string
body string
}{
{
section: "BODY[1.MIME]",
body: testMsgHdr,
},
{
section: "BODY[1]",
body: testMsgBody,
},
}
for _, test := range tests {
test := test
t.Run(test.section, func(t *testing.T) {
bufferedBody := bufio.NewReader(strings.NewReader(testMsg))
header, err := textproto.ReadHeader(bufferedBody)
if err != nil {
t.Fatal("Expected no error while reading mail, got:", err)
}
section, err := imap.ParseBodySectionName(imap.FetchItem(test.section))
if err != nil {
t.Fatal("Expected no error while parsing body section name, got:", err)
}
r, err := FetchBodySection(header, bufferedBody, section)
if err != nil {
t.Fatal("Expected no error while extracting body section, got:", err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal("Expected no error while reading body section, got:", err)
}
if s := string(b); s != test.body {
t.Errorf("Expected body section %q to be \n%s\n but got \n%s", test.section, test.body, s)
}
})
}
}
|