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

New Preparer/Responder for Unmarshalling Bytes #407

Merged
merged 4 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions autorest/mocks/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ func NewResponse() *http.Response {
return NewResponseWithContent("")
}

// NewResponseWithBytes instantiates a new response with the passed bytes as the body content.
func NewResponseWithBytes(input []byte) *http.Response {
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Body: NewBodyWithBytes(input),
Request: NewRequest(),
}
}

// NewResponseWithContent instantiates a new response with the passed string as the body content.
func NewResponseWithContent(c string) *http.Response {
return &http.Response{
Expand Down
5 changes: 5 additions & 0 deletions autorest/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func NewBody(s string) *Body {
return (&Body{s: s}).reset()
}

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

// NewBodyClose creates a new instance of Body.
func NewBodyClose(s string) *Body {
return &Body{s: s}
Expand Down
19 changes: 19 additions & 0 deletions autorest/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ func WithBaseURL(baseURL string) PrepareDecorator {
}
}

// WithBytes returns a PrepareDecorator that takes a list of bytes
// which passes the bytes directly to the body
func WithBytes(input *[]byte) PrepareDecorator {
return func(p Preparer) Preparer {
return PreparerFunc(func(r *http.Request) (*http.Request, error) {
r, err := p.Prepare(r)
if err == nil {
if input == nil {
return r, fmt.Errorf("Input Bytes was nil")
}

r.ContentLength = int64(len(*input))
r.Body = ioutil.NopCloser(bytes.NewReader(*input))
}
return r, err
})
}
}

// WithCustomBaseURL returns a PrepareDecorator that replaces brace-enclosed keys within the
// request base URL (i.e., http.Request.URL) with the corresponding values from the passed map.
func WithCustomBaseURL(baseURL string, urlParameters map[string]interface{}) PrepareDecorator {
Expand Down
24 changes: 24 additions & 0 deletions autorest/preparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ func ExampleWithBaseURL_second() {
// Output: parse :: missing protocol scheme
}

// Create a request whose Body is a byte array
func TestWithBytes(t *testing.T) {
input := []byte{41, 82, 109}

r, err := Prepare(&http.Request{},
WithBytes(&input))
if err != nil {
t.Fatalf("ERROR: %v\n", err)
}

b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatalf("ERROR: %v\n", err)
}

if len(b) != len(input) {
t.Fatalf("Expected the Body to contain %d bytes but got %d", len(input), len(b))
}

if !reflect.DeepEqual(b, input) {
t.Fatalf("Body doesn't contain the same bytes: %s (Expected %s)", b, input)
}
}

func ExampleWithCustomBaseURL() {
r, err := Prepare(&http.Request{},
WithCustomBaseURL("https://{account}.{service}.core.windows.net/",
Expand Down
19 changes: 19 additions & 0 deletions autorest/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ func ByClosingIfError() RespondDecorator {
}
}

// ByUnmarshallingBytes returns a RespondDecorator that copies the Bytes returned in the
// response Body into the value pointed to by v.
func ByUnmarshallingBytes(v *[]byte) RespondDecorator {
return func(r Responder) Responder {
return ResponderFunc(func(resp *http.Response) error {
err := r.Respond(resp)
if err == nil {
bytes, errInner := ioutil.ReadAll(resp.Body)
if errInner != nil {
err = fmt.Errorf("Error occurred reading http.Response#Body - Error = '%v'", errInner)
} else {
copy(*v, bytes)
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved
}
}
return err
})
}
}

// ByUnmarshallingJSON returns a RespondDecorator that decodes a JSON document returned in the
// response Body into the value pointed to by v.
func ByUnmarshallingJSON(v interface{}) RespondDecorator {
Expand Down
19 changes: 19 additions & 0 deletions autorest/responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ func ExampleWithErrorUnlessOK() {
// Output: GET of https://microsoft.com/a/b/c/ returned HTTP 200
}

func TestByUnmarshallingBytes(t *testing.T) {
expected := []byte("Lorem Ipsum Dolor")

// we'll create a fixed-sized array here, since that's the expectation
var bytes = make([]byte, len(expected))

Respond(mocks.NewResponseWithBytes(expected),
ByUnmarshallingBytes(&bytes),
ByClosing())

if len(bytes) != len(expected) {
t.Fatalf("Expected Response to be %d bytes but got %d bytes", len(expected), len(bytes))
}

if !reflect.DeepEqual(expected, bytes) {
t.Fatalf("Expected Response to be %s but got %s", expected, bytes)
}
}

func ExampleByUnmarshallingJSON() {
c := `
{
Expand Down