mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-14 01:42:54 +01:00
Refactor test cases and introduce mock writers in b64linebreaker_test.go
The commit modifies existing test cases in the TestBase64LineBreaker_WriteAndClose function where it introduces mock writers to better simulate I/O operations. The introduced mock writers are 'mockWriterExcess' and 'mockWriterNewline' which respectively simulate scenarios of data exceeding body length and inclusion of newline.
This commit is contained in:
parent
c0e856f2ad
commit
4d6bca0f65
1 changed files with 48 additions and 23 deletions
|
@ -384,6 +384,11 @@ LjI4MiIgc3R5bGU9ImZpbGw6I2ZmYjI1YztzdHJva2U6IzAwMDtzdHJva2Utd2lkdGg6NC45NXB4
|
||||||
OyIvPjwvZz48L3N2Zz4=
|
OyIvPjwvZz48L3N2Zz4=
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var (
|
||||||
|
mockErr = errors.New("mock write error")
|
||||||
|
mockNewlineErr = errors.New("mock newline error")
|
||||||
|
)
|
||||||
|
|
||||||
// TestBase64LineBreaker tests the Write and Close methods of the Base64LineBreaker
|
// TestBase64LineBreaker tests the Write and Close methods of the Base64LineBreaker
|
||||||
func TestBase64LineBreaker(t *testing.T) {
|
func TestBase64LineBreaker(t *testing.T) {
|
||||||
l, err := os.Open("assets/gopher2.svg")
|
l, err := os.Open("assets/gopher2.svg")
|
||||||
|
@ -442,37 +447,38 @@ func TestBase64LineBreaker_WriteAndClose(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
data []byte
|
data []byte
|
||||||
expectedWrite string
|
writer io.Writer
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Write data within MaxBodyLength",
|
name: "Write data within MaxBodyLength",
|
||||||
data: []byte("testdata"),
|
data: []byte("testdata"),
|
||||||
expectedWrite: "testdata",
|
writer: &mockWriterExcess{writeError: mockErr},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Write data exceeds MaxBodyLength",
|
name: "Write data exceeds MaxBodyLength",
|
||||||
data: []byte("verylongtestdata"),
|
data: []byte("verylongtestdataverylongtestdataverylongtestdata" +
|
||||||
expectedWrite: "verylongtest",
|
"verylongtestdataverylongtestdataverylongtestdata"),
|
||||||
|
writer: &mockWriterExcess{writeError: mockErr},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Write data exceeds MaxBodyLength with newline",
|
||||||
|
data: []byte("verylongtestdataverylongtestdataverylongtestdata" +
|
||||||
|
"verylongtestdataverylongtestdataverylongtestdata"),
|
||||||
|
writer: &mockWriterNewline{writeError: mockErr},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var mockErr = errors.New("mock write error")
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
var buf bytes.Buffer
|
blr := &Base64LineBreaker{out: tt.writer}
|
||||||
blr := &Base64LineBreaker{out: &buf}
|
|
||||||
mw := &mockWriter{writeError: mockErr}
|
|
||||||
blr.out = mw
|
|
||||||
|
|
||||||
_, err := blr.Write(tt.data)
|
_, err := blr.Write(tt.data)
|
||||||
if err != nil && !errors.Is(err, mockErr) {
|
if err != nil && !errors.Is(err, mockErr) && !errors.Is(err, mockNewlineErr) {
|
||||||
t.Errorf("Unexpected error while writing: %v", err)
|
t.Errorf("Unexpected error while writing: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
err = blr.Close()
|
err = blr.Close()
|
||||||
if err != nil && !errors.Is(err, mockErr) {
|
if err != nil && !errors.Is(err, mockErr) && !errors.Is(err, mockNewlineErr) {
|
||||||
t.Errorf("Unexpected error while closing: %v", err)
|
t.Errorf("Unexpected error while closing: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -504,14 +510,33 @@ func (e errorWriter) Close() error {
|
||||||
return fmt.Errorf("supposed to always fail")
|
return fmt.Errorf("supposed to always fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MockWriter is a mock implementation of io.Writer used for testing.
|
type mockWriterExcess struct {
|
||||||
type mockWriter struct {
|
writeError error
|
||||||
|
}
|
||||||
|
type mockWriterNewline struct {
|
||||||
writeError error
|
writeError error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes the data into a buffer.
|
func (w *mockWriterExcess) Write(p []byte) (n int, err error) {
|
||||||
func (w *mockWriter) Write(p []byte) (n int, err error) {
|
switch len(p) {
|
||||||
return 0, w.writeError
|
case 0:
|
||||||
|
return 0, nil
|
||||||
|
case 2:
|
||||||
|
return 2, nil
|
||||||
|
default:
|
||||||
|
return len(p), mockErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *mockWriterNewline) Write(p []byte) (n int, err error) {
|
||||||
|
switch len(p) {
|
||||||
|
case 0:
|
||||||
|
return 0, nil
|
||||||
|
case 2:
|
||||||
|
return 2, mockNewlineErr
|
||||||
|
default:
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FuzzBase64LineBreaker_Write(f *testing.F) {
|
func FuzzBase64LineBreaker_Write(f *testing.F) {
|
||||||
|
|
Loading…
Reference in a new issue