envelope_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package mail
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. "testing"
  6. )
  7. func TestMimeHeaderDecode(t *testing.T) {
  8. str := MimeHeaderDecode("=?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=")
  9. if i := strings.Index(str, "【女子高生チャ"); i != 0 {
  10. t.Error("expecting 【女子高生チャ, got:", str)
  11. }
  12. str = MimeHeaderDecode("=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>")
  13. if strings.Index(str, "André Pirard") != 0 {
  14. t.Error("expecting André Pirard, got:", str)
  15. }
  16. }
  17. func TestNewAddress(t *testing.T) {
  18. addr, err := NewAddress("<hoop>")
  19. if err == nil {
  20. t.Error("there should be an error:", addr)
  21. }
  22. addr, err = NewAddress(`Gogh Fir <[email protected]>`)
  23. if err != nil {
  24. t.Error("there should be no error:", addr.Host, err)
  25. }
  26. }
  27. func TestEnvelope(t *testing.T) {
  28. e := NewEnvelope("127.0.0.1", 22)
  29. e.QueuedId = "abc123"
  30. e.Helo = "helo.example.com"
  31. e.MailFrom = Address{User: "test", Host: "example.com"}
  32. e.TLS = true
  33. e.RemoteIP = "222.111.233.121"
  34. to := Address{User: "test", Host: "example.com"}
  35. e.PushRcpt(to)
  36. if to.String() != "[email protected]" {
  37. t.Error("to does not equal [email protected], it was:", to.String())
  38. }
  39. e.Data.WriteString("Subject: Test\n\nThis is a test nbnb nbnb hgghgh nnnbnb nbnbnb nbnbn.")
  40. addHead := "Delivered-To: " + to.String() + "\n"
  41. addHead += "Received: from " + e.Helo + " (" + e.Helo + " [" + e.RemoteIP + "])\n"
  42. e.DeliveryHeader = addHead
  43. r := e.NewReader()
  44. data, _ := ioutil.ReadAll(r)
  45. if len(data) != e.Len() {
  46. t.Error("e.Len() is inccorrect, it shown ", e.Len(), " but we wanted ", len(data))
  47. }
  48. e.ParseHeaders()
  49. if e.Subject != "Test" {
  50. t.Error("Subject expecting: Test, got:", e.Subject)
  51. }
  52. }