150 lines
3.9 KiB
Go
150 lines
3.9 KiB
Go
package googleapi
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAttachmentDumpNormalWithFilename(t *testing.T) {
|
|
a := MailAttachment{
|
|
IsInline: false,
|
|
ContentType: "text/plain",
|
|
Filename: "hello.txt",
|
|
Data: []byte("HelloWorld"),
|
|
}
|
|
|
|
lines := a.dump()
|
|
joined := strings.Join(lines, "\n")
|
|
|
|
tst.AssertTrue(t, strings.Contains(joined, "Content-Type: text/plain; charset=UTF-8"))
|
|
tst.AssertTrue(t, strings.Contains(joined, "Content-Transfer-Encoding: base64"))
|
|
tst.AssertTrue(t, strings.Contains(joined, `Content-Disposition: attachment;filename="hello.txt"`))
|
|
tst.AssertFalse(t, strings.Contains(joined, "Content-Disposition: inline"))
|
|
|
|
expectedB64 := base64.StdEncoding.EncodeToString([]byte("HelloWorld"))
|
|
tst.AssertTrue(t, strings.Contains(joined, expectedB64))
|
|
}
|
|
|
|
func TestAttachmentDumpInlineWithFilename(t *testing.T) {
|
|
a := MailAttachment{
|
|
IsInline: true,
|
|
ContentType: "image/png",
|
|
Filename: "img.png",
|
|
Data: []byte{0x01, 0x02, 0x03, 0x04},
|
|
}
|
|
|
|
lines := a.dump()
|
|
joined := strings.Join(lines, "\n")
|
|
|
|
tst.AssertTrue(t, strings.Contains(joined, "Content-Type: image/png; charset=UTF-8"))
|
|
tst.AssertTrue(t, strings.Contains(joined, "Content-Transfer-Encoding: base64"))
|
|
tst.AssertTrue(t, strings.Contains(joined, `Content-Disposition: inline;filename="img.png"`))
|
|
}
|
|
|
|
func TestAttachmentDumpNormalNoFilename(t *testing.T) {
|
|
a := MailAttachment{
|
|
IsInline: false,
|
|
ContentType: "text/plain",
|
|
Filename: "",
|
|
Data: []byte("foo"),
|
|
}
|
|
|
|
lines := a.dump()
|
|
joined := strings.Join(lines, "\n")
|
|
|
|
tst.AssertTrue(t, langext.InArray("Content-Disposition: attachment", lines))
|
|
tst.AssertFalse(t, strings.Contains(joined, "filename="))
|
|
}
|
|
|
|
func TestAttachmentDumpInlineNoFilename(t *testing.T) {
|
|
a := MailAttachment{
|
|
IsInline: true,
|
|
ContentType: "text/plain",
|
|
Filename: "",
|
|
Data: []byte("foo"),
|
|
}
|
|
|
|
lines := a.dump()
|
|
|
|
tst.AssertTrue(t, langext.InArray("Content-Disposition: inline", lines))
|
|
}
|
|
|
|
func TestAttachmentDumpNoContentType(t *testing.T) {
|
|
a := MailAttachment{
|
|
IsInline: false,
|
|
ContentType: "",
|
|
Filename: "x.bin",
|
|
Data: []byte("x"),
|
|
}
|
|
|
|
lines := a.dump()
|
|
|
|
for _, l := range lines {
|
|
tst.AssertFalse(t, strings.HasPrefix(l, "Content-Type:"))
|
|
}
|
|
tst.AssertTrue(t, langext.InArray("Content-Transfer-Encoding: base64", lines))
|
|
}
|
|
|
|
func TestAttachmentDumpEmptyData(t *testing.T) {
|
|
a := MailAttachment{
|
|
IsInline: false,
|
|
ContentType: "application/octet-stream",
|
|
Filename: "empty.bin",
|
|
Data: []byte{},
|
|
}
|
|
|
|
lines := a.dump()
|
|
joined := strings.Join(lines, "\n")
|
|
|
|
tst.AssertTrue(t, strings.Contains(joined, "Content-Type: application/octet-stream; charset=UTF-8"))
|
|
tst.AssertTrue(t, strings.Contains(joined, "Content-Transfer-Encoding: base64"))
|
|
}
|
|
|
|
func TestAttachmentDumpLongDataLineWrapped(t *testing.T) {
|
|
// Data needs to result in > 80 base64 chars to test the wrapping.
|
|
// 100 bytes => 136 base64 chars => should wrap into 2 lines (80 + 56).
|
|
data := make([]byte, 100)
|
|
for i := range data {
|
|
data[i] = byte(i)
|
|
}
|
|
|
|
a := MailAttachment{
|
|
IsInline: false,
|
|
ContentType: "application/octet-stream",
|
|
Filename: "big.bin",
|
|
Data: data,
|
|
}
|
|
|
|
lines := a.dump()
|
|
|
|
// Find the base64 lines (everything after the headers).
|
|
b64Lines := make([]string, 0)
|
|
foundFirstHeader := false
|
|
for _, l := range lines {
|
|
if !foundFirstHeader && (strings.HasPrefix(l, "Content-") || l == "") {
|
|
foundFirstHeader = true
|
|
continue
|
|
}
|
|
if strings.HasPrefix(l, "Content-") {
|
|
continue
|
|
}
|
|
b64Lines = append(b64Lines, l)
|
|
}
|
|
|
|
full := strings.Join(b64Lines, "")
|
|
expected := base64.StdEncoding.EncodeToString(data)
|
|
tst.AssertEqual(t, full, expected)
|
|
|
|
// Each line (except possibly last) should be 80 chars.
|
|
for i, l := range b64Lines {
|
|
if i < len(b64Lines)-1 {
|
|
tst.AssertEqual(t, len(l), 80)
|
|
} else {
|
|
tst.AssertTrue(t, len(l) <= 80)
|
|
}
|
|
}
|
|
}
|