Skip to content

Commit

Permalink
aws/request Fixes WithSetRequestHeaders key added to header map dir…
Browse files Browse the repository at this point in the history
…ectly (#4372)

Addresses an issue where the header keys being added were being added
directly to the header map, and did not have the canonical header casing
applied. This introduced bugs where instead of overwriting existing
header key, it added another map entry.
  • Loading branch information
jasdel authored Apr 25, 2022
1 parent 83cf2e6 commit 181965c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### SDK Features
* `aws/request`: Fixes bug in WithSetRequestHeaders where the header key was added to the header map directly
* Addresses an issue where the header keys being added were being added directly to the header map, and did not have the canonical header casing applied. This introduced bugs where instead of overwriting existing header key, it added another map entry.

### SDK Enhancements

Expand Down
5 changes: 4 additions & 1 deletion aws/request/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ func MakeAddToUserAgentFreeFormHandler(s string) func(*Request) {
// WithSetRequestHeaders updates the operation request's HTTP header to contain
// the header key value pairs provided. If the header key already exists in the
// request's HTTP header set, the existing value(s) will be replaced.
//
// Header keys added will be added as canonical format with title casing
// applied via http.Header.Set method.
func WithSetRequestHeaders(h map[string]string) Option {
return withRequestHeader(h).SetRequestHeaders
}
Expand All @@ -338,6 +341,6 @@ type withRequestHeader map[string]string

func (h withRequestHeader) SetRequestHeaders(r *Request) {
for k, v := range h {
r.HTTPRequest.Header[k] = []string{v}
r.HTTPRequest.Header.Set(k, v)
}
}
34 changes: 34 additions & 0 deletions aws/request/handlers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package request_test

import (
"net/http"
"reflect"
"testing"

Expand Down Expand Up @@ -197,6 +198,39 @@ func TestStopHandlers(t *testing.T) {
}
}

func TestWithSetRequestHeaders(t *testing.T) {
fn := request.WithSetRequestHeaders(map[string]string{
"x-foo-bar": "abc123",
"X-Bar-foo": "efg456",
})

req := &request.Request{HTTPRequest: &http.Request{Header: http.Header{}}}
fn(req)

expect := map[string][]string{
"X-Foo-Bar": {"abc123"},
"X-Bar-Foo": {"efg456"},
}

if e, a := len(req.HTTPRequest.Header), len(expect); e != a {
t.Fatalf("expect %v headers, got %v", e, a)
}
for k, expectVs := range expect {
actualVs, ok := req.HTTPRequest.Header[k]
if !ok {
t.Errorf("expect %v header", k)
}
if e, a := len(expectVs), len(actualVs); e != a {
t.Fatalf("expect %v values for %v, got %v", e, k, a)
}
for i, expectV := range expectVs {
if e, a := expectV, actualVs[i]; e != a {
t.Errorf("expect %v[%d] to be %v, got %v", k, i, e, a)
}
}
}
}

func BenchmarkNewRequest(b *testing.B) {
svc := s3.New(unit.Session)

Expand Down

0 comments on commit 181965c

Please sign in to comment.