-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
145 lines (132 loc) · 4.75 KB
/
header_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package qp
import (
"fmt"
"strings"
"testing"
)
func ExampleEncodeHeader() {
fmt.Println(StdWordEncoder.EncodeHeader("Cofee"))
fmt.Println(StdWordEncoder.EncodeHeader("Café"))
// Output:
// Cofee
// =?UTF-8?Q?Caf=C3=A9?=
}
func ExampleNewWordEncoder() {
e, err := NewWordEncoder("UTF-8", B)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf(e.EncodeHeader("Caf\xc3"))
// Output: =?UTF-8?B?Q2Fmww==?=
}
func ExampleDecodeHeader() {
// text is not encoded in UTF-8 but in ISO-8859-1
text, charset, err := DecodeHeader("=?ISO-8859-1?Q?Caf=C3?=")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("Text: %q, charset: %q", text, charset)
// Output: Text: "Caf\xc3", charset: "ISO-8859-1"
}
func TestNewWordEncoder(t *testing.T) {
_, err := NewWordEncoder("UTF-8", "A")
if err == nil {
t.Error(`NewWordEncoder("UTF-8", "A") should return an error`)
}
}
func TestEncodeWord(t *testing.T) {
utf8, iso88591 := "UTF-8", "iso-8859-1"
tests := []struct {
charset, encoding, src, exp string
}{
{utf8, Q, "François-Jérôme", "=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?="},
{utf8, B, "André", "=?UTF-8?B?QW5kcsOp?="},
{iso88591, Q, "Rapha\xebl Dupont", "=?iso-8859-1?Q?Rapha=EBl_Dupont?="},
{utf8, Q, "A", "=?UTF-8?Q?A?="},
{utf8, Q, "An 'encoded-word' may not be more than 75 characters long, including 'charset', 'encoding', 'encoded-text', and delimiters.", "=?UTF-8?Q?An_'encoded-word'_may_not_be_more_than_75_characters_long,_incl?=\r\n =?UTF-8?Q?uding_'charset',_'encoding',_'encoded-text',_and_delimiters.?="},
{utf8, Q, strings.Repeat("0", 62) + "é", "=?UTF-8?Q?" + strings.Repeat("0", 62) + "?=\r\n =?UTF-8?Q?=C3=A9?="},
{utf8, B, strings.Repeat("?", 46), "=?UTF-8?B?" + strings.Repeat("Pz8/", 15) + "?=\r\n =?UTF-8?B?Pw==?="},
}
for _, test := range tests {
e, err := NewWordEncoder(test.charset, test.encoding)
if err != nil {
t.Errorf("NewWordEncoder(%q, %q) = error %v, want %v", test.charset, test.encoding, err, error(nil))
} else if s := e.EncodeWord(test.src); s != test.exp {
t.Errorf("EncodeWord(%q) = %q, want %q", test.src, s, test.exp)
}
}
}
func TestEncodeHeader(t *testing.T) {
utf8 := "UTF-8"
tests := []struct {
charset, encoding, src, exp string
}{
{utf8, Q, "François-Jérôme", "=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?="},
{utf8, Q, "A", "A"},
}
for _, test := range tests {
e, err := NewWordEncoder(test.charset, test.encoding)
if err != nil {
t.Errorf("NewWordEncoder(%q, %q) = error %v, want %v", test.charset, test.encoding, err, error(nil))
} else if s := e.EncodeHeader(test.src); s != test.exp {
t.Errorf("EncodeHeader(%q) = %q, want %q", test.src, s, test.exp)
}
}
}
func TestDecodeWord(t *testing.T) {
tests := []struct {
src, exp, charset string
isError bool
}{
{"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme", "UTF-8", false},
{"=?UTF-8?q?ascii?=", "ascii", "UTF-8", false},
{"=?utf-8?B?QW5kcsOp?=", "André", "utf-8", false},
{"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Rapha\xebl Dupont", "ISO-8859-1", false},
{"Jean", "Jean", "", false},
{"=?UTF-8?A?Test?=", "", "UTF-8", true},
{"=?UTF-8?Q?A=B?=", "A", "UTF-8", true},
}
for _, test := range tests {
s, charset, err := DecodeWord(test.src)
if test.isError && err == nil {
t.Errorf("DecodeWord(%q) should return an error", test.src)
}
if !test.isError && err != nil {
t.Errorf("DecodeWord(%q) = error %v, want %v", test.src, err, error(nil))
}
if s != test.exp || charset != test.charset {
t.Errorf("DecodeWord(%q) = %q (charset=%q), want %q (charset=%q)", test.src, s, charset, test.exp, test.charset)
}
}
}
func TestDecodeHeader(t *testing.T) {
tests := []struct {
src, exp, charset string
isError bool
}{
{"=?UTF-8?Q?=A?=", "=?UTF-8?Q?=A?=", "", false},
{"=?UTF-8?A?A?=", "=?UTF-8?A?A?=", "", false},
// Tests from RFC 2047
{"=?ISO-8859-1?Q?a?=", "a", "ISO-8859-1", false},
{"=?ISO-8859-1?Q?a?= b", "a b", "ISO-8859-1", false},
{"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab", "ISO-8859-1", false},
{"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab", "ISO-8859-1", false},
{"=?ISO-8859-1?Q?a?= \r\n\t =?ISO-8859-1?Q?b?=", "ab", "ISO-8859-1", false},
{"=?ISO-8859-1?Q?a_b?=", "a b", "ISO-8859-1", false},
{"=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=", "", "", true},
}
for _, test := range tests {
s, charset, err := DecodeHeader(test.src)
if test.isError && err == nil {
t.Errorf("DecodeHeader(%q) should return an error", test.src)
}
if !test.isError && err != nil {
t.Errorf("DecodeHeader(%q) = error %v, want %v", test.src, err, error(nil))
}
if s != test.exp || charset != test.charset {
t.Errorf("DecodeHeader(%q) = %q (charset=%q), want %q (charset=%q)", test.src, s, charset, test.exp, test.charset)
}
}
}