MCPcopy
hub / github.com/jordan-wright/email / TestEmailWithHTMLAttachments

Function TestEmailWithHTMLAttachments

email_test.go:73–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

71}
72
73func 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 {

Callers

nothing calls this directly

Calls 4

prepareEmailFunction · 0.85
parseMIMEPartsFunction · 0.85
AttachMethod · 0.80
BytesMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…