Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix byte buffer being cleared during replay #690

Merged
merged 1 commit into from Mar 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 10 additions & 13 deletions autorest/mocks/mocks.go
Expand Up @@ -26,40 +26,37 @@ import (

// Body implements acceptable body over a string.
type Body struct {
s string
b []byte
src []byte
buf []byte
isOpen bool
closeAttempts int
}

// NewBody creates a new instance of Body.
func NewBody(s string) *Body {
return (&Body{s: s}).reset()
return (&Body{src: []byte(s)}).reset()
}

// NewBodyWithBytes creates a new instance of Body.
func NewBodyWithBytes(b []byte) *Body {
return &Body{
b: b,
isOpen: true,
}
return (&Body{src: b}).reset()
}

// NewBodyClose creates a new instance of Body.
func NewBodyClose(s string) *Body {
return &Body{s: s}
return &Body{src: []byte(s)}
}

// Read reads into the passed byte slice and returns the bytes read.
func (body *Body) Read(b []byte) (n int, err error) {
if !body.IsOpen() {
return 0, fmt.Errorf("ERROR: Body has been closed")
}
if len(body.b) == 0 {
if len(body.buf) == 0 {
return 0, io.EOF
}
n = copy(b, body.b)
body.b = body.b[n:]
n = copy(b, body.buf)
body.buf = body.buf[n:]
return n, nil
}

Expand All @@ -84,7 +81,7 @@ func (body *Body) IsOpen() bool {

func (body *Body) reset() *Body {
body.isOpen = true
body.b = []byte(body.s)
body.buf = body.src
return body
}

Expand All @@ -93,7 +90,7 @@ func (body *Body) Length() int64 {
if body == nil {
return 0
}
return int64(len(body.b))
return int64(len(body.src))
}

type response struct {
Expand Down