(t *testing.T)
| 71 | } |
| 72 | |
| 73 | func TestEmailWithHTMLAttachments(t *testing.T) { |
| 74 | e := prepareEmail() |
| 75 | |
| 76 | // Set plain text to exercise "mime/alternative" |
| 77 | e.Text = []byte("Text Body is, of course, supported!\n") |
| 78 | |
| 79 | e.HTML = []byte("<html><body>This is a text.</body></html>") |
| 80 | |
| 81 | // Set HTML attachment to exercise "mime/related". |
| 82 | attachment, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "image/png; charset=utf-8") |
| 83 | if err != nil { |
| 84 | t.Fatal("Could not add an attachment to the message: ", err) |
| 85 | } |
| 86 | attachment.HTMLRelated = true |
| 87 | |
| 88 | b, err := e.Bytes() |
| 89 | if err != nil { |
| 90 | t.Fatal("Could not serialize e-mail:", err) |
| 91 | } |
| 92 | |
| 93 | // Print the bytes for ocular validation and make sure no errors. |
| 94 | //fmt.Println(string(b)) |
| 95 | |
| 96 | // TODO: Verify the attachments. |
| 97 | s := trimReader{rd: bytes.NewBuffer(b)} |
| 98 | tp := textproto.NewReader(bufio.NewReader(s)) |
| 99 | // Parse the main headers |
| 100 | hdrs, err := tp.ReadMIMEHeader() |
| 101 | if err != nil { |
| 102 | t.Fatal("Could not parse the headers:", err) |
| 103 | } |
| 104 | // Recursively parse the MIME parts |
| 105 | ps, err := parseMIMEParts(hdrs, tp.R) |
| 106 | if err != nil { |
| 107 | t.Fatal("Could not parse the MIME parts recursively:", err) |
| 108 | } |
| 109 | |
| 110 | plainTextFound := false |
| 111 | htmlFound := false |
| 112 | imageFound := false |
| 113 | if expected, actual := 3, len(ps); actual != expected { |
| 114 | t.Error("Unexpected number of parts. Expected:", expected, "Was:", actual) |
| 115 | } |
| 116 | for _, part := range ps { |
| 117 | // part has "header" and "body []byte" |
| 118 | ct := part.header.Get("Content-Type") |
| 119 | if strings.Contains(ct, "image/png") { |
| 120 | imageFound = true |
| 121 | } |
| 122 | if strings.Contains(ct, "text/html") { |
| 123 | htmlFound = true |
| 124 | } |
| 125 | if strings.Contains(ct, "text/plain") { |
| 126 | plainTextFound = true |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if !plainTextFound { |
nothing calls this directly
no test coverage detected
searching dependent graphs…