email_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Benchmark_base64Wrap(b *testing.B) {
  48. // Reasonable base case; 128K random bytes
  49. file := make([]byte, 128*1024)
  50. if _, err := rand.Read(file); err != nil {
  51. panic(err)
  52. }
  53. for i := 0; i <= b.N; i++ {
  54. base64Wrap(ioutil.Discard, file)
  55. }
  56. }