|
4 | 4 | "bufio"
|
5 | 5 | "bytes"
|
6 | 6 | netmail "net/mail"
|
| 7 | + "net/url" |
7 | 8 | "reflect"
|
8 | 9 | "strings"
|
9 | 10 | "testing"
|
@@ -215,3 +216,224 @@ func TestHeader_EmptyAddressList(t *testing.T) {
|
215 | 216 | }
|
216 | 217 |
|
217 | 218 | }
|
| 219 | + |
| 220 | +func TestHeader_ListCommandURLList(t *testing.T) { |
| 221 | + tests := []struct { |
| 222 | + header string |
| 223 | + raw string |
| 224 | + urls []*url.URL |
| 225 | + xfail bool |
| 226 | + }{ |
| 227 | + { |
| 228 | + header: "List-Help", |
| 229 | + |
| 230 | + xfail: true, |
| 231 | + }, |
| 232 | + // These tests might seem repetitive, but they are the examples given at |
| 233 | + // https://www.rfc-editor.org/rfc/rfc2369. |
| 234 | + { |
| 235 | + header: "List-Help", |
| 236 | + raw: "<mailto:[email protected]?subject=help> (List Instructions)", |
| 237 | + urls: []*url.URL{ |
| 238 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=help"}, |
| 239 | + }, |
| 240 | + }, |
| 241 | + { |
| 242 | + header: "List-Help", |
| 243 | + raw: "<mailto:[email protected]?body=info>", |
| 244 | + urls: []*url.URL{ |
| 245 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "body=info"}, |
| 246 | + }, |
| 247 | + }, |
| 248 | + { |
| 249 | + header: "List-Help", |
| 250 | + raw: "<mailto:[email protected]> (Info about the list)", |
| 251 | + urls: []*url.URL{ |
| 252 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 253 | + }, |
| 254 | + }, |
| 255 | + { |
| 256 | + header: "List-Help", |
| 257 | + raw: "<http://www.host.com/list/>, <mailto:[email protected]>", |
| 258 | + urls: []*url.URL{ |
| 259 | + {Scheme: "http", Host: "www.host.com", Path: "/list/"}, |
| 260 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 261 | + }, |
| 262 | + }, |
| 263 | + { |
| 264 | + header: "List-Help", |
| 265 | + raw: "<ftp://ftp.host.com/list.txt> (FTP),\r\n\t<mailto:[email protected]?subject=help>", |
| 266 | + urls: []*url.URL{ |
| 267 | + {Scheme: "ftp", Host: "ftp.host.com", Path: "/list.txt"}, |
| 268 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=help"}, |
| 269 | + }, |
| 270 | + }, |
| 271 | + { |
| 272 | + header: "List-Unsubscribe", |
| 273 | + raw: "<mailto:[email protected]?subject=unsubscribe>", |
| 274 | + urls: []*url.URL{ |
| 275 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=unsubscribe"}, |
| 276 | + }, |
| 277 | + }, |
| 278 | + { |
| 279 | + header: "List-Unsubscribe", |
| 280 | + raw: "(Use this command to get off the list)\r\n\t<mailto:[email protected]?body=unsubscribe%20list>", |
| 281 | + urls: []*url.URL{ |
| 282 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "body=unsubscribe%20list"}, |
| 283 | + }, |
| 284 | + }, |
| 285 | + { |
| 286 | + header: "List-Unsubscribe", |
| 287 | + raw: "<mailto:[email protected]>", |
| 288 | + urls: []*url.URL{ |
| 289 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 290 | + }, |
| 291 | + }, |
| 292 | + { |
| 293 | + header: "List-Unsubscribe", |
| 294 | + raw: "<http://www.host.com/list.cgi?cmd=unsub&lst=list>,\r\n\t<mailto:[email protected]?subject=unsubscribe>", |
| 295 | + urls: []*url.URL{ |
| 296 | + {Scheme: "http", Host: "www.host.com", Path: "/list.cgi", RawQuery: "cmd=unsub&lst=list"}, |
| 297 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=unsubscribe"}, |
| 298 | + }, |
| 299 | + }, |
| 300 | + { |
| 301 | + header: "List-Subscribe", |
| 302 | + raw: "<mailto:[email protected]?subject=subscribe>", |
| 303 | + urls: []*url.URL{ |
| 304 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=subscribe"}, |
| 305 | + }, |
| 306 | + }, |
| 307 | + { |
| 308 | + header: "List-Subscribe", |
| 309 | + raw: "(Use this command to join the list)\r\n\t<mailto:[email protected]?body=subscribe%20list>", |
| 310 | + urls: []*url.URL{ |
| 311 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "body=subscribe%20list"}, |
| 312 | + }, |
| 313 | + }, |
| 314 | + { |
| 315 | + header: "List-Unsubscribe", |
| 316 | + raw: "<mailto:[email protected]>", |
| 317 | + urls: []*url.URL{ |
| 318 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 319 | + }, |
| 320 | + }, |
| 321 | + { |
| 322 | + header: "List-Subscribe", |
| 323 | + raw: "<http://www.host.com/list.cgi?cmd=sub&lst=list>,\r\n\t<mailto:[email protected]?subject=subscribe>", |
| 324 | + urls: []*url.URL{ |
| 325 | + {Scheme: "http", Host: "www.host.com", Path: "/list.cgi", RawQuery: "cmd=sub&lst=list"}, |
| 326 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=subscribe"}, |
| 327 | + }, |
| 328 | + }, |
| 329 | + { |
| 330 | + header: "List-Post", |
| 331 | + raw: "<mailto:[email protected]>", |
| 332 | + urls: []*url.URL{ |
| 333 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 334 | + }, |
| 335 | + }, |
| 336 | + { |
| 337 | + header: "List-Post", |
| 338 | + raw: "<mailto:[email protected]> (Postings are Moderated)", |
| 339 | + urls: []*url.URL{ |
| 340 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 341 | + }, |
| 342 | + }, |
| 343 | + { |
| 344 | + header: "List-Post", |
| 345 | + raw: "<mailto:[email protected]?subject=list%20posting>", |
| 346 | + urls: []*url.URL{ |
| 347 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=list%20posting"}, |
| 348 | + }, |
| 349 | + }, |
| 350 | + { |
| 351 | + header: "List-Post", |
| 352 | + raw: "NO (posting not allowed on this list)", |
| 353 | + urls: []*url.URL{nil}, |
| 354 | + }, |
| 355 | + { |
| 356 | + header: "List-Owner", |
| 357 | + raw: "<mailto:[email protected]> (Contact Person for Help)", |
| 358 | + urls: []*url.URL{ |
| 359 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 360 | + }, |
| 361 | + }, |
| 362 | + { |
| 363 | + header: "List-Owner", |
| 364 | + raw: "<mailto:[email protected]> (Grant Neufeld)", |
| 365 | + urls: []*url.URL{ |
| 366 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 367 | + }, |
| 368 | + }, |
| 369 | + { |
| 370 | + header: "List-Owner", |
| 371 | + raw: "<mailto:[email protected]?Subject=list>", |
| 372 | + urls: []*url.URL{ |
| 373 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "Subject=list"}, |
| 374 | + }, |
| 375 | + }, |
| 376 | + { |
| 377 | + header: "List-Archive", |
| 378 | + raw: "<mailto:[email protected]?subject=index%20list>", |
| 379 | + urls: []*url.URL{ |
| 380 | + { Scheme: "mailto", Opaque: "[email protected]", RawQuery: "subject=index%20list"}, |
| 381 | + }, |
| 382 | + }, |
| 383 | + { |
| 384 | + header: "List-Archive", |
| 385 | + raw: "<ftp://ftp.host.com/pub/list/archive/>", |
| 386 | + urls: []*url.URL{ |
| 387 | + {Scheme: "ftp", Host: "ftp.host.com", Path: "/pub/list/archive/"}, |
| 388 | + }, |
| 389 | + }, |
| 390 | + { |
| 391 | + header: "List-Archive", |
| 392 | + raw: "<http://www.host.com/list/archive/> (Web Archive)", |
| 393 | + urls: []*url.URL{ |
| 394 | + {Scheme: "http", Host: "www.host.com", Path: "/list/archive/"}, |
| 395 | + }, |
| 396 | + }, |
| 397 | + } |
| 398 | + |
| 399 | + for _, test := range tests { |
| 400 | + var h mail.Header |
| 401 | + h.Set(test.header, test.raw) |
| 402 | + |
| 403 | + urls, err := h.ListCommandURLList(test.header) |
| 404 | + if err != nil && !test.xfail { |
| 405 | + t.Errorf("Failed to parse %s %q: Header.ListCommandURLList() = %v", test.header, test.raw, err) |
| 406 | + } else if !reflect.DeepEqual(urls, test.urls) { |
| 407 | + t.Errorf("Failed to parse %s %q: Header.ListCommandURLList() = %q, want %q", test.header, test.raw, urls, test.urls) |
| 408 | + } |
| 409 | + } |
| 410 | +} |
| 411 | + |
| 412 | +func TestHeader_SetListCommandURLList(t *testing.T) { |
| 413 | + tests := []struct { |
| 414 | + raw string |
| 415 | + urls []*url.URL |
| 416 | + }{ |
| 417 | + { |
| 418 | + raw: "<mailto:[email protected]>", |
| 419 | + urls: []*url.URL{ |
| 420 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 421 | + }, |
| 422 | + }, |
| 423 | + { |
| 424 | + raw: "<mailto:[email protected]>, <https://example.com:8080>", |
| 425 | + urls: []*url.URL{ |
| 426 | + { Scheme: "mailto", Opaque: "[email protected]"}, |
| 427 | + {Scheme: "https", Host: "example.com:8080"}, |
| 428 | + }, |
| 429 | + }, |
| 430 | + } |
| 431 | + for _, test := range tests { |
| 432 | + var h mail.Header |
| 433 | + h.SetListCommandURLList("List-Post", test.urls) |
| 434 | + raw := h.Get("List-Post") |
| 435 | + if raw != test.raw { |
| 436 | + t.Errorf("Failed to format List-Post %q: Header.Get() = %q, want %q", test.urls, raw, test.raw) |
| 437 | + } |
| 438 | + } |
| 439 | +} |
0 commit comments