email_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package email
  2. import (
  3. "net/smtp"
  4. "testing"
  5. "bytes"
  6. "crypto/rand"
  7. "io/ioutil"
  8. )
  9. func TestEmail(*testing.T) {
  10. e := NewEmail()
  11. e.From = "Jordan Wright <test@example.com>"
  12. e.To = []string{"test@example.com"}
  13. e.Bcc = []string{"test_bcc@example.com"}
  14. e.Cc = []string{"test_cc@example.com"}
  15. e.Subject = "Awesome Subject"
  16. e.Text = "Text Body is, of course, supported!"
  17. e.HTML = "<h1>Fancy Html is supported, too!</h1>"
  18. }
  19. func ExampleGmail() {
  20. e := NewEmail()
  21. e.From = "Jordan Wright <test@gmail.com>"
  22. e.To = []string{"test@example.com"}
  23. e.Bcc = []string{"test_bcc@example.com"}
  24. e.Cc = []string{"test_cc@example.com"}
  25. e.Subject = "Awesome Subject"
  26. e.Text = "Text Body is, of course, supported!"
  27. e.HTML = "<h1>Fancy Html is supported, too!</h1>"
  28. e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
  29. }
  30. func ExampleAttach() {
  31. e := NewEmail()
  32. e.AttachFile("test.txt")
  33. }
  34. func Test_base64Wrap(t *testing.T) {
  35. file := "I'm a file long enough to force the function to wrap a\n" +
  36. "couple of lines, but I stop short of the end of one line and\n" +
  37. "have some padding dangling at the end."
  38. encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
  39. "dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
  40. "ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
  41. var buf bytes.Buffer
  42. base64Wrap(&buf, []byte(file))
  43. if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
  44. t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
  45. }
  46. }
  47. func Test_quotedPrintEncode(t *testing.T) {
  48. var buf bytes.Buffer
  49. text := "Dear reader!\n\n" +
  50. "This is a test email to try and capture some of the corner cases that exist within\n" +
  51. "the quoted-printable encoding.\n" +
  52. "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
  53. "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n"
  54. expected := "Dear reader!\r\n\r\n" +
  55. "This is a test email to try and capture some of the corner cases that exist=\r\n" +
  56. " within\r\n" +
  57. "the quoted-printable encoding.\r\n" +
  58. "There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
  59. "s so=0D\r\n" +
  60. "it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
  61. " a fish: =F0=9F=90=9F\r\n"
  62. if err := quotePrintEncode(&buf, text); err != nil {
  63. t.Fatal("quotePrintEncode: ", err)
  64. }
  65. if s := buf.String(); s != expected {
  66. t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", s, expected)
  67. }
  68. }
  69. func Benchmark_quotedPrintEncode(b *testing.B) {
  70. text := "Dear reader!\n\n" +
  71. "This is a test email to try and capture some of the corner cases that exist within\n" +
  72. "the quoted-printable encoding.\n" +
  73. "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
  74. "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n"
  75. for i := 0; i <= b.N; i++ {
  76. if err := quotePrintEncode(ioutil.Discard, text); err != nil {
  77. panic(err)
  78. }
  79. }
  80. }
  81. func Benchmark_base64Wrap(b *testing.B) {
  82. // Reasonable base case; 128K random bytes
  83. file := make([]byte, 128*1024)
  84. if _, err := rand.Read(file); err != nil {
  85. panic(err)
  86. }
  87. for i := 0; i <= b.N; i++ {
  88. base64Wrap(ioutil.Discard, file)
  89. }
  90. }