1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 func TestEncodeWord(t
*testing
.T
) {
16 utf8
, iso88591
:= "utf-8", "iso-8859-1"
22 {QEncoding
, utf8
, "François-Jérôme", "=?utf-8?q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?="},
23 {BEncoding
, utf8
, "Café", "=?utf-8?b?Q2Fmw6k=?="},
24 {QEncoding
, iso88591
, "La Seleção", "=?iso-8859-1?q?La_Sele=C3=A7=C3=A3o?="},
25 {QEncoding
, utf8
, "", ""},
26 {QEncoding
, utf8
, "A", "A"},
27 {QEncoding
, iso88591
, "a", "a"},
28 {QEncoding
, utf8
, "123 456", "123 456"},
29 {QEncoding
, utf8
, "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~", "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~"},
30 {QEncoding
, utf8
, strings
.Repeat("é", 10), "=?utf-8?q?" + strings
.Repeat("=C3=A9", 10) + "?="},
31 {QEncoding
, utf8
, strings
.Repeat("é", 11), "=?utf-8?q?" + strings
.Repeat("=C3=A9", 10) + "?= =?utf-8?q?=C3=A9?="},
32 {QEncoding
, iso88591
, strings
.Repeat("\xe9", 22), "=?iso-8859-1?q?" + strings
.Repeat("=E9", 22) + "?="},
33 {QEncoding
, utf8
, strings
.Repeat("\x80", 22), "=?utf-8?q?" + strings
.Repeat("=80", 21) + "?= =?utf-8?q?=80?="},
34 {BEncoding
, iso88591
, strings
.Repeat("\xe9", 45), "=?iso-8859-1?b?" + strings
.Repeat("6enp", 15) + "?="},
35 {BEncoding
, utf8
, strings
.Repeat("\x80", 48), "=?utf-8?b?" + strings
.Repeat("gICA", 15) + "?= =?utf-8?b?gICA?="},
38 for _
, test
:= range tests
{
39 if s
:= test
.enc
.Encode(test
.charset
, test
.src
); s
!= test
.exp
{
40 t
.Errorf("Encode(%q) = %q, want %q", test
.src
, s
, test
.exp
)
45 func TestEncodedWordLength(t
*testing
.T
) {
50 {QEncoding
, strings
.Repeat("à", 30)},
51 {QEncoding
, strings
.Repeat("é", 60)},
52 {BEncoding
, strings
.Repeat("ï", 25)},
53 {BEncoding
, strings
.Repeat("ô", 37)},
54 {BEncoding
, strings
.Repeat("\x80", 50)},
55 {QEncoding
, "{$firstname} Bienvendio a Apostolica, aquà inicia el camino de tu"},
58 for _
, test
:= range tests
{
59 s
:= test
.enc
.Encode("utf-8", test
.src
)
61 for i
:= 0; i
< len(s
); i
++ {
68 if wordLen
> maxEncodedWordLen
{
69 t
.Errorf("Encode(%q) has more than %d characters: %q",
70 test
.src
, maxEncodedWordLen
, s
)
76 func TestDecodeWord(t
*testing
.T
) {
81 {"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!", false},
82 {"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme", false},
83 {"=?UTF-8?q?ascii?=", "ascii", false},
84 {"=?utf-8?B?QW5kcsOp?=", "André", false},
85 {"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont", false},
86 {"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`, false},
87 {"=?UTF-8?A?Test?=", "", true},
88 {"=?UTF-8?Q?A=B?=", "", true},
89 {"=?UTF-8?Q?=A?=", "", true},
90 {"=?UTF-8?A?A?=", "", true},
93 for _
, test
:= range tests
{
94 dec
:= new(WordDecoder
)
95 s
, err
:= dec
.Decode(test
.src
)
96 if test
.hasErr
&& err
== nil {
97 t
.Errorf("Decode(%q) should return an error", test
.src
)
100 if !test
.hasErr
&& err
!= nil {
101 t
.Errorf("Decode(%q): %v", test
.src
, err
)
105 t
.Errorf("Decode(%q) = %q, want %q", test
.src
, s
, test
.exp
)
110 func TestDecodeHeader(t
*testing
.T
) {
114 {"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!"},
115 {"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme"},
116 {"=?UTF-8?q?ascii?=", "ascii"},
117 {"=?utf-8?B?QW5kcsOp?=", "André"},
118 {"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont"},
120 {"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`},
121 {"=?UTF-8?A?Test?=", "=?UTF-8?A?Test?="},
122 {"=?UTF-8?Q?A=B?=", "=?UTF-8?Q?A=B?="},
123 {"=?UTF-8?Q?=A?=", "=?UTF-8?Q?=A?="},
124 {"=?UTF-8?A?A?=", "=?UTF-8?A?A?="},
127 {"=?UTF-8?", "=?UTF-8?"},
128 {"=?UTF-8?=", "=?UTF-8?="},
129 {"=?UTF-8?Q", "=?UTF-8?Q"},
130 {"=?UTF-8?Q?", "=?UTF-8?Q?"},
131 {"=?UTF-8?Q?=", "=?UTF-8?Q?="},
132 {"=?UTF-8?Q?A", "=?UTF-8?Q?A"},
133 {"=?UTF-8?Q?A?", "=?UTF-8?Q?A?"},
134 // Tests from RFC 2047
135 {"=?ISO-8859-1?Q?a?=", "a"},
136 {"=?ISO-8859-1?Q?a?= b", "a b"},
137 {"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"},
138 {"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"},
139 {"=?ISO-8859-1?Q?a?= \r\n\t =?ISO-8859-1?Q?b?=", "ab"},
140 {"=?ISO-8859-1?Q?a_b?=", "a b"},
143 for _
, test
:= range tests
{
144 dec
:= new(WordDecoder
)
145 s
, err
:= dec
.DecodeHeader(test
.src
)
147 t
.Errorf("DecodeHeader(%q): %v", test
.src
, err
)
150 t
.Errorf("DecodeHeader(%q) = %q, want %q", test
.src
, s
, test
.exp
)
155 func TestCharsetDecoder(t
*testing
.T
) {
162 {"=?utf-8?b?Q2Fmw6k=?=", "Café", nil, nil},
163 {"=?ISO-8859-1?Q?caf=E9?=", "café", nil, nil},
164 {"=?US-ASCII?Q?foo_bar?=", "foo bar", nil, nil},
165 {"=?utf-8?Q?=?=", "=?utf-8?Q?=?=", nil, nil},
166 {"=?utf-8?Q?=A?=", "=?utf-8?Q?=A?=", nil, nil},
168 "=?ISO-8859-15?Q?f=F5=F6?= =?windows-1252?Q?b=E0r?=",
170 []string{"iso-8859-15", "windows-1252"},
171 []string{"f\xf5\xf6", "b\xe0r"},
175 for _
, test
:= range tests
{
178 CharsetReader
: func(charset
string, input io
.Reader
) (io
.Reader
, error
) {
179 if charset
!= test
.charsets
[i
] {
180 t
.Errorf("DecodeHeader(%q), got charset %q, want %q", test
.src
, charset
, test
.charsets
[i
])
182 content
, err
:= ioutil
.ReadAll(input
)
184 t
.Errorf("DecodeHeader(%q), error in reader: %v", test
.src
, err
)
186 got
:= string(content
)
187 if got
!= test
.content
[i
] {
188 t
.Errorf("DecodeHeader(%q), got content %q, want %q", test
.src
, got
, test
.content
[i
])
192 return strings
.NewReader(got
), nil
195 got
, err
:= dec
.DecodeHeader(test
.src
)
197 t
.Errorf("DecodeHeader(%q): %v", test
.src
, err
)
199 if got
!= test
.want
{
200 t
.Errorf("DecodeHeader(%q) = %q, want %q", test
.src
, got
, test
.want
)
205 func TestCharsetDecoderError(t
*testing
.T
) {
207 CharsetReader
: func(charset
string, input io
.Reader
) (io
.Reader
, error
) {
208 return nil, errors
.New("Test error")
212 if _
, err
:= dec
.DecodeHeader("=?charset?Q?foo?="); err
== nil {
213 t
.Error("DecodeHeader should return an error")
217 func BenchmarkQEncodeWord(b
*testing
.B
) {
218 for i
:= 0; i
< b
.N
; i
++ {
219 QEncoding
.Encode("UTF-8", "¡Hola, señor!")
223 func BenchmarkQDecodeWord(b
*testing
.B
) {
224 dec
:= new(WordDecoder
)
226 for i
:= 0; i
< b
.N
; i
++ {
227 dec
.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
231 func BenchmarkQDecodeHeader(b
*testing
.B
) {
232 dec
:= new(WordDecoder
)
234 for i
:= 0; i
< b
.N
; i
++ {
235 dec
.DecodeHeader("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")