From 98a1de6b7d5fc8971a0961338b5d33dfa5b13f8c Mon Sep 17 00:00:00 2001 From: Bryanide Date: Fri, 24 Apr 2020 15:44:54 -0400 Subject: [PATCH] handling multi-value request headers --- core/request.go | 14 ++++++++++++-- core/request_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/core/request.go b/core/request.go index f0bbd13..a13b70c 100644 --- a/core/request.go +++ b/core/request.go @@ -190,9 +190,19 @@ 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]) + } } + return httpRequest, nil } diff --git a/core/request_test.go b/core/request_test.go index 5affd25..82f0439 100644 --- a/core/request_test.go +++ b/core/request_test.go @@ -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" @@ -109,6 +110,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() {