Skip to content

Commit 181965c

Browse files
authored
aws/request Fixes WithSetRequestHeaders key added to header map directly (#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.
1 parent 83cf2e6 commit 181965c

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG_PENDING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
### SDK Features
2+
* `aws/request`: Fixes bug in WithSetRequestHeaders where the header key was added to the header map directly
3+
* 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.
24

35
### SDK Enhancements
46

aws/request/handlers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ func MakeAddToUserAgentFreeFormHandler(s string) func(*Request) {
330330
// WithSetRequestHeaders updates the operation request's HTTP header to contain
331331
// the header key value pairs provided. If the header key already exists in the
332332
// request's HTTP header set, the existing value(s) will be replaced.
333+
//
334+
// Header keys added will be added as canonical format with title casing
335+
// applied via http.Header.Set method.
333336
func WithSetRequestHeaders(h map[string]string) Option {
334337
return withRequestHeader(h).SetRequestHeaders
335338
}
@@ -338,6 +341,6 @@ type withRequestHeader map[string]string
338341

339342
func (h withRequestHeader) SetRequestHeaders(r *Request) {
340343
for k, v := range h {
341-
r.HTTPRequest.Header[k] = []string{v}
344+
r.HTTPRequest.Header.Set(k, v)
342345
}
343346
}

aws/request/handlers_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package request_test
22

33
import (
4+
"net/http"
45
"reflect"
56
"testing"
67

@@ -197,6 +198,39 @@ func TestStopHandlers(t *testing.T) {
197198
}
198199
}
199200

201+
func TestWithSetRequestHeaders(t *testing.T) {
202+
fn := request.WithSetRequestHeaders(map[string]string{
203+
"x-foo-bar": "abc123",
204+
"X-Bar-foo": "efg456",
205+
})
206+
207+
req := &request.Request{HTTPRequest: &http.Request{Header: http.Header{}}}
208+
fn(req)
209+
210+
expect := map[string][]string{
211+
"X-Foo-Bar": {"abc123"},
212+
"X-Bar-Foo": {"efg456"},
213+
}
214+
215+
if e, a := len(req.HTTPRequest.Header), len(expect); e != a {
216+
t.Fatalf("expect %v headers, got %v", e, a)
217+
}
218+
for k, expectVs := range expect {
219+
actualVs, ok := req.HTTPRequest.Header[k]
220+
if !ok {
221+
t.Errorf("expect %v header", k)
222+
}
223+
if e, a := len(expectVs), len(actualVs); e != a {
224+
t.Fatalf("expect %v values for %v, got %v", e, k, a)
225+
}
226+
for i, expectV := range expectVs {
227+
if e, a := expectV, actualVs[i]; e != a {
228+
t.Errorf("expect %v[%d] to be %v, got %v", k, i, e, a)
229+
}
230+
}
231+
}
232+
}
233+
200234
func BenchmarkNewRequest(b *testing.B) {
201235
svc := s3.New(unit.Session)
202236

0 commit comments

Comments
 (0)