-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
part_test.go
378 lines (361 loc) · 9.75 KB
/
part_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package mail
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
)
// TestPartEncoding tests the WithPartEncoding and Part.SetEncoding methods
func TestPartEncoding(t *testing.T) {
tests := []struct {
name string
enc Encoding
want string
}{
{"Part encoding: Base64", EncodingB64, "base64"},
{"Part encoding: Quoted-Printable", EncodingQP, "quoted-printable"},
{"Part encoding: 8bit", NoEncoding, "8bit"},
}
for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) {
part := m.newPart(TypeTextPlain, WithPartEncoding(tt.enc), nil)
if part == nil {
t.Errorf("newPart() WithPartEncoding() failed: no part returned")
return
}
if part.encoding.String() != tt.want {
t.Errorf("newPart() WithPartEncoding() failed: expected encoding: %s, got: %s", tt.want,
part.encoding.String())
}
part.encoding = ""
part.SetEncoding(tt.enc)
if part.encoding.String() != tt.want {
t.Errorf("newPart() SetEncoding() failed: expected encoding: %s, got: %s", tt.want,
part.encoding.String())
}
})
}
}
// TestWithPartCharset tests the WithPartCharset method
func TestWithPartCharset(t *testing.T) {
tests := []struct {
name string
cs Charset
want string
}{
{"Part charset: UTF-8", CharsetUTF8, "UTF-8"},
{"Part charset: ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
{"Part charset: empty", "", ""},
}
for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) {
part := m.newPart(TypeTextPlain, WithPartCharset(tt.cs), nil)
if part == nil {
t.Errorf("newPart() WithPartCharset() failed: no part returned")
return
}
if part.charset.String() != tt.want {
t.Errorf("newPart() WithPartCharset() failed: expected charset: %s, got: %s",
tt.want, part.charset.String())
}
})
}
}
// TestPart_WithPartContentDescription tests the WithPartContentDescription method
func TestPart_WithPartContentDescription(t *testing.T) {
tests := []struct {
name string
desc string
}{
{"Part description: test", "test"},
{"Part description: empty", ""},
}
for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) {
part := m.newPart(TypeTextPlain, WithPartContentDescription(tt.desc), nil)
if part == nil {
t.Errorf("newPart() WithPartContentDescription() failed: no part returned")
return
}
if part.description != tt.desc {
t.Errorf("newPart() WithPartContentDescription() failed: expected: %s, got: %s", tt.desc,
part.description)
}
part.description = ""
part.SetDescription(tt.desc)
if part.description != tt.desc {
t.Errorf("newPart() SetDescription() failed: expected: %s, got: %s", tt.desc, part.description)
}
})
}
}
// TestPartContentType tests Part.SetContentType
func TestPart_SetContentType(t *testing.T) {
tests := []struct {
name string
ct ContentType
want string
}{
{"ContentType: text/plain", TypeTextPlain, "text/plain"},
{"ContentType: text/html", TypeTextHTML, "text/html"},
{"ContentType: application/json", "application/json", "application/json"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMsg()
m.SetBodyString(TypeTextPlain, "This is a test with ümläutß")
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pl[0].SetContentType(tt.ct)
ct := pl[0].GetContentType()
if string(ct) != tt.want {
t.Errorf("SetContentType failed. Got: %s, expected: %s", string(ct), tt.want)
}
})
}
}
// TestPartEncoding tests Part.GetEncoding
func TestPart_GetEncoding(t *testing.T) {
tests := []struct {
name string
enc Encoding
want string
}{
{"Part encoding: Base64", EncodingB64, "base64"},
{"Part encoding: Quoted-Printable", EncodingQP, "quoted-printable"},
{"Part encoding: 8bit", NoEncoding, "8bit"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMsg()
m.SetBodyString(TypeTextPlain, "This is a test with ümläutß", WithPartEncoding(tt.enc))
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
e := pl[0].GetEncoding()
if e.String() != tt.want {
t.Errorf("Part.GetEncoding failed. Expected: %s, got: %s", tt.want, e.String())
}
})
}
}
// TestPart_GetContentType tests Part.GetContentType
func TestPart_GetContentType(t *testing.T) {
tests := []struct {
name string
ct ContentType
want string
}{
{"ContentType: text/plain", TypeTextPlain, "text/plain"},
{"ContentType: text/html", TypeTextHTML, "text/html"},
{"ContentType: application/json", "application/json", "application/json"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMsg()
m.SetBodyString(tt.ct, "This is a test with ümläutß")
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
c := pl[0].GetContentType()
if string(c) != tt.want {
t.Errorf("Part.GetContentType failed. Expected: %s, got: %s", tt.want, string(c))
}
})
}
}
// TestPart_GetWriteFunc tests Part.GetWriteFunc
func TestPart_GetWriteFunc(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
wf := pl[0].GetWriteFunc()
var b bytes.Buffer
if _, err := wf(&b); err != nil {
t.Errorf("failed to execute writefunc: %s", err)
}
if b.String() != c {
t.Errorf("GetWriteFunc failed. Expected: %s, got: %s", c, b.String())
}
}
// TestPart_GetContent tests Part.GetContent
func TestPart_GetContent(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
cb, err := pl[0].GetContent()
if err != nil {
t.Errorf("Part.GetContent failed: %s", err)
}
if string(cb) != c {
t.Errorf("Part.GetContent failed. Expected: %s, got: %s", c, string(cb))
}
}
// TestPart_GetContentBroken tests Part.GetContent
func TestPart_GetContentBroken(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pl[0].writeFunc = func(io.Writer) (int64, error) {
return 0, fmt.Errorf("broken")
}
_, err = pl[0].GetContent()
if err == nil {
t.Errorf("Part.GetContent was supposed to failed, but didn't")
}
}
// TestPart_SetWriteFunc tests Part.SetWriteFunc
func TestPart_SetWriteFunc(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
cb, err := pl[0].GetContent()
if err != nil {
t.Errorf("Part.GetContent failed: %s", err)
}
pl[0].SetWriteFunc(func(w io.Writer) (int64, error) {
ns := strings.ToUpper(string(cb))
buf := bytes.NewBufferString(ns)
nb, err := w.Write(buf.Bytes())
return int64(nb), err
})
nc, err := pl[0].GetContent()
if err != nil {
t.Errorf("Part.GetContent failed: %s", err)
}
if string(nc) != strings.ToUpper(c) {
t.Errorf("SetWriteFunc failed. Expected: %s, got: %s", strings.ToUpper(c), string(nc))
}
}
// TestPart_SetContent tests Part.SetContent
func TestPart_SetContent(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
cb, err := pl[0].GetContent()
if err != nil {
t.Errorf("Part.GetContent failed: %s", err)
}
pl[0].SetContent(strings.ToUpper(string(cb)))
nc, err := pl[0].GetContent()
if err != nil {
t.Errorf("Part.GetContent failed: %s", err)
}
if string(nc) != strings.ToUpper(c) {
t.Errorf("SetContent failed. Expected: %s, got: %s", strings.ToUpper(c), string(nc))
}
}
// TestPart_SetDescription tests Part.SetDescription
func TestPart_SetDescription(t *testing.T) {
c := "This is a test"
d := "test-description"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pd := pl[0].GetDescription()
if pd != "" {
t.Errorf("Part.GetDescription failed. Expected empty description but got: %s", pd)
}
pl[0].SetDescription(d)
if pl[0].description != d {
t.Errorf("Part.SetDescription failed. Expected description to be: %s, got: %s", d, pd)
}
pd = pl[0].GetDescription()
if pd != d {
t.Errorf("Part.GetDescription failed. Expected: %s, got: %s", d, pd)
}
}
// TestPart_Delete tests Part.Delete
func TestPart_Delete(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pl[0].Delete()
if !pl[0].isDeleted {
t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].isDeleted)
}
}
// getPartList is a helper function
func getPartList(m *Msg) ([]*Part, error) {
pl := m.GetParts()
if len(pl) <= 0 {
return nil, fmt.Errorf("Msg.GetParts failed. Part list is empty")
}
return pl, nil
}
// TestPart_SetCharset tests Part.SetCharset method
func TestPart_SetCharset(t *testing.T) {
tests := []struct {
name string
cs Charset
want string
}{
{"Charset: UTF-8", CharsetUTF8, "UTF-8"},
{"Charset: ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
{"Charset: empty", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMsg()
m.SetBodyString(TypeTextPlain, "This is a test with ümläutß")
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pl[0].SetCharset(tt.cs)
cs := pl[0].GetCharset()
if string(cs) != tt.want {
t.Errorf("SetCharset failed. Got: %s, expected: %s", string(cs), tt.want)
}
})
}
}