mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-09 15:32:54 +01:00
Closes #60. Added final test coverage and an additional SetContent method
This commit is contained in:
parent
dbe58745be
commit
15caea0181
2 changed files with 128 additions and 0 deletions
6
part.go
6
part.go
|
@ -43,6 +43,12 @@ func (p *Part) GetWriteFunc() func(io.Writer) (int64, error) {
|
|||
return p.w
|
||||
}
|
||||
|
||||
// SetContent overrides the content of the Part with the given string
|
||||
func (p *Part) SetContent(c string) {
|
||||
buf := bytes.NewBufferString(c)
|
||||
p.w = writeFuncFromBuffer(buf)
|
||||
}
|
||||
|
||||
// SetContentType overrides the ContentType of the Part
|
||||
func (p *Part) SetContentType(c ContentType) {
|
||||
p.ctype = c
|
||||
|
|
122
part_test.go
122
part_test.go
|
@ -7,6 +7,8 @@ package mail
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -43,6 +45,35 @@ func TestPartEncoding(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestPartContentType tests Part.SetContentType
|
||||
func TestPart_SetContentType(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ct ContentType
|
||||
want string
|
||||
}{
|
||||
{"ContentType: text/plain", TypeTextPlain, "text/plain"},
|
||||
{"ContentType: text/html", TypeTextHTML, "text/html"},
|
||||
{"ContentType: application/json", "application/json", "application/json"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m := NewMsg()
|
||||
m.SetBodyString(TypeTextPlain, "This is a test with ümläutß")
|
||||
pl, err := getPartList(m)
|
||||
if err != nil {
|
||||
t.Errorf("failed: %s", err)
|
||||
return
|
||||
}
|
||||
pl[0].SetContentType(tt.ct)
|
||||
ct := pl[0].GetContentType()
|
||||
if string(ct) != tt.want {
|
||||
t.Errorf("SetContentType failed. Got: %s, expected: %s", string(ct), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPartEncoding tests Part.GetEncoding
|
||||
func TestPart_GetEncoding(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
@ -119,6 +150,97 @@ func TestPart_GetWriteFunc(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestPart_GetContent tests Part.GetContent
|
||||
func TestPart_GetContent(t *testing.T) {
|
||||
c := "This is a test with ümläutß"
|
||||
m := NewMsg()
|
||||
m.SetBodyString(TypeTextPlain, c)
|
||||
pl, err := getPartList(m)
|
||||
if err != nil {
|
||||
t.Errorf("failed: %s", err)
|
||||
return
|
||||
}
|
||||
cb, err := pl[0].GetContent()
|
||||
if err != nil {
|
||||
t.Errorf("Part.GetContent failed: %s", err)
|
||||
}
|
||||
if string(cb) != c {
|
||||
t.Errorf("Part.GetContent failed. Expected: %s, got: %s", c, string(cb))
|
||||
}
|
||||
}
|
||||
|
||||
// TestPart_GetContentBroken tests Part.GetContent
|
||||
func TestPart_GetContentBroken(t *testing.T) {
|
||||
c := "This is a test with ümläutß"
|
||||
m := NewMsg()
|
||||
m.SetBodyString(TypeTextPlain, c)
|
||||
pl, err := getPartList(m)
|
||||
if err != nil {
|
||||
t.Errorf("failed: %s", err)
|
||||
return
|
||||
}
|
||||
pl[0].w = func(io.Writer) (int64, error) {
|
||||
return 0, fmt.Errorf("broken")
|
||||
}
|
||||
_, err = pl[0].GetContent()
|
||||
if err == nil {
|
||||
t.Errorf("Part.GetContent was supposed to failed, but didn't")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPart_SetWriteFunc tests Part.SetWriteFunc
|
||||
func TestPart_SetWriteFunc(t *testing.T) {
|
||||
c := "This is a test with ümläutß"
|
||||
m := NewMsg()
|
||||
m.SetBodyString(TypeTextPlain, c)
|
||||
pl, err := getPartList(m)
|
||||
if err != nil {
|
||||
t.Errorf("failed: %s", err)
|
||||
return
|
||||
}
|
||||
cb, err := pl[0].GetContent()
|
||||
if err != nil {
|
||||
t.Errorf("Part.GetContent failed: %s", err)
|
||||
}
|
||||
pl[0].SetWriteFunc(func(w io.Writer) (int64, error) {
|
||||
ns := strings.ToUpper(string(cb))
|
||||
buf := bytes.NewBufferString(ns)
|
||||
nb, err := w.Write(buf.Bytes())
|
||||
return int64(nb), err
|
||||
})
|
||||
nc, err := pl[0].GetContent()
|
||||
if err != nil {
|
||||
t.Errorf("Part.GetContent failed: %s", err)
|
||||
}
|
||||
if string(nc) != strings.ToUpper(c) {
|
||||
t.Errorf("SetWriteFunc failed. Expected: %s, got: %s", strings.ToUpper(c), string(nc))
|
||||
}
|
||||
}
|
||||
|
||||
// TestPart_SetContent tests Part.SetContent
|
||||
func TestPart_SetContent(t *testing.T) {
|
||||
c := "This is a test with ümläutß"
|
||||
m := NewMsg()
|
||||
m.SetBodyString(TypeTextPlain, c)
|
||||
pl, err := getPartList(m)
|
||||
if err != nil {
|
||||
t.Errorf("failed: %s", err)
|
||||
return
|
||||
}
|
||||
cb, err := pl[0].GetContent()
|
||||
if err != nil {
|
||||
t.Errorf("Part.GetContent failed: %s", err)
|
||||
}
|
||||
pl[0].SetContent(strings.ToUpper(string(cb)))
|
||||
nc, err := pl[0].GetContent()
|
||||
if err != nil {
|
||||
t.Errorf("Part.GetContent failed: %s", err)
|
||||
}
|
||||
if string(nc) != strings.ToUpper(c) {
|
||||
t.Errorf("SetContent failed. Expected: %s, got: %s", strings.ToUpper(c), string(nc))
|
||||
}
|
||||
}
|
||||
|
||||
// getPartList is a helper function
|
||||
func getPartList(m *Msg) ([]*Part, error) {
|
||||
pl := m.GetParts()
|
||||
|
|
Loading…
Reference in a new issue