envelope_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package mail
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "strings"
  6. "testing"
  7. )
  8. // Test MimeHeader decoding, not using iconv
  9. func TestMimeHeaderDecode(t *testing.T) {
  10. /*
  11. Normally this would fail if not using iconv
  12. str := MimeHeaderDecode("=?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=")
  13. if i := strings.Index(str, "【女子高生チャ"); i != 0 {
  14. t.Error("expecting 【女子高生チャ, got:", str)
  15. }
  16. */
  17. str := MimeHeaderDecode("=?utf-8?B?55So5oi34oCcRXBpZGVtaW9sb2d5IGluIG51cnNpbmcgYW5kIGg=?= =?utf-8?B?ZWFsdGggY2FyZSBlQm9vayByZWFkL2F1ZGlvIGlkOm8=?= =?utf-8?B?cTNqZWVr4oCd5Zyo572R56uZ4oCcU1BZ5Lit5paH5a6Y5pa5572R56uZ4oCd?= =?utf-8?B?55qE5biQ5Y+36K+m5oOF?=")
  18. if i := strings.Index(str, "用户“Epidemiology in nursing and h ealth care eBook read/audio id:o q3jeek”在网站“SPY中文官方网站” 的帐号详情"); i != 0 {
  19. t.Error("expecting 用户“Epidemiology in nursing and h ealth care eBook read/audio id:o q3jeek”在网站“SPY中文官方网站” 的帐号详情, got:", str)
  20. }
  21. str = MimeHeaderDecode("=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>")
  22. if strings.Index(str, "André Pirard") != 0 {
  23. t.Error("expecting André Pirard, got:", str)
  24. }
  25. }
  26. func TestNewAddress(t *testing.T) {
  27. addr, err := NewAddress("<hoop>")
  28. if err == nil {
  29. t.Error("there should be an error:", addr)
  30. }
  31. addr, err = NewAddress(`Gogh Fir <[email protected]>`)
  32. if err != nil {
  33. t.Error("there should be no error:", addr.Host, err)
  34. }
  35. }
  36. func TestEnvelope(t *testing.T) {
  37. e := NewEnvelope("127.0.0.1", 22)
  38. e.QueuedId = "abc123"
  39. e.Helo = "helo.example.com"
  40. e.MailFrom = Address{User: "test", Host: "example.com"}
  41. e.TLS = true
  42. e.RemoteIP = "222.111.233.121"
  43. to := Address{User: "test", Host: "example.com"}
  44. e.PushRcpt(to)
  45. if to.String() != "[email protected]" {
  46. t.Error("to does not equal [email protected], it was:", to.String())
  47. }
  48. e.Data.WriteString("Subject: Test\n\nThis is a test nbnb nbnb hgghgh nnnbnb nbnbnb nbnbn.")
  49. addHead := "Delivered-To: " + to.String() + "\n"
  50. addHead += "Received: from " + e.Helo + " (" + e.Helo + " [" + e.RemoteIP + "])\n"
  51. e.DeliveryHeader = addHead
  52. r := e.NewReader()
  53. data, _ := ioutil.ReadAll(r)
  54. if len(data) != e.Len() {
  55. t.Error("e.Len() is incorrect, it shown ", e.Len(), " but we wanted ", len(data))
  56. }
  57. if err := e.ParseHeaders(); err != nil && err != io.EOF {
  58. t.Error("cannot parse headers:", err)
  59. return
  60. }
  61. if e.Subject != "Test" {
  62. t.Error("Subject expecting: Test, got:", e.Subject)
  63. }
  64. }