Skip to content

Commit

Permalink
Merge pull request awslabs#62 from Bryanide/master
Browse files Browse the repository at this point in the history
Handling multi-value request headers
  • Loading branch information
jt0 authored Jun 9, 2020
2 parents c08fff8 + 0f22bb2 commit 9acdb20
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
19 changes: 15 additions & 4 deletions core/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,22 @@ func (r *RequestAccessor) EventToRequest(req events.APIGatewayProxyRequest) (*ht
log.Println(err)
return nil, err
}
for h := range req.Headers {
httpRequest.Header.Add(h, req.Headers[h])

if req.MultiValueHeaders != nil {
for k, values := range req.MultiValueHeaders {
for _, value := range values {
httpRequest.Header.Add(k, value)
}
}
} else {
for h := range req.Headers {
httpRequest.Header.Add(h, req.Headers[h])
}
}
httpRequest.RequestURI = httpRequest.URL.RequestURI()
return httpRequest, nil

httpRequest.RequestURI = httpRequest.URL.RequestURI()

return httpRequest, nil
}

func addToHeader(req *http.Request, apiGwRequest events.APIGatewayProxyRequest) (*http.Request, error) {
Expand Down
39 changes: 39 additions & 0 deletions core/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"math/rand"
"os"
"strings"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambdacontext"
Expand Down Expand Up @@ -117,6 +118,44 @@ var _ = Describe("RequestAccessor tests", func() {
Expect("2").To(Equal(query["world"][0]))
})

mvhRequest := getProxyRequest("/hello", "GET")
mvhRequest.MultiValueHeaders = map[string][]string{
"hello": {"1"},
"world": {"2", "3"},
}
It("Populates multiple value headers correctly", func() {
httpReq, err := accessor.EventToRequestWithContext(context.Background(), mvhRequest)
Expect(err).To(BeNil())
Expect("/hello").To(Equal(httpReq.URL.Path))
Expect("GET").To(Equal(httpReq.Method))

headers := httpReq.Header
Expect(2).To(Equal(len(headers)))

for k, value := range headers {
Expect(value).To(Equal(mvhRequest.MultiValueHeaders[strings.ToLower(k)]))
}
})

svhRequest := getProxyRequest("/hello", "GET")
svhRequest.Headers = map[string]string{
"hello": "1",
"world": "2",
}
It("Populates single value headers correctly", func() {
httpReq, err := accessor.EventToRequestWithContext(context.Background(), svhRequest)
Expect(err).To(BeNil())
Expect("/hello").To(Equal(httpReq.URL.Path))
Expect("GET").To(Equal(httpReq.Method))

headers := httpReq.Header
Expect(2).To(Equal(len(headers)))

for k, value := range headers {
Expect(value[0]).To(Equal(svhRequest.Headers[strings.ToLower(k)]))
}
})

basePathRequest := getProxyRequest("/app1/orders", "GET")

It("Stips the base path correct", func() {
Expand Down

0 comments on commit 9acdb20

Please sign in to comment.