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

aws/config: Update Request Send to always ensure HTTPResponse is not nil #4256

Merged
merged 1 commit into from Jan 26, 2022
Merged
Show file tree
Hide file tree
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
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