encoding_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package encoding
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "strings"
  5. "testing"
  6. )
  7. // This will use the golang.org/x/net/html/charset encoder
  8. func TestEncodingMimeHeaderDecode(t *testing.T) {
  9. str := mail.MimeHeaderDecode("=?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=")
  10. if i := strings.Index(str, "【女子高生チャ"); i != 0 {
  11. t.Error("expecting 【女子高生チャ, got:", str)
  12. }
  13. str = mail.MimeHeaderDecode("=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>")
  14. if strings.Index(str, "André Pirard") != 0 {
  15. t.Error("expecting André Pirard, got:", str)
  16. }
  17. str = mail.MimeHeaderDecode("=?ISO-8859-1?Q?Andr=E9?=\tPirard <[email protected]>")
  18. if strings.Index(str, "André\tPirard") != 0 {
  19. t.Error("expecting André Pirard, got:", str)
  20. }
  21. }
  22. // TestEncodingMimeHeaderDecodeEnding tests when the encoded word is at the end
  23. func TestEncodingMimeHeaderDecodeEnding(t *testing.T) {
  24. // plaintext at the beginning
  25. str := mail.MimeHeaderDecode("What about this one? =?ISO-8859-1?Q?Andr=E9?=")
  26. if str != "What about this one? André" {
  27. t.Error("expecting: What about this one? André, but got:", str)
  28. }
  29. // not plaintext at beginning
  30. str = mail.MimeHeaderDecode("=?ISO-8859-1?Q?Andr=E9?= What about this one? =?ISO-8859-1?Q?Andr=E9?=")
  31. if str != "André What about this one? André" {
  32. t.Error("expecting: André What about this one? André, but got:", str)
  33. }
  34. // plaintext at beginning corruped
  35. str = mail.MimeHeaderDecode("=?ISO-8859-1?B?Andr=E9?= What about this one? =?ISO-8859-1?Q?Andr=E9?=")
  36. if strings.Index(str, "=?ISO-8859-1?B?Andr=E9?= What about this one? André") != 0 {
  37. t.Error("expecting:=?ISO-8859-1?B?Andr=E9?= What about this one? André, but got:", str)
  38. }
  39. }
  40. // TestEncodingMimeHeaderDecodeBad tests the case of a malformed encoding
  41. func TestEncodingMimeHeaderDecodeBad(t *testing.T) {
  42. // bad base64 encoding, it should return the string unencoded
  43. str := mail.MimeHeaderDecode("=?ISO-8859-1?B?Andr=E9?=\tPirard <[email protected]>")
  44. if strings.Index(str, "=?ISO-8859-1?B?Andr=E9?=\tPirard <[email protected]>") != 0 {
  45. t.Error("expecting =?ISO-8859-1?B?Andr=E9?=\tPirard <[email protected]>, got:", str)
  46. }
  47. }
  48. func TestEncodingMimeHeaderDecodeNoSpace(t *testing.T) {
  49. // there is no space
  50. str := mail.MimeHeaderDecode("A =?ISO-8859-1?Q?Andr=E9?=WORLD IN YOUR POCKET")
  51. if str != "A AndréWORLD IN YOUR POCKET" {
  52. // in this case, if it's QP and ?= is found at the end then we can assume no space?
  53. t.Error("Did not get [A AndréWORLD IN YOUR POCKET]")
  54. }
  55. }
  56. func TestEncodingMimeHeaderDecodeMulti(t *testing.T) {
  57. str := mail.MimeHeaderDecode("=?iso-2022-jp?B?GyRCIVpLXEZ8Om89fCFbPEIkT0lUOk5NUSROJU0lPyROSn0bKEI=?= =?iso-2022-jp?B?GyRCJCxCPyQkJEckORsoQg==?=")
  58. if strings.Index(str, "【本日削除】実は不採用のネタの方が多いです") != 0 {
  59. t.Error("expecting 【本日削除】実は不採用のネタの方が多いです, got:", str)
  60. }
  61. str = mail.MimeHeaderDecode("=?iso-2022-jp?B?GyRCIVpLXEZ8Om89fCFbPEIkT0lUOk5NUSROJU0lPyROSn0bKEI=?= \t =?iso-2022-jp?B?GyRCJCxCPyQkJEckORsoQg==?=")
  62. if strings.Index(str, "【本日削除】実は不採用のネタの方が多いです") != 0 {
  63. t.Error("expecting 【本日削除】実は不採用のネタの方が多いです, got:", str)
  64. }
  65. }