introduced content disposition in part

This commit is contained in:
theexiile1305 2024-09-18 14:17:40 +02:00
parent 6fda661dc7
commit e0a59dba6d
No known key found for this signature in database
GPG key ID: A1BDDE98F2BF6E40
2 changed files with 72 additions and 0 deletions

18
part.go
View file

@ -17,6 +17,7 @@ type Part struct {
contentType ContentType contentType ContentType
charset Charset charset Charset
description string description string
disposition Disposition
encoding Encoding encoding Encoding
isDeleted bool isDeleted bool
writeFunc func(io.Writer) (int64, error) writeFunc func(io.Writer) (int64, error)
@ -56,6 +57,11 @@ func (p *Part) GetDescription() string {
return p.description 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 // SetContent overrides the content of the Part with the given string
func (p *Part) SetContent(content string) { func (p *Part) SetContent(content string) {
buffer := bytes.NewBufferString(content) buffer := bytes.NewBufferString(content)
@ -82,6 +88,11 @@ func (p *Part) SetDescription(description string) {
p.description = description 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 // SetWriteFunc overrides the WriteFunc of the Part
func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) { func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) {
p.writeFunc = writeFunc p.writeFunc = writeFunc
@ -113,3 +124,10 @@ func WithPartContentDescription(description string) PartOption {
p.description = description p.description = description
} }
} }
// WithContentDisposition overrides the default Part Content-Disposition
func WithContentDisposition(disposition Disposition) PartOption {
return func(p *Part) {
p.disposition = disposition
}
}

View file

@ -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 // TestPartContentType tests Part.SetContentType
func TestPart_SetContentType(t *testing.T) { func TestPart_SetContentType(t *testing.T) {
tests := []struct { 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 // TestPart_Delete tests Part.Delete
func TestPart_Delete(t *testing.T) { func TestPart_Delete(t *testing.T) {
c := "This is a test with ümläutß" c := "This is a test with ümläutß"