123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package email
- import (
- "net/smtp"
- "testing"
- "bytes"
- "crypto/rand"
- "io/ioutil"
- )
- func TestEmail(*testing.T) {
- e := NewEmail()
- e.From = "Jordan Wright <test@example.com>"
- e.To = []string{"test@example.com"}
- e.Bcc = []string{"test_bcc@example.com"}
- e.Cc = []string{"test_cc@example.com"}
- e.Subject = "Awesome Subject"
- e.Text = "Text Body is, of course, supported!"
- e.HTML = "<h1>Fancy Html is supported, too!</h1>"
- }
- func ExampleGmail() {
- e := NewEmail()
- e.From = "Jordan Wright <test@gmail.com>"
- e.To = []string{"test@example.com"}
- e.Bcc = []string{"test_bcc@example.com"}
- e.Cc = []string{"test_cc@example.com"}
- e.Subject = "Awesome Subject"
- e.Text = "Text Body is, of course, supported!"
- e.HTML = "<h1>Fancy Html is supported, too!</h1>"
- e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
- }
- func ExampleAttach() {
- e := NewEmail()
- e.AttachFile("test.txt")
- }
- func Test_base64Wrap(t *testing.T) {
- file := "I'm a file long enough to force the function to wrap a\n" +
- "couple of lines, but I stop short of the end of one line and\n" +
- "have some padding dangling at the end."
- encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
- "dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
- "ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
- var buf bytes.Buffer
- base64Wrap(&buf, []byte(file))
- if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
- t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
- }
- }
- func Test_quotedPrintEncode(t *testing.T) {
- var buf bytes.Buffer
- text := "Dear reader!\n\n" +
- "This is a test email to try and capture some of the corner cases that exist within\n" +
- "the quoted-printable encoding.\n" +
- "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n"
- expected := "Dear reader!\r\n\r\n" +
- "This is a test email to try and capture some of the corner cases that exist=\r\n" +
- " within\r\n" +
- "the quoted-printable encoding.\r\n" +
- "There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
- "s so=0D\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
- " a fish: =F0=9F=90=9F\r\n"
- if err := quotePrintEncode(&buf, text); err != nil {
- t.Fatal("quotePrintEncode: ", err)
- }
- if s := buf.String(); s != expected {
- t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", s, expected)
- }
- }
- func Benchmark_quotedPrintEncode(b *testing.B) {
- text := "Dear reader!\n\n" +
- "This is a test email to try and capture some of the corner cases that exist within\n" +
- "the quoted-printable encoding.\n" +
- "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n"
- for i := 0; i <= b.N; i++ {
- if err := quotePrintEncode(ioutil.Discard, text); err != nil {
- panic(err)
- }
- }
- }
- func Benchmark_base64Wrap(b *testing.B) {
- // Reasonable base case; 128K random bytes
- file := make([]byte, 128*1024)
- if _, err := rand.Read(file); err != nil {
- panic(err)
- }
- for i := 0; i <= b.N; i++ {
- base64Wrap(ioutil.Discard, file)
- }
- }
|