From e0a59dba6da226dfae2e8fdded66c1e538afb083 Mon Sep 17 00:00:00 2001 From: theexiile1305 Date: Wed, 18 Sep 2024 14:17:40 +0200 Subject: [PATCH] introduced content disposition in part --- part.go | 18 ++++++++++++++++++ part_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/part.go b/part.go index 7c76b7d..005da29 100644 --- a/part.go +++ b/part.go @@ -17,6 +17,7 @@ type Part struct { contentType ContentType charset Charset description string + disposition Disposition encoding Encoding isDeleted bool writeFunc func(io.Writer) (int64, error) @@ -56,6 +57,11 @@ func (p *Part) GetDescription() string { return p.description } +// GetDisposition returns the currently set Content-Disposition of the Part +func (p *Part) GetDisposition() Disposition { + return p.disposition +} + // SetContent overrides the content of the Part with the given string func (p *Part) SetContent(content string) { buffer := bytes.NewBufferString(content) @@ -82,6 +88,11 @@ func (p *Part) SetDescription(description string) { p.description = description } +// SetDisposition overrides the Content-Disposition of the Part +func (p *Part) SetDisposition(disposition Disposition) { + p.disposition = disposition +} + // SetWriteFunc overrides the WriteFunc of the Part func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) { p.writeFunc = writeFunc @@ -113,3 +124,10 @@ func WithPartContentDescription(description string) PartOption { p.description = description } } + +// WithContentDisposition overrides the default Part Content-Disposition +func WithContentDisposition(disposition Disposition) PartOption { + return func(p *Part) { + p.disposition = disposition + } +} diff --git a/part_test.go b/part_test.go index a196986..800acff 100644 --- a/part_test.go +++ b/part_test.go @@ -102,6 +102,35 @@ func TestPart_WithPartContentDescription(t *testing.T) { } } +// TestPart_withContentDisposition tests the WithContentDisposition method +func TestPart_withContentDisposition(t *testing.T) { + tests := []struct { + name string + disposition Disposition + }{ + {"Part disposition: test", "test"}, + {"Part disposition: empty", ""}, + } + for _, tt := range tests { + m := NewMsg() + t.Run(tt.name, func(t *testing.T) { + part := m.newPart(TypeTextPlain, WithContentDisposition(tt.disposition), nil) + if part == nil { + t.Errorf("newPart() WithPartContentDescription() failed: no part returned") + return + } + if part.disposition != tt.disposition { + t.Errorf("newPart() WithContentDisposition() failed: expected: %s, got: %s", tt.disposition, part.description) + } + part.disposition = "" + part.SetDisposition(tt.disposition) + if part.disposition != tt.disposition { + t.Errorf("newPart() SetDisposition() failed: expected: %s, got: %s", tt.disposition, part.description) + } + }) + } +} + // TestPartContentType tests Part.SetContentType func TestPart_SetContentType(t *testing.T) { tests := []struct { @@ -323,6 +352,31 @@ func TestPart_SetDescription(t *testing.T) { } } +// TestPart_SetDisposition tests Part.SetDisposition +func TestPart_SetDisposition(t *testing.T) { + c := "This is a test" + d := Disposition("test-disposition") + m := NewMsg() + m.SetBodyString(TypeTextPlain, c) + pl, err := getPartList(m) + if err != nil { + t.Errorf("failed: %s", err) + return + } + pd := pl[0].GetDisposition() + if pd != "" { + t.Errorf("Part.GetDisposition failed. Expected empty description but got: %s", pd) + } + pl[0].SetDisposition(d) + if pl[0].disposition != d { + t.Errorf("Part.SetDisposition failed. Expected description to be: %s, got: %s", d, pd) + } + pd = pl[0].GetDisposition() + if pd != d { + t.Errorf("Part.GetDisposition failed. Expected: %s, got: %s", d, pd) + } +} + // TestPart_Delete tests Part.Delete func TestPart_Delete(t *testing.T) { c := "This is a test with ümläutß"