Finalized the Reader type and removed the broken Read() method

This commit is contained in:
Winni Neessen 2022-10-20 18:26:51 +02:00
parent 183fb347d6
commit b6beeb5cae
Signed by: wneessen
GPG key ID: 385AC9889632126E
4 changed files with 75 additions and 84 deletions

10
msg.go
View file

@ -840,16 +840,6 @@ func (m *Msg) UpdateReader(r *Reader) {
r.err = err r.err = err
} }
// Read outputs the length of p into p to satisfy the io.Reader interface
func (m *Msg) Read(p []byte) (int, error) {
wbuf := bytes.Buffer{}
_, err := m.WriteTo(&wbuf)
if err != nil {
return 0, fmt.Errorf("failed to write message to internal write buffer: %w", err)
}
return wbuf.Read(p)
}
// encodeString encodes a string based on the configured message encoder and the corresponding // encodeString encodes a string based on the configured message encoder and the corresponding
// charset for the Msg // charset for the Msg
func (m *Msg) encodeString(s string) string { func (m *Msg) encodeString(s string) string {

View file

@ -1718,72 +1718,6 @@ func TestMsg_multipleWrites(t *testing.T) {
} }
} }
// TestMsg_Read tests the Msg.Read method that implements the io.Reader interface
func TestMsg_Read(t *testing.T) {
tests := []struct {
name string
plen int
}{
{"P length is bigger than the mail", 32000},
{"P length is smaller than the mail", 128},
}
m := NewMsg()
m.SetBodyString(TypeTextPlain, "TEST123")
wbuf := bytes.Buffer{}
_, err := m.Write(&wbuf)
if err != nil {
t.Errorf("failed to write message into temporary buffer: %s", err)
}
elen := wbuf.Len()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := make([]byte, tt.plen)
n, err := m.Read(p)
if err != nil {
t.Errorf("failed to Read(): %s", err)
}
if n == 0 {
t.Errorf("failed to Read() - received 0 bytes of data")
}
if tt.plen >= elen && n != elen {
t.Errorf("failed to Read() - not all data received. Expected: %d, got: %d", elen, n)
}
if tt.plen < elen && n != tt.plen {
t.Errorf("failed to Read() - full length of p wasn't filled with data. Expected: %d, got: %d",
tt.plen, n)
}
})
}
}
// TestMsg_Read_ioCopy tests the Msg.Read method using io.Copy
func TestMsg_Read_ioCopy(t *testing.T) {
wbuf1 := bytes.Buffer{}
wbuf2 := bytes.Buffer{}
m := NewMsg()
m.SetBodyString(TypeTextPlain, "TEST123")
// First we use WriteTo to have something to compare to
_, err := m.WriteTo(&wbuf1)
if err != nil {
t.Errorf("failed to write body to buffer: %s", err)
}
// Then we write to wbuf2 via io.Copy
n, err := io.Copy(&wbuf2, m)
if err != nil {
t.Errorf("failed to use io.Copy on Msg: %s", err)
}
if n != int64(wbuf1.Len()) {
t.Errorf("message length of WriteTo and io.Copy differ. Expected: %d, got: %d", wbuf1.Len(), n)
}
if wbuf1.String() != wbuf2.String() {
t.Errorf("message content of WriteTo and io.Copy differ")
}
}
// TestMsg_NewReader tests the Msg.NewReader method // TestMsg_NewReader tests the Msg.NewReader method
func TestMsg_NewReader(t *testing.T) { func TestMsg_NewReader(t *testing.T) {
m := NewMsg() m := NewMsg()
@ -1792,14 +1726,8 @@ func TestMsg_NewReader(t *testing.T) {
if mr == nil { if mr == nil {
t.Errorf("NewReader failed: Reader is nil") t.Errorf("NewReader failed: Reader is nil")
} }
} if mr.Error() != nil {
t.Errorf("NewReader failed: %s", mr.Error())
// TestMsg_NewReader_broken tests the Msg.NewReader method error handling
func TestMsg_NewReader_broken(t *testing.T) {
m := &Msg{}
mr := m.NewReader()
if mr == nil {
t.Errorf("NewReader failed: Reader is nil")
} }
} }

View file

@ -15,6 +15,11 @@ type Reader struct {
err error // initalization error err error // initalization error
} }
// Error returns an error if the Reader err field is not nil
func (r *Reader) Error() error {
return r.err
}
// Read reads the length of p of the Msg buffer to satisfy the io.Reader interface // Read reads the length of p of the Msg buffer to satisfy the io.Reader interface
func (r *Reader) Read(p []byte) (n int, err error) { func (r *Reader) Read(p []byte) (n int, err error) {
if r.err != nil { if r.err != nil {

68
reader_test.go Normal file
View file

@ -0,0 +1,68 @@
package mail
import (
"bytes"
"fmt"
"testing"
)
// TestReader_Read tests the Reader.Read method that implements the io.Reader interface
func TestReader_Read(t *testing.T) {
tests := []struct {
name string
plen int
}{
{"P length is bigger than the mail", 3200000},
{"P length is smaller than the mail", 128},
}
m := NewMsg()
m.SetBodyString(TypeTextPlain, "TEST123")
wbuf := bytes.Buffer{}
_, err := m.Write(&wbuf)
if err != nil {
t.Errorf("failed to write message into temporary buffer: %s", err)
}
elen := wbuf.Len()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := make([]byte, tt.plen)
mr := m.NewReader()
n, err := mr.Read(p)
if err != nil {
t.Errorf("failed to Read(): %s", err)
}
if n == 0 {
t.Errorf("failed to Read() - received 0 bytes of data")
}
if tt.plen >= elen && n != elen {
t.Errorf("failed to Read() - not all data received. Expected: %d, got: %d", elen, n)
}
if tt.plen < elen && n != tt.plen {
t.Errorf("failed to Read() - full length of p wasn't filled with data. Expected: %d, got: %d",
tt.plen, n)
}
})
}
}
// TestReader_Read_error tests the Reader.Read method with an intentional error
func TestReader_Read_error(t *testing.T) {
r := Reader{err: fmt.Errorf("FAILED")}
var p []byte
_, err := r.Read(p)
if err == nil {
t.Errorf("Reader was supposed to fail, but didn't")
}
}
// TestReader_Read_empty tests the Reader.Read method with an empty buffer
func TestReader_Read_empty(t *testing.T) {
r := Reader{buf: []byte{}}
var p []byte
_, err := r.Read(p)
if err != nil {
t.Errorf("Reader failed: %s", err)
}
}