| // Copyright 2011 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package mail |
| |
| import ( |
| "bytes" |
| "io/ioutil" |
| "reflect" |
| "testing" |
| "time" |
| ) |
| |
| var parseTests = []struct { |
| in string |
| header Header |
| body string |
| }{ |
| { |
| // RFC 5322, Appendix A.1.1 |
| in: `From: John Doe <jdoe@machine.example> |
| To: Mary Smith <mary@example.net> |
| Subject: Saying Hello |
| Date: Fri, 21 Nov 1997 09:55:06 -0600 |
| Message-ID: <1234@local.machine.example> |
| |
| This is a message just to say hello. |
| So, "Hello". |
| `, |
| header: Header{ |
| "From": []string{"John Doe <jdoe@machine.example>"}, |
| "To": []string{"Mary Smith <mary@example.net>"}, |
| "Subject": []string{"Saying Hello"}, |
| "Date": []string{"Fri, 21 Nov 1997 09:55:06 -0600"}, |
| "Message-Id": []string{"<1234@local.machine.example>"}, |
| }, |
| body: "This is a message just to say hello.\nSo, \"Hello\".\n", |
| }, |
| } |
| |
| func TestParsing(t *testing.T) { |
| for i, test := range parseTests { |
| msg, err := ReadMessage(bytes.NewBuffer([]byte(test.in))) |
| if err != nil { |
| t.Errorf("test #%d: Failed parsing message: %v", i, err) |
| continue |
| } |
| if !headerEq(msg.Header, test.header) { |
| t.Errorf("test #%d: Incorrectly parsed message header.\nGot:\n%+v\nWant:\n%+v", |
| i, msg.Header, test.header) |
| } |
| body, err := ioutil.ReadAll(msg.Body) |
| if err != nil { |
| t.Errorf("test #%d: Failed reading body: %v", i, err) |
| continue |
| } |
| bodyStr := string(body) |
| if bodyStr != test.body { |
| t.Errorf("test #%d: Incorrectly parsed message body.\nGot:\n%+v\nWant:\n%+v", |
| i, bodyStr, test.body) |
| } |
| } |
| } |
| |
| func headerEq(a, b Header) bool { |
| if len(a) != len(b) { |
| return false |
| } |
| for k, as := range a { |
| bs, ok := b[k] |
| if !ok { |
| return false |
| } |
| if !reflect.DeepEqual(as, bs) { |
| return false |
| } |
| } |
| return true |
| } |
| |
| func TestDateParsing(t *testing.T) { |
| tests := []struct { |
| dateStr string |
| exp *time.Time |
| }{ |
| // RFC 5322, Appendix A.1.1 |
| { |
| "Fri, 21 Nov 1997 09:55:06 -0600", |
| &time.Time{ |
| Year: 1997, |
| Month: 11, |
| Day: 21, |
| Hour: 9, |
| Minute: 55, |
| Second: 6, |
| Weekday: 5, // Fri |
| ZoneOffset: -6 * 60 * 60, |
| }, |
| }, |
| // RFC5322, Appendix A.6.2 |
| // Obsolete date. |
| { |
| "21 Nov 97 09:55:06 GMT", |
| &time.Time{ |
| Year: 1997, |
| Month: 11, |
| Day: 21, |
| Hour: 9, |
| Minute: 55, |
| Second: 6, |
| Zone: "GMT", |
| }, |
| }, |
| } |
| for _, test := range tests { |
| hdr := Header{ |
| "Date": []string{test.dateStr}, |
| } |
| date, err := hdr.Date() |
| if err != nil { |
| t.Errorf("Failed parsing %q: %v", test.dateStr, err) |
| continue |
| } |
| if !reflect.DeepEqual(date, test.exp) { |
| t.Errorf("Parse of %q: got %+v, want %+v", test.dateStr, date, test.exp) |
| } |
| } |
| } |