envelope_test.go 2.3 KB

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