Debian Codegraph @

encode_test.go

raw
1 package enmime_test
2
3 import (
4 "bytes"
5 "testing"
6 "time"
7
8 "github.com/jhillyerd/enmime"
9 "github.com/jhillyerd/enmime/internal/test"
10 )
11
12 func TestEncodePartEmpty(t *testing.T) {
13 p := &enmime.Part{}
14
15 b := &bytes.Buffer{}
16 err := p.Encode(b)
17 if err != nil {
18 t.Fatal(err)
19 }
20 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-empty.golden")
21 }
22
23 func TestEncodePartHeaderOnly(t *testing.T) {
24 p := enmime.NewPart("text/plain")
25
26 b := &bytes.Buffer{}
27 err := p.Encode(b)
28 if err != nil {
29 t.Fatal(err)
30 }
31 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-header-only.golden")
32 }
33
34 func TestEncodePartHeaderOnlyDefaultTransferEncoding(t *testing.T) {
35 p := enmime.NewPart("text/plain")
36 p.Header.Add("X-Empty-Header", "")
37
38 b := &bytes.Buffer{}
39 err := p.Encode(b)
40 if err != nil {
41 t.Fatal(err)
42 }
43 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-header-only-default-encoding.golden")
44 }
45
46 func TestEncodePartDefaultHeaders(t *testing.T) {
47 p := enmime.NewPart("application/zip")
48 p.Boundary = "enmime-abcdefg0123456789"
49 p.Charset = "binary"
50 p.ContentID = "mycontentid"
51 p.ContentTypeParams["param1"] = "myparameter1"
52 p.ContentTypeParams["param2"] = "myparameter2"
53 p.Disposition = "attachment"
54 p.FileName = "stuff.zip"
55 p.FileModDate, _ = time.Parse(time.RFC822, "01 Feb 03 04:05 GMT")
56 p.Content = []byte("ZIPZIPZIP")
57
58 b := &bytes.Buffer{}
59 err := p.Encode(b)
60 if err != nil {
61 t.Fatal(err)
62 }
63 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-default-headers.golden")
64 }
65
66 func TestEncodePartQuotedHeaders(t *testing.T) {
67 p := enmime.NewPart("application/zip")
68 p.Boundary = "enmime-abcdefg0123456789"
69 p.Charset = "binary"
70 p.ContentID = "mycontentid"
71 p.ContentTypeParams["param1"] = "myparameter1"
72 p.ContentTypeParams["param2"] = "myparameter2"
73 p.Disposition = "attachment"
74 p.FileName = `árvíztűrő "x" tükörfúrógép.zip`
75 p.FileModDate, _ = time.Parse(time.RFC822, "01 Feb 03 04:05 GMT")
76 p.Content = []byte("ZIPZIPZIP")
77
78 b := &bytes.Buffer{}
79 err := p.Encode(b)
80 if err != nil {
81 t.Fatal(err)
82 }
83 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-quoted-headers.golden")
84 }
85
86 func TestEncodePartQuotedPrintableHeaders(t *testing.T) {
87 p := enmime.NewPart("application/zip")
88 p.Boundary = "enmime-abcdefg0123456789"
89 p.Charset = "binary"
90 p.ContentID = "mycontentid"
91 p.ContentTypeParams["param1"] = "myparameter1"
92 p.ContentTypeParams["param2"] = "myparameter2"
93 p.Disposition = "attachment"
94 p.FileName = `árvíztűrő "x" tükörfúrógép.zip`
95 p.FileModDate, _ = time.Parse(time.RFC822, "01 Feb 03 04:05 GMT")
96 p.Header.Add("X-QP-Header", "Just enough to need qp ☆")
97 p.Content = []byte("ZIPZIPZIP")
98
99 b := &bytes.Buffer{}
100 err := p.Encode(b)
101 if err != nil {
102 t.Fatal(err)
103 }
104 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-quoted-printable-headers.golden")
105 }
106
107 func TestEncodePartBinaryHeader(t *testing.T) {
108 p := enmime.NewPart("text/plain")
109 p.Header.Set("Subject", "¡Hola, señor!")
110 p.Header.Set("X-Data", string([]byte{
111 0x3, 0x17, 0xe1, 0x7e, 0xe8, 0xeb, 0xa2, 0x96, 0x9d, 0x95, 0xa7, 0x67, 0x82, 0x9,
112 0xdf, 0x8e, 0xc, 0x2c, 0x6a, 0x2b, 0x9b, 0xbe, 0x79, 0xa4, 0x69, 0xd8, 0xae, 0x86,
113 0xd7, 0xab, 0xa8, 0x72, 0x52, 0x15, 0xfb, 0x80, 0x8e, 0x47, 0xe1, 0xae, 0xaa, 0x5e,
114 0xa2, 0xb2, 0xc0, 0x90, 0x59, 0xe3, 0x35, 0xf8, 0x60, 0xb7, 0xb1, 0x63, 0x77, 0xd7,
115 0x5f, 0x92, 0x58, 0xa8, 0x75,
116 }))
117 p.Content = []byte("This is a test of a plain text part.\r\n\r\nAnother line.\r\n")
118
119 b := &bytes.Buffer{}
120 err := p.Encode(b)
121 if err != nil {
122 t.Fatal(err)
123 }
124 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-bin-header.golden")
125 }
126
127 func TestEncodePartContentOnly(t *testing.T) {
128 p := &enmime.Part{}
129 p.Content = []byte("No header, only content.")
130
131 b := &bytes.Buffer{}
132 err := p.Encode(b)
133 if err != nil {
134 t.Fatal(err)
135 }
136 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-content-only.golden")
137 }
138
139 func TestEncodePartContentOnlyQP(t *testing.T) {
140 p := &enmime.Part{}
141 p.Content = []byte("☆ No header, only content.")
142
143 b := &bytes.Buffer{}
144 err := p.Encode(b)
145 if err != nil {
146 t.Fatal(err)
147 }
148 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-content-only-qp.golden")
149 }
150
151 func TestEncodePartPlain(t *testing.T) {
152 p := enmime.NewPart("text/plain")
153 p.Content = []byte("This is a test of a plain text part.\r\n\r\nAnother line.\r\n")
154
155 b := &bytes.Buffer{}
156 err := p.Encode(b)
157 if err != nil {
158 t.Fatal(err)
159 }
160 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-plain.golden")
161 }
162
163 func TestEncodePartWithChildren(t *testing.T) {
164 p := enmime.NewPart("multipart/alternative")
165 p.Boundary = "enmime-1234567890-parent"
166 p.Content = []byte("Bro, do you even MIME?")
167 root := p
168
169 p = enmime.NewPart("text/html")
170 p.Content = []byte("<div>HTML part</div>")
171 root.FirstChild = p
172
173 p = enmime.NewPart("text/plain")
174 p.Content = []byte("Plain text part")
175 root.FirstChild.NextSibling = p
176
177 b := &bytes.Buffer{}
178 err := root.Encode(b)
179 if err != nil {
180 t.Fatal(err)
181 }
182 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-with-children.golden")
183 }
184
185 func TestEncodePartNoContentWithChildren(t *testing.T) {
186 p := enmime.NewPart("multipart/alternative")
187 p.Boundary = "enmime-1234567890-parent"
188 root := p
189
190 p = enmime.NewPart("text/html")
191 p.Content = []byte("<div>HTML part</div>")
192 root.FirstChild = p
193
194 p = enmime.NewPart("text/plain")
195 p.Content = []byte("Plain text part")
196 root.FirstChild.NextSibling = p
197
198 b := &bytes.Buffer{}
199 err := root.Encode(b)
200 if err != nil {
201 t.Fatal(err)
202 }
203 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "nocontent-with-children.golden")
204 }
205
206 func TestEncodePartContentQuotable(t *testing.T) {
207 p := enmime.NewPart("text/plain")
208 p.Content = []byte("¡Hola, señor! Welcome to MIME")
209
210 b := &bytes.Buffer{}
211 err := p.Encode(b)
212 if err != nil {
213 t.Fatal(err)
214 }
215 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-quoted-content.golden")
216 }
217
218 func TestEncodePartWithExistingEncodingHeader(t *testing.T) {
219 p := enmime.NewPart("text/plain")
220 p.Header.Add("Content-Transfer-Encoding", "quoted-printable")
221 p.Content = []byte("Hello=")
222
223 b := &bytes.Buffer{}
224 err := p.Encode(b)
225 if err != nil {
226 t.Fatal(err)
227 }
228 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-quotable-content.golden")
229 }
230
231 func TestEncodePartContentBinary(t *testing.T) {
232 c := make([]byte, 2000)
233 for i := range c {
234 c[i] = byte(i % 256)
235 }
236 p := enmime.NewPart("image/jpeg")
237 p.Content = c
238
239 b := &bytes.Buffer{}
240 err := p.Encode(b)
241 if err != nil {
242 t.Fatal(err)
243 }
244 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-bin-content.golden")
245 }
246
247 func TestEncodeFileModDate(t *testing.T) {
248 p := enmime.NewPart("text/plain")
249 p.Content = []byte("¡Hola, señor! Welcome to MIME")
250 p.Disposition = "inline"
251 p.FileModDate, _ = time.Parse(time.RFC822, "01 Feb 03 04:05 GMT")
252
253 b := &bytes.Buffer{}
254 err := p.Encode(b)
255 if err != nil {
256 t.Fatal(err)
257 }
258 test.DiffGolden(t, b.Bytes(), "testdata", "encode", "part-file-mod-date.golden")
259 }
260
261 func TestEncodePartContentNonAsciiText(t *testing.T) {
262 p := enmime.NewPart("text/plain")
263
264 threshold := 20
265
266 cases := []int{
267 threshold - 1,
268 threshold,
269 threshold + 1,
270 }
271
272 for _, numNonAscii := range cases {
273 nonAscii := bytes.Repeat([]byte{byte(0x10)}, numNonAscii)
274 ascii := bytes.Repeat([]byte{0x41}, 100-numNonAscii)
275
276 p.Content = append(nonAscii, ascii[:]...)
277
278 b := &bytes.Buffer{}
279 err := p.Encode(b)
280 if err != nil {
281 t.Fatal(err)
282 }
283
284 if numNonAscii < threshold {
285 test.DiffStrings(t, []string{p.Header.Get("Content-Transfer-Encoding")}, []string{"quoted-printable"})
286 } else {
287 test.DiffStrings(t, []string{p.Header.Get("Content-Transfer-Encoding")}, []string{"base64"})
288 }
289 }
290 }