| 1 |
package mailyak |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"regexp" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestMailYakTo(t *testing.T) { |
| 10 |
t.Parallel() |
| 11 |
|
| 12 |
tests := []struct { |
| 13 |
// Test description. |
| 14 |
name string |
| 15 |
// Parameters. |
| 16 |
addrs []string |
| 17 |
// Want |
| 18 |
want []string |
| 19 |
}{ |
| 20 |
{ |
| 21 |
"Single email (without name)", |
| 22 |
[]string{"[email protected]"}, |
| 23 |
[]string{"[email protected]"}, |
| 24 |
}, |
| 25 |
{ |
| 26 |
"Single email (with name)", |
| 27 |
[]string{"Dom <[email protected]>"}, |
| 28 |
[]string{"Dom <[email protected]>"}, |
| 29 |
}, |
| 30 |
{ |
| 31 |
"Multiple email", |
| 32 |
[]string{"Dom <[email protected]>", "[email protected]"}, |
| 33 |
[]string{"Dom <[email protected]>", "[email protected]"}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
"Empty last", |
| 37 |
[]string{"[email protected]", "[email protected]", ""}, |
| 38 |
[]string{"[email protected]", "[email protected]"}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
"Empty Middle", |
| 42 |
[]string{"[email protected]", "", "[email protected]"}, |
| 43 |
[]string{"[email protected]", "[email protected]"}, |
| 44 |
}, |
| 45 |
} |
| 46 |
for _, tt := range tests { |
| 47 |
tt := tt |
| 48 |
t.Run(tt.name, func(t *testing.T) { |
| 49 |
t.Parallel() |
| 50 |
|
| 51 |
m := &MailYak{ |
| 52 |
toAddrs: []string{}, |
| 53 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 54 |
} |
| 55 |
m.To(tt.addrs...) |
| 56 |
|
| 57 |
if !reflect.DeepEqual(m.toAddrs, tt.want) { |
| 58 |
t.Errorf("%q. MailYak.To() = %v, want %v", tt.name, m.toAddrs, tt.want) |
| 59 |
} |
| 60 |
}) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestMailYakBcc(t *testing.T) { |
| 65 |
t.Parallel() |
| 66 |
|
| 67 |
tests := []struct { |
| 68 |
// Test description. |
| 69 |
name string |
| 70 |
// Parameters. |
| 71 |
addrs []string |
| 72 |
// Want |
| 73 |
want []string |
| 74 |
}{ |
| 75 |
{ |
| 76 |
"Single email (without name)", |
| 77 |
[]string{"[email protected]"}, |
| 78 |
[]string{"[email protected]"}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
"Single email (with name)", |
| 82 |
[]string{"Dom <[email protected]>"}, |
| 83 |
[]string{"Dom <[email protected]>"}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
"Multiple email", |
| 87 |
[]string{"Dom <[email protected]>", "[email protected]"}, |
| 88 |
[]string{"Dom <[email protected]>", "[email protected]"}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
"Empty last", |
| 92 |
[]string{"[email protected]", "[email protected]", ""}, |
| 93 |
[]string{"[email protected]", "[email protected]"}, |
| 94 |
}, |
| 95 |
{ |
| 96 |
"Empty Middle", |
| 97 |
[]string{"[email protected]", "", "[email protected]"}, |
| 98 |
[]string{"[email protected]", "[email protected]"}, |
| 99 |
}, |
| 100 |
} |
| 101 |
for _, tt := range tests { |
| 102 |
tt := tt |
| 103 |
t.Run(tt.name, func(t *testing.T) { |
| 104 |
t.Parallel() |
| 105 |
|
| 106 |
m := &MailYak{ |
| 107 |
bccAddrs: []string{}, |
| 108 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 109 |
} |
| 110 |
m.Bcc(tt.addrs...) |
| 111 |
|
| 112 |
if !reflect.DeepEqual(m.bccAddrs, tt.want) { |
| 113 |
t.Errorf("%q. MailYak.Bcc() = %v, want %v", tt.name, m.bccAddrs, tt.want) |
| 114 |
} |
| 115 |
}) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestMailYakCc(t *testing.T) { |
| 120 |
t.Parallel() |
| 121 |
|
| 122 |
tests := []struct { |
| 123 |
// Test description. |
| 124 |
name string |
| 125 |
// Parameters. |
| 126 |
addrs []string |
| 127 |
// Want |
| 128 |
want []string |
| 129 |
}{ |
| 130 |
{ |
| 131 |
"Single email (without name)", |
| 132 |
[]string{"[email protected]"}, |
| 133 |
[]string{"[email protected]"}, |
| 134 |
}, |
| 135 |
{ |
| 136 |
"Single email (with name)", |
| 137 |
[]string{"Dom <[email protected]>"}, |
| 138 |
[]string{"Dom <[email protected]>"}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
"Multiple email", |
| 142 |
[]string{"Dom <[email protected]>", "[email protected]"}, |
| 143 |
[]string{"Dom <[email protected]>", "[email protected]"}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
"Empty last", |
| 147 |
[]string{"[email protected]", "[email protected]", ""}, |
| 148 |
[]string{"[email protected]", "[email protected]"}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
"Empty Middle", |
| 152 |
[]string{"[email protected]", "", "[email protected]"}, |
| 153 |
[]string{"[email protected]", "[email protected]"}, |
| 154 |
}, |
| 155 |
} |
| 156 |
for _, tt := range tests { |
| 157 |
tt := tt |
| 158 |
t.Run(tt.name, func(t *testing.T) { |
| 159 |
t.Parallel() |
| 160 |
|
| 161 |
m := &MailYak{ |
| 162 |
ccAddrs: []string{}, |
| 163 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 164 |
} |
| 165 |
m.Cc(tt.addrs...) |
| 166 |
|
| 167 |
if !reflect.DeepEqual(m.ccAddrs, tt.want) { |
| 168 |
t.Errorf("%q. MailYak.Cc() = %v, want %v", tt.name, m.ccAddrs, tt.want) |
| 169 |
} |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func TestMailYakSubject(t *testing.T) { |
| 175 |
t.Parallel() |
| 176 |
|
| 177 |
tests := []struct { |
| 178 |
// Test description. |
| 179 |
name string |
| 180 |
// Parameters. |
| 181 |
subject string |
| 182 |
// Want |
| 183 |
want string |
| 184 |
}{ |
| 185 |
{ |
| 186 |
"ASCII", |
| 187 |
"Banana\r\n", |
| 188 |
"Banana", |
| 189 |
}, |
| 190 |
{ |
| 191 |
"Q-encoded", |
| 192 |
"🍌\r\n", |
| 193 |
"=?UTF-8?q?=F0=9F=8D=8C?=", |
| 194 |
}, |
| 195 |
} |
| 196 |
for _, tt := range tests { |
| 197 |
tt := tt |
| 198 |
t.Run(tt.name, func(t *testing.T) { |
| 199 |
t.Parallel() |
| 200 |
|
| 201 |
m := &MailYak{ |
| 202 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 203 |
} |
| 204 |
m.Subject(tt.subject) |
| 205 |
|
| 206 |
if !reflect.DeepEqual(m.subject, tt.want) { |
| 207 |
t.Errorf("%q. MailYak.Subject() = %v, want %v", tt.name, m.subject, tt.want) |
| 208 |
} |
| 209 |
}) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestMailYakFromName(t *testing.T) { |
| 214 |
t.Parallel() |
| 215 |
|
| 216 |
tests := []struct { |
| 217 |
// Test description. |
| 218 |
name string |
| 219 |
// Parameters. |
| 220 |
from string |
| 221 |
// Want |
| 222 |
want string |
| 223 |
}{ |
| 224 |
{ |
| 225 |
"ASCII", |
| 226 |
"Goat\r\n", |
| 227 |
"Goat", |
| 228 |
}, |
| 229 |
{ |
| 230 |
"Q-encoded", |
| 231 |
"🐐", |
| 232 |
"=?UTF-8?q?=F0=9F=90=90?=", |
| 233 |
}, |
| 234 |
} |
| 235 |
for _, tt := range tests { |
| 236 |
tt := tt |
| 237 |
t.Run(tt.name, func(t *testing.T) { |
| 238 |
t.Parallel() |
| 239 |
|
| 240 |
m := &MailYak{ |
| 241 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 242 |
} |
| 243 |
m.FromName(tt.from) |
| 244 |
|
| 245 |
if !reflect.DeepEqual(m.fromName, tt.want) { |
| 246 |
t.Errorf("%q. MailYak.Subject() = %v, want %v", tt.name, m.fromName, tt.want) |
| 247 |
} |
| 248 |
}) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func TestMailYakAddHeader(t *testing.T) { |
| 253 |
t.Parallel() |
| 254 |
|
| 255 |
tests := []struct { |
| 256 |
// Test description. |
| 257 |
name string |
| 258 |
// Parameters. |
| 259 |
from map[string][]string |
| 260 |
// Want |
| 261 |
want map[string][]string |
| 262 |
}{ |
| 263 |
{ |
| 264 |
"ASCII", |
| 265 |
map[string][]string{ |
| 266 |
"List-Unsubscribe": {"http://example.com"}, |
| 267 |
"X-NASTY": {"true\r\nBcc: [email protected]"}, |
| 268 |
}, |
| 269 |
map[string][]string{ |
| 270 |
"List-Unsubscribe": {"http://example.com"}, |
| 271 |
"X-NASTY": {"trueBcc: [email protected]"}, |
| 272 |
}, |
| 273 |
}, |
| 274 |
{ |
| 275 |
"Q-encoded", |
| 276 |
map[string][]string{ |
| 277 |
"X-BEETHOVEN": {"für Elise"}, |
| 278 |
}, |
| 279 |
map[string][]string{ |
| 280 |
"X-BEETHOVEN": {"=?UTF-8?q?f=C3=BCr_Elise?="}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
{ |
| 284 |
"Multi", |
| 285 |
map[string][]string{ |
| 286 |
"X-MailGun-Tag": {"marketing", "transactional"}, |
| 287 |
}, |
| 288 |
map[string][]string{ |
| 289 |
"X-MailGun-Tag": {"marketing", "transactional"}, |
| 290 |
}, |
| 291 |
}, |
| 292 |
} |
| 293 |
for _, tt := range tests { |
| 294 |
tt := tt |
| 295 |
t.Run(tt.name, func(t *testing.T) { |
| 296 |
t.Parallel() |
| 297 |
|
| 298 |
m := &MailYak{ |
| 299 |
headers: map[string][]string{}, |
| 300 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 301 |
} |
| 302 |
|
| 303 |
for k, values := range tt.from { |
| 304 |
for _, v := range values { |
| 305 |
m.AddHeader(k, v) |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
if !reflect.DeepEqual(m.headers, tt.want) { |
| 310 |
t.Errorf("%q. MailYak.AddHeader() = %v, want %v", tt.name, m.headers, tt.want) |
| 311 |
} |
| 312 |
}) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
func TestMailYakLocalName(t *testing.T) { |
| 317 |
t.Parallel() |
| 318 |
|
| 319 |
tests := []struct { |
| 320 |
// Test description. |
| 321 |
name string |
| 322 |
// Parameters. |
| 323 |
from string |
| 324 |
// Want |
| 325 |
want string |
| 326 |
}{ |
| 327 |
{ |
| 328 |
"empty", |
| 329 |
"", |
| 330 |
"", |
| 331 |
}, |
| 332 |
{ |
| 333 |
"ASCII", |
| 334 |
"example.com\r\n", |
| 335 |
"example.com", |
| 336 |
}, |
| 337 |
} |
| 338 |
for _, tt := range tests { |
| 339 |
tt := tt |
| 340 |
t.Run(tt.name, func(t *testing.T) { |
| 341 |
t.Parallel() |
| 342 |
|
| 343 |
m := &MailYak{ |
| 344 |
headers: map[string][]string{}, |
| 345 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 346 |
} |
| 347 |
|
| 348 |
m.LocalName(tt.from) |
| 349 |
|
| 350 |
if !reflect.DeepEqual(m.localName, tt.want) { |
| 351 |
t.Errorf("%q. MailYak.LocalName() = %v, want %v", tt.name, m.localName, tt.want) |
| 352 |
} |
| 353 |
}) |
| 354 |
} |
| 355 |
} |