Skip to content

Commit

Permalink
perf(matchers): simplify http.NoBody case
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Jan 17, 2023
1 parent b9e83a2 commit db50b76
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 24 deletions.
12 changes: 9 additions & 3 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ func (m matchRouteKey) String() string {
}

// bodyCopyOnRead copies body content to buf on first Read(), except
// if body is nil. In this case, EOF is returned for each Read() and
// buf stays to nil.
// if body is nil or http.NoBody. In this case, EOF is returned for
// each Read() and buf stays to nil.
type bodyCopyOnRead struct {
body io.ReadCloser
buf []byte
Expand All @@ -480,7 +480,7 @@ func (b *bodyCopyOnRead) rearm() {
}

func (b *bodyCopyOnRead) copy() {
if b.buf == nil && b.body != nil {
if b.buf == nil && b.body != nil && b.body != http.NoBody {
var body bytes.Buffer
io.Copy(&body, b.body) //nolint: errcheck
b.body.Close()
Expand All @@ -500,3 +500,9 @@ func (b *bodyCopyOnRead) Read(p []byte) (n int, err error) {
func (b *bodyCopyOnRead) Close() error {
return nil
}

// Len returns the buffer total length, whatever the Read position in body is.
func (b *bodyCopyOnRead) Len() int {
b.copy()
return len(b.buf)
}
85 changes: 64 additions & 21 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,28 +418,71 @@ func TestBodyCopyOnRead(t *testing.T) {
td.CmpNoError(t, bc.Close())
})

t.Run("nil body", func(t *testing.T) {
bc := httpmock.NewBodyCopyOnRead(nil)

bc.Rearm()
td.CmpNil(t, bc.Buf())

var buf [4]byte
n, err := bc.Read(buf[:])
td.Cmp(t, err, io.EOF)
td.Cmp(t, n, 0)
td.CmpNil(t, bc.Buf())
td.Cmp(t, bc.Body(), nil)

bc.Rearm()

n, err = bc.Read(buf[:])
td.Cmp(t, err, io.EOF)
td.Cmp(t, n, 0)
td.CmpNil(t, bc.Buf())
td.Cmp(t, bc.Body(), nil)
testCases := []struct {
name string
body io.ReadCloser
}{
{
name: "nil body",
},
{
name: "no body",
body: http.NoBody,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
bc := httpmock.NewBodyCopyOnRead(tc.body)

bc.Rearm()
td.CmpNil(t, bc.Buf())

var buf [4]byte
n, err := bc.Read(buf[:])
td.Cmp(t, err, io.EOF)
td.Cmp(t, n, 0)
td.CmpNil(t, bc.Buf())
td.Cmp(t, bc.Body(), tc.body)

bc.Rearm()

n, err = bc.Read(buf[:])
td.Cmp(t, err, io.EOF)
td.Cmp(t, n, 0)
td.CmpNil(t, bc.Buf())
td.Cmp(t, bc.Body(), tc.body)

td.CmpNoError(t, bc.Close())
})
}

td.CmpNoError(t, bc.Close())
t.Run("len", func(t *testing.T) {
testCases := []struct {
name string
bc interface{ Len() int }
expected int
}{
{
name: "nil",
bc: httpmock.NewBodyCopyOnRead(nil),
expected: 0,
},
{
name: "no body",
bc: httpmock.NewBodyCopyOnRead(http.NoBody),
expected: 0,
},
{
name: "filled",
bc: httpmock.NewBodyCopyOnRead(ioutil.NopCloser(bytes.NewReader([]byte(`BODY`)))),
expected: 4,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
td.Cmp(t, tc.bc.Len(), tc.expected)
})
}
})
}

Expand Down

0 comments on commit db50b76

Please sign in to comment.