-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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/request: Fix for Go 1.8 request incorrectly sent with body #991
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e66ba2a
aws/request: Fix for Go 1.8 request incorrectly sent with body
jasdel 6b1f0ee
add docs
jasdel bdda5b9
deprecate aws.ReaderSeekerCloser
jasdel e2d64a0
simplified implementation
jasdel 1ef9dd8
Fix usage of io.Seek constances not added until go 1.7
jasdel 0a88972
ensure the request body is in a good state after signing
jasdel ebae57f
Add tests for signer option to not replace body
jasdel 52df8ab
Add test for exlucde body by method
jasdel c9f262e
Add lifecycle test
jasdel d18e226
Add round trip test for each request method
jasdel 9113cb5
Fixup signer disable overwrite body condition
jasdel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// +build !go1.8 | ||
|
||
package request | ||
|
||
import "io" | ||
|
||
// NoBody is an io.ReadCloser with no bytes. Read always returns EOF | ||
// and Close always returns nil. It can be used in an outgoing client | ||
// request to explicitly signal that a request has zero bytes. | ||
// An alternative, however, is to simply set Request.Body to nil. | ||
// | ||
// Copy of Go 1.8 NoBody type from net/http/http.go | ||
type noBody struct{} | ||
|
||
func (noBody) Read([]byte) (int, error) { return 0, io.EOF } | ||
func (noBody) Close() error { return nil } | ||
func (noBody) WriteTo(io.Writer) (int64, error) { return 0, nil } | ||
|
||
// Is an empty reader that will trigger the Go HTTP client to not include | ||
// and body in the HTTP request. | ||
var noBodyReader = noBody{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build !go1.8 | ||
|
||
package request | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestResetBody_WithEmptyBody(t *testing.T) { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if a, e := r.HTTPRequest.Body, (noBody{}); a != e { | ||
t.Errorf("expected request body to be set to reader, got %#v", r.HTTPRequest.Body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build go1.8 | ||
|
||
package request | ||
|
||
import "net/http" | ||
|
||
// Is a http.NoBody reader instructing Go HTTP client to not include | ||
// and body in the HTTP request. | ||
var noBodyReader = http.NoBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// +build go1.8 | ||
|
||
package request | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestResetBody_WithEmptyBody(t *testing.T) { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if a, e := r.HTTPRequest.Body, http.NoBody; a != e { | ||
t.Errorf("expected request body to be set to reader, got %#v", | ||
r.HTTPRequest.Body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package request | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
) | ||
|
||
func TestResetBody_WithBodyContents(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have tests to test the non-seekable case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will add |
||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
} | ||
|
||
reader := strings.NewReader("abc") | ||
r.Body = reader | ||
|
||
r.ResetBody() | ||
|
||
if v, ok := r.HTTPRequest.Body.(*offsetReader); !ok || v == nil { | ||
t.Errorf("expected request body to be set to reader, got %#v", | ||
r.HTTPRequest.Body) | ||
} | ||
} | ||
|
||
func TestResetBody_ExcludeUnseekableBodyByMethod(t *testing.T) { | ||
cases := []struct { | ||
Method string | ||
IsNoBody bool | ||
}{ | ||
{"GET", true}, | ||
{"HEAD", true}, | ||
{"DELETE", true}, | ||
{"PUT", false}, | ||
{"PATCH", false}, | ||
{"POST", false}, | ||
} | ||
|
||
reader := aws.ReadSeekCloser(bytes.NewBuffer([]byte("abc"))) | ||
|
||
for i, c := range cases { | ||
r := Request{ | ||
HTTPRequest: &http.Request{}, | ||
Operation: &Operation{ | ||
HTTPMethod: c.Method, | ||
}, | ||
} | ||
|
||
r.SetReaderBody(reader) | ||
|
||
if a, e := r.HTTPRequest.Body == noBodyReader, c.IsNoBody; a != e { | ||
t.Errorf("%d, expect body to be set to noBody(%t), but was %t", i, e, a) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this looks a lot better