From 15caea01819bba344cd953857ef8faa12df6a9a4 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 11 Oct 2022 19:27:54 +0200 Subject: [PATCH] Closes #60. Added final test coverage and an additional SetContent method --- part.go | 6 +++ part_test.go | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/part.go b/part.go index 0bd0136..e25eeeb 100644 --- a/part.go +++ b/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 diff --git a/part_test.go b/part_test.go index 09f01bf..eeaa611 100644 --- a/part_test.go +++ b/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()