@@ -7,6 +7,8 @@ package mail
7
7
import (
8
8
"bytes"
9
9
"fmt"
10
+ "io"
11
+ "strings"
10
12
"testing"
11
13
)
12
14
@@ -43,6 +45,35 @@ func TestPartEncoding(t *testing.T) {
43
45
}
44
46
}
45
47
48
+ // TestPartContentType tests Part.SetContentType
49
+ func TestPart_SetContentType (t * testing.T ) {
50
+ tests := []struct {
51
+ name string
52
+ ct ContentType
53
+ want string
54
+ }{
55
+ {"ContentType: text/plain" , TypeTextPlain , "text/plain" },
56
+ {"ContentType: text/html" , TypeTextHTML , "text/html" },
57
+ {"ContentType: application/json" , "application/json" , "application/json" },
58
+ }
59
+ for _ , tt := range tests {
60
+ t .Run (tt .name , func (t * testing.T ) {
61
+ m := NewMsg ()
62
+ m .SetBodyString (TypeTextPlain , "This is a test with ümläutß" )
63
+ pl , err := getPartList (m )
64
+ if err != nil {
65
+ t .Errorf ("failed: %s" , err )
66
+ return
67
+ }
68
+ pl [0 ].SetContentType (tt .ct )
69
+ ct := pl [0 ].GetContentType ()
70
+ if string (ct ) != tt .want {
71
+ t .Errorf ("SetContentType failed. Got: %s, expected: %s" , string (ct ), tt .want )
72
+ }
73
+ })
74
+ }
75
+ }
76
+
46
77
// TestPartEncoding tests Part.GetEncoding
47
78
func TestPart_GetEncoding (t * testing.T ) {
48
79
tests := []struct {
@@ -119,6 +150,97 @@ func TestPart_GetWriteFunc(t *testing.T) {
119
150
}
120
151
}
121
152
153
+ // TestPart_GetContent tests Part.GetContent
154
+ func TestPart_GetContent (t * testing.T ) {
155
+ c := "This is a test with ümläutß"
156
+ m := NewMsg ()
157
+ m .SetBodyString (TypeTextPlain , c )
158
+ pl , err := getPartList (m )
159
+ if err != nil {
160
+ t .Errorf ("failed: %s" , err )
161
+ return
162
+ }
163
+ cb , err := pl [0 ].GetContent ()
164
+ if err != nil {
165
+ t .Errorf ("Part.GetContent failed: %s" , err )
166
+ }
167
+ if string (cb ) != c {
168
+ t .Errorf ("Part.GetContent failed. Expected: %s, got: %s" , c , string (cb ))
169
+ }
170
+ }
171
+
172
+ // TestPart_GetContentBroken tests Part.GetContent
173
+ func TestPart_GetContentBroken (t * testing.T ) {
174
+ c := "This is a test with ümläutß"
175
+ m := NewMsg ()
176
+ m .SetBodyString (TypeTextPlain , c )
177
+ pl , err := getPartList (m )
178
+ if err != nil {
179
+ t .Errorf ("failed: %s" , err )
180
+ return
181
+ }
182
+ pl [0 ].w = func (io.Writer ) (int64 , error ) {
183
+ return 0 , fmt .Errorf ("broken" )
184
+ }
185
+ _ , err = pl [0 ].GetContent ()
186
+ if err == nil {
187
+ t .Errorf ("Part.GetContent was supposed to failed, but didn't" )
188
+ }
189
+ }
190
+
191
+ // TestPart_SetWriteFunc tests Part.SetWriteFunc
192
+ func TestPart_SetWriteFunc (t * testing.T ) {
193
+ c := "This is a test with ümläutß"
194
+ m := NewMsg ()
195
+ m .SetBodyString (TypeTextPlain , c )
196
+ pl , err := getPartList (m )
197
+ if err != nil {
198
+ t .Errorf ("failed: %s" , err )
199
+ return
200
+ }
201
+ cb , err := pl [0 ].GetContent ()
202
+ if err != nil {
203
+ t .Errorf ("Part.GetContent failed: %s" , err )
204
+ }
205
+ pl [0 ].SetWriteFunc (func (w io.Writer ) (int64 , error ) {
206
+ ns := strings .ToUpper (string (cb ))
207
+ buf := bytes .NewBufferString (ns )
208
+ nb , err := w .Write (buf .Bytes ())
209
+ return int64 (nb ), err
210
+ })
211
+ nc , err := pl [0 ].GetContent ()
212
+ if err != nil {
213
+ t .Errorf ("Part.GetContent failed: %s" , err )
214
+ }
215
+ if string (nc ) != strings .ToUpper (c ) {
216
+ t .Errorf ("SetWriteFunc failed. Expected: %s, got: %s" , strings .ToUpper (c ), string (nc ))
217
+ }
218
+ }
219
+
220
+ // TestPart_SetContent tests Part.SetContent
221
+ func TestPart_SetContent (t * testing.T ) {
222
+ c := "This is a test with ümläutß"
223
+ m := NewMsg ()
224
+ m .SetBodyString (TypeTextPlain , c )
225
+ pl , err := getPartList (m )
226
+ if err != nil {
227
+ t .Errorf ("failed: %s" , err )
228
+ return
229
+ }
230
+ cb , err := pl [0 ].GetContent ()
231
+ if err != nil {
232
+ t .Errorf ("Part.GetContent failed: %s" , err )
233
+ }
234
+ pl [0 ].SetContent (strings .ToUpper (string (cb )))
235
+ nc , err := pl [0 ].GetContent ()
236
+ if err != nil {
237
+ t .Errorf ("Part.GetContent failed: %s" , err )
238
+ }
239
+ if string (nc ) != strings .ToUpper (c ) {
240
+ t .Errorf ("SetContent failed. Expected: %s, got: %s" , strings .ToUpper (c ), string (nc ))
241
+ }
242
+ }
243
+
122
244
// getPartList is a helper function
123
245
func getPartList (m * Msg ) ([]* Part , error ) {
124
246
pl := m .GetParts ()
0 commit comments