base: improve coverage

This commit is contained in:
aler9
2021-05-21 18:52:55 +02:00
parent 9a7d0c56fa
commit ce83edbed1
6 changed files with 139 additions and 7 deletions

View File

@@ -3,11 +3,25 @@ package base
import (
"bufio"
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type limitedBuffer struct {
cap int
n int
}
func (b *limitedBuffer) Write(p []byte) (int, error) {
b.n += len(p)
if b.n > b.cap {
return 0, fmt.Errorf("capacity reached")
}
return len(p), nil
}
var casesBody = []struct {
name string
h Header
@@ -90,3 +104,9 @@ func TestBodyReadErrors(t *testing.T) {
})
}
}
func TestBodyWriteErrors(t *testing.T) {
bw := bufio.NewWriterSize(&limitedBuffer{cap: 3}, 1)
err := body([]byte("1234")).write(bw)
require.Equal(t, "capacity reached", err.Error())
}