Skip to content

Commit

Permalink
aws/config: Update Request Send to always ensure HTTPResponse is not …
Browse files Browse the repository at this point in the history
…nil (#4256)

Updates SDK Request.Send to always ensure HTTPResponse member is not
nil, and its Header and Body fields are not nil as well.

Fixes #4211
  • Loading branch information
jasdel committed Jan 26, 2022
1 parent 37a82ef commit 28b1fd2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Expand Up @@ -3,3 +3,5 @@
### SDK Enhancements

### SDK Bugs
* `aws/request`: Update Request Send to always ensure Request.HTTPResponse is populated.
* Fixes [#4211](https://github.com/aws/aws-sdk-go/issues/4211)
9 changes: 9 additions & 0 deletions aws/request/request.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -525,6 +526,14 @@ func (r *Request) GetBody() io.ReadSeeker {
// Send will not close the request.Request's body.
func (r *Request) Send() error {
defer func() {
// Ensure a non-nil HTTPResponse parameter is set to ensure handlers
// checking for HTTPResponse values, don't fail.
if r.HTTPResponse == nil {
r.HTTPResponse = &http.Response{
Header: http.Header{},
Body: ioutil.NopCloser(&bytes.Buffer{}),
}
}
// Regardless of success or failure of the request trigger the Complete
// request handlers.
r.Handlers.Complete.Run(r)
Expand Down
31 changes: 31 additions & 0 deletions aws/request/request_test.go
Expand Up @@ -1257,6 +1257,37 @@ func TestRequestMarshaledEndpointWithNonDefaultPort(t *testing.T) {
}
}

func TestRequestCompleteWithoutHTTPResponse(t *testing.T) {
s := awstesting.NewClient(aws.NewConfig().WithRegion("mock-region"))
r := s.NewRequest(&request.Operation{
Name: "FooBar",
HTTPMethod: "GET",
HTTPPath: "/",
}, nil, nil)
r.Handlers.Sign.Clear()
r.Handlers.Sign.PushFront(func(r *request.Request) {
r.Error = fmt.Errorf("failed")
})
r.Handlers.Complete.PushBack(func(r *request.Request) {
if r.HTTPResponse == nil {
t.Fatalf("expect HTTPResponse not to be nil")
}
if r.HTTPResponse.Header == nil {
t.Fatalf("expect HTTPResponse.Header not to be nil")
}
if r.HTTPResponse.Body == nil {
t.Fatalf("expect HTTPResponse.Body not to be nil")
}
})
err := r.Send()
if err == nil {
t.Fatalf("expect error, got none")
}
if e, a := "failed", err.Error(); !strings.Contains(a, e) {
t.Errorf("expect %v error in %v", e, a)
}
}

type stubSeekFail struct {
Err error
}
Expand Down

0 comments on commit 28b1fd2

Please sign in to comment.