mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 12:46:46 +00:00
GODT-1779: Remove go-imap
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
@ -33,129 +34,129 @@ import (
|
||||
func TestParseLongHeaderLine(t *testing.T) {
|
||||
f := getFileReader("long_header_line.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseLongHeaderLineMultiline(t *testing.T) {
|
||||
f := getFileReader("long_header_line_multiline.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlain(t *testing.T) {
|
||||
f := getFileReader("text_plain.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainUTF8(t *testing.T) {
|
||||
f := getFileReader("text_plain_utf8.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainLatin1(t *testing.T) {
|
||||
f := getFileReader("text_plain_latin1.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "ééééééé", m.Body)
|
||||
assert.Equal(t, "ééééééé", plainBody)
|
||||
assert.Equal(t, "ééééééé", string(m.RichBody))
|
||||
assert.Equal(t, "ééééééé", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainUTF8Subject(t *testing.T) {
|
||||
f := getFileReader("text_plain_utf8_subject.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
assert.Equal(t, `汉字汉字汉`, m.Subject)
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainLatin2Subject(t *testing.T) {
|
||||
f := getFileReader("text_plain_latin2_subject.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
assert.Equal(t, `If you can read this you understand the example.`, m.Subject)
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainUnknownCharsetIsActuallyLatin1(t *testing.T) {
|
||||
f := getFileReader("text_plain_unknown_latin1.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "ééééééé", m.Body)
|
||||
assert.Equal(t, "ééééééé", plainBody)
|
||||
assert.Equal(t, "ééééééé", string(m.RichBody))
|
||||
assert.Equal(t, "ééééééé", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainUnknownCharsetIsActuallyLatin2(t *testing.T) {
|
||||
f := getFileReader("text_plain_unknown_latin2.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
@ -167,97 +168,97 @@ func TestParseTextPlainUnknownCharsetIsActuallyLatin2(t *testing.T) {
|
||||
expect, _ := charmap.ISO8859_1.NewDecoder().Bytes(latin2)
|
||||
assert.NotEqual(t, []byte("řšřšřš"), expect)
|
||||
|
||||
assert.Equal(t, string(expect), m.Body)
|
||||
assert.Equal(t, string(expect), plainBody)
|
||||
assert.Equal(t, string(expect), string(m.RichBody))
|
||||
assert.Equal(t, string(expect), string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainAlready7Bit(t *testing.T) {
|
||||
f := getFileReader("text_plain_7bit.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainWithOctetAttachment(t *testing.T) {
|
||||
f := getFileReader("text_plain_octet_attachment.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
require.Len(t, attReaders, 1)
|
||||
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||
require.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, string(m.Attachments[0].Data), "if you are reading this, hi!")
|
||||
}
|
||||
|
||||
func TestParseTextPlainWithOctetAttachmentGoodFilename(t *testing.T) {
|
||||
f := getFileReader("text_plain_octet_attachment_good_2231_filename.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 1)
|
||||
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||
assert.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, string(m.Attachments[0].Data), "if you are reading this, hi!")
|
||||
assert.Equal(t, "😁😂.txt", m.Attachments[0].Name)
|
||||
}
|
||||
|
||||
func TestParseTextPlainWithRFC822Attachment(t *testing.T) {
|
||||
f := getFileReader("text_plain_rfc822_attachment.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 1)
|
||||
assert.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, "message.eml", m.Attachments[0].Name)
|
||||
}
|
||||
|
||||
func TestParseTextPlainWithOctetAttachmentBadFilename(t *testing.T) {
|
||||
f := getFileReader("text_plain_octet_attachment_bad_2231_filename.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 1)
|
||||
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||
assert.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, string(m.Attachments[0].Data), "if you are reading this, hi!")
|
||||
assert.Equal(t, "attachment.bin", m.Attachments[0].Name)
|
||||
}
|
||||
|
||||
func TestParseTextPlainWithOctetAttachmentNameInContentType(t *testing.T) {
|
||||
f := getFileReader("text_plain_octet_attachment_name_in_contenttype.eml")
|
||||
|
||||
m, _, _, _, err := Parse(f) //nolint:dogsled
|
||||
m, err := Parse(f) //nolint:dogsled
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "attachment-contenttype.txt", m.Attachments[0].Name)
|
||||
@ -266,7 +267,7 @@ func TestParseTextPlainWithOctetAttachmentNameInContentType(t *testing.T) {
|
||||
func TestParseTextPlainWithOctetAttachmentNameConflict(t *testing.T) {
|
||||
f := getFileReader("text_plain_octet_attachment_name_conflict.eml")
|
||||
|
||||
m, _, _, _, err := Parse(f) //nolint:dogsled
|
||||
m, err := Parse(f) //nolint:dogsled
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "attachment-disposition.txt", m.Attachments[0].Name)
|
||||
@ -275,49 +276,49 @@ func TestParseTextPlainWithOctetAttachmentNameConflict(t *testing.T) {
|
||||
func TestParseTextPlainWithPlainAttachment(t *testing.T) {
|
||||
f := getFileReader("text_plain_plain_attachment.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
require.Len(t, attReaders, 1)
|
||||
assert.Equal(t, readerToString(attReaders[0]), "attachment")
|
||||
require.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, string(m.Attachments[0].Data), "attachment")
|
||||
}
|
||||
|
||||
func TestParseTextPlainEmptyAddresses(t *testing.T) {
|
||||
f := getFileReader("text_plain_empty_addresses.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextPlainWithImageInline(t *testing.T) {
|
||||
f := getFileReader("text_plain_image_inline.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
// The inline image is an 8x8 mic-dropping gopher.
|
||||
require.Len(t, attReaders, 1)
|
||||
img, err := png.DecodeConfig(attReaders[0])
|
||||
require.Len(t, m.Attachments, 1)
|
||||
img, err := png.DecodeConfig(bytes.NewReader(m.Attachments[0].Data))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 8, img.Width)
|
||||
assert.Equal(t, 8, img.Height)
|
||||
@ -326,111 +327,111 @@ func TestParseTextPlainWithImageInline(t *testing.T) {
|
||||
func TestParseTextPlainWithDuplicateCharset(t *testing.T) {
|
||||
f := getFileReader("text_plain_duplicate_charset.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseWithMultipleTextParts(t *testing.T) {
|
||||
f := getFileReader("multiple_text_parts.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body\nsome other part of the message", m.Body)
|
||||
assert.Equal(t, "body\nsome other part of the message", plainBody)
|
||||
assert.Equal(t, "body\nsome other part of the message", string(m.RichBody))
|
||||
assert.Equal(t, "body\nsome other part of the message", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextHTML(t *testing.T) {
|
||||
f := getFileReader("text_html.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", m.Body)
|
||||
assert.Equal(t, "This is body of *HTML mail* without attachment", plainBody)
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", string(m.RichBody))
|
||||
assert.Equal(t, "This is body of *HTML mail* without attachment", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextHTMLAlready7Bit(t *testing.T) {
|
||||
f := getFileReader("text_html_7bit.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", m.Body)
|
||||
assert.Equal(t, "This is body of *HTML mail* without attachment", plainBody)
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", string(m.RichBody))
|
||||
assert.Equal(t, "This is body of *HTML mail* without attachment", string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextHTMLWithOctetAttachment(t *testing.T) {
|
||||
f := getFileReader("text_html_octet_attachment.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
||||
assert.Equal(t, "This is body of *HTML mail* with attachment", plainBody)
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", string(m.RichBody))
|
||||
assert.Equal(t, "This is body of *HTML mail* with attachment", string(m.PlainBody))
|
||||
|
||||
require.Len(t, attReaders, 1)
|
||||
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||
require.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, string(m.Attachments[0].Data), "if you are reading this, hi!")
|
||||
}
|
||||
|
||||
func TestParseTextHTMLWithPlainAttachment(t *testing.T) {
|
||||
f := getFileReader("text_html_plain_attachment.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
// BAD: plainBody should not be empty!
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
||||
assert.Equal(t, "This is body of *HTML mail* with attachment", plainBody)
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", string(m.RichBody))
|
||||
assert.Equal(t, "This is body of *HTML mail* with attachment", string(m.PlainBody))
|
||||
|
||||
require.Len(t, attReaders, 1)
|
||||
assert.Equal(t, readerToString(attReaders[0]), "attachment")
|
||||
require.Len(t, m.Attachments, 1)
|
||||
assert.Equal(t, string(m.Attachments[0].Data), "attachment")
|
||||
}
|
||||
|
||||
func TestParseTextHTMLWithImageInline(t *testing.T) {
|
||||
f := getFileReader("text_html_image_inline.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
||||
assert.Equal(t, "This is body of *HTML mail* with attachment", plainBody)
|
||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", string(m.RichBody))
|
||||
assert.Equal(t, "This is body of *HTML mail* with attachment", string(m.PlainBody))
|
||||
|
||||
// The inline image is an 8x8 mic-dropping gopher.
|
||||
require.Len(t, attReaders, 1)
|
||||
img, err := png.DecodeConfig(attReaders[0])
|
||||
require.Len(t, m.Attachments, 1)
|
||||
img, err := png.DecodeConfig(bytes.NewReader(m.Attachments[0].Data))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 8, img.Width)
|
||||
assert.Equal(t, 8, img.Height)
|
||||
@ -441,40 +442,42 @@ func TestParseWithAttachedPublicKey(t *testing.T) {
|
||||
|
||||
p, err := parser.New(f)
|
||||
require.NoError(t, err)
|
||||
m, plainBody, attReaders, err := ParserWithParser(p)
|
||||
AttachPublicKey(p, "publickey", "publickeyname")
|
||||
|
||||
m, err := ParseWithParser(p)
|
||||
require.NoError(t, err)
|
||||
|
||||
p.AttachPublicKey("publickey", "publickeyname")
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "body", m.Body)
|
||||
assert.Equal(t, "body", plainBody)
|
||||
assert.Equal(t, "body", string(m.RichBody))
|
||||
assert.Equal(t, "body", string(m.PlainBody))
|
||||
|
||||
// The pubkey should not be collected as an attachment.
|
||||
// We upload the pubkey when creating the draft.
|
||||
require.Len(t, attReaders, 0)
|
||||
require.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseTextHTMLWithEmbeddedForeignEncoding(t *testing.T) {
|
||||
f := getFileReader("text_html_embedded_foreign_encoding.eml")
|
||||
|
||||
m, _, plainBody, attReaders, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, `<html><head><meta charset="UTF-8"/></head><body>latin2 řšřš</body></html>`, m.Body)
|
||||
assert.Equal(t, `latin2 řšřš`, plainBody)
|
||||
assert.Equal(t, `<html><head><meta charset="UTF-8"/></head><body>latin2 řšřš</body></html>`, string(m.RichBody))
|
||||
assert.Equal(t, `latin2 řšřš`, string(m.PlainBody))
|
||||
|
||||
assert.Len(t, attReaders, 0)
|
||||
assert.Len(t, m.Attachments, 0)
|
||||
}
|
||||
|
||||
func TestParseMultipartAlternative(t *testing.T) {
|
||||
f := getFileReader("multipart_alternative.eml")
|
||||
|
||||
m, _, plainBody, _, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"schizofrenic" <schizofrenic@pm.me>`, m.Sender.String())
|
||||
@ -487,15 +490,15 @@ func TestParseMultipartAlternative(t *testing.T) {
|
||||
<b>aoeuaoeu</b>
|
||||
|
||||
|
||||
</body></html>`, m.Body)
|
||||
</body></html>`, string(m.RichBody))
|
||||
|
||||
assert.Equal(t, "*aoeuaoeu*\n\n", plainBody)
|
||||
assert.Equal(t, "*aoeuaoeu*\n\n", string(m.PlainBody))
|
||||
}
|
||||
|
||||
func TestParseMultipartAlternativeNested(t *testing.T) {
|
||||
f := getFileReader("multipart_alternative_nested.eml")
|
||||
|
||||
m, _, plainBody, _, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"schizofrenic" <schizofrenic@pm.me>`, m.Sender.String())
|
||||
@ -508,15 +511,15 @@ func TestParseMultipartAlternativeNested(t *testing.T) {
|
||||
<b>multipart 2.2</b>
|
||||
|
||||
|
||||
</body></html>`, m.Body)
|
||||
</body></html>`, string(m.RichBody))
|
||||
|
||||
assert.Equal(t, "*multipart 2.1*\n\n", plainBody)
|
||||
assert.Equal(t, "*multipart 2.1*\n\n", string(m.PlainBody))
|
||||
}
|
||||
|
||||
func TestParseMultipartAlternativeLatin1(t *testing.T) {
|
||||
f := getFileReader("multipart_alternative_latin1.eml")
|
||||
|
||||
m, _, plainBody, _, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"schizofrenic" <schizofrenic@pm.me>`, m.Sender.String())
|
||||
@ -529,52 +532,52 @@ func TestParseMultipartAlternativeLatin1(t *testing.T) {
|
||||
<b>aoeuaoeu</b>
|
||||
|
||||
|
||||
</body></html>`, m.Body)
|
||||
</body></html>`, string(m.RichBody))
|
||||
|
||||
assert.Equal(t, "*aoeuaoeu*\n\n", plainBody)
|
||||
assert.Equal(t, "*aoeuaoeu*\n\n", string(m.PlainBody))
|
||||
}
|
||||
|
||||
func TestParseWithTrailingEndOfMailIndicator(t *testing.T) {
|
||||
f := getFileReader("text_html_trailing_end_of_mail.eml")
|
||||
|
||||
m, _, plainBody, _, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@sender.com>`, m.Sender.String())
|
||||
assert.Equal(t, `"Receiver" <receiver@receiver.com>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "<!DOCTYPE html><html><head></head><body>boo!</body></html>", m.Body)
|
||||
assert.Equal(t, "boo!", plainBody)
|
||||
assert.Equal(t, "<!DOCTYPE html><html><head></head><body>boo!</body></html>", string(m.RichBody))
|
||||
assert.Equal(t, "boo!", string(m.PlainBody))
|
||||
}
|
||||
|
||||
func TestParseEncodedContentType(t *testing.T) {
|
||||
f := getFileReader("rfc2047-content-transfer-encoding.eml")
|
||||
|
||||
m, _, plainBody, _, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@sender.com>`, m.Sender.String())
|
||||
assert.Equal(t, `<user@somewhere.org>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "bodybodybody\n", plainBody)
|
||||
assert.Equal(t, "bodybodybody\n", string(m.PlainBody))
|
||||
}
|
||||
|
||||
func TestParseNonEncodedContentType(t *testing.T) {
|
||||
f := getFileReader("non-encoded-content-transfer-encoding.eml")
|
||||
|
||||
m, _, plainBody, _, err := Parse(f)
|
||||
m, err := Parse(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, `"Sender" <sender@sender.com>`, m.Sender.String())
|
||||
assert.Equal(t, `<user@somewhere.org>`, m.ToList[0].String())
|
||||
|
||||
assert.Equal(t, "bodybodybody\n", plainBody)
|
||||
assert.Equal(t, "bodybodybody\n", string(m.PlainBody))
|
||||
}
|
||||
|
||||
func TestParseEncodedContentTypeBad(t *testing.T) {
|
||||
f := getFileReader("rfc2047-content-transfer-encoding-bad.eml")
|
||||
|
||||
_, _, _, _, err := Parse(f) //nolint:dogsled
|
||||
_, err := Parse(f) //nolint:dogsled
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
@ -587,7 +590,7 @@ func (panicReader) Read(p []byte) (int, error) {
|
||||
func TestParsePanic(t *testing.T) {
|
||||
var err error
|
||||
require.NotPanics(t, func() {
|
||||
_, _, _, _, err = Parse(&panicReader{})
|
||||
_, err = Parse(&panicReader{})
|
||||
})
|
||||
require.Error(t, err)
|
||||
}
|
||||
@ -600,12 +603,3 @@ func getFileReader(filename string) io.Reader {
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func readerToString(r io.Reader) string {
|
||||
b, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user