Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 9e75c85

Browse files
author
Arran Ubels
committed
Cookies were incorrectly handled for v2.
1 parent 975761a commit 9e75c85

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

core/requestv2.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (r *RequestAccessorV2) EventToRequest(req events.APIGatewayV2HTTPRequest) (
164164
return nil, err
165165
}
166166

167+
for _, cookie := range req.Cookies {
168+
httpRequest.Header.Add("Cookie", cookie)
169+
}
170+
167171
for headerKey, headerValue := range req.Headers {
168172
for _, val := range strings.Split(headerValue, ",") {
169173
httpRequest.Header.Add(headerKey, strings.Trim(val, " "))

core/requestv2_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ var _ = Describe("RequestAccessorV2 tests", func() {
320320
Expect(myCustomHost).To(Equal("http://" + httpReq.URL.Host))
321321
os.Unsetenv(core.CustomHostVariable)
322322
})
323+
324+
It("handles cookies okay", func() {
325+
basicRequest := getProxyRequestV2("orders", "GET")
326+
basicRequest.Cookies = []string{
327+
"TestCookie=123",
328+
}
329+
accessor := core.RequestAccessorV2{}
330+
httpReq, err := accessor.EventToRequestWithContext(context.Background(), basicRequest)
331+
Expect(err).To(BeNil())
332+
Expect(httpReq.Cookie("TestCookie")).To(gstruct.PointTo(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
333+
"Value": Equal("123"),
334+
})))
335+
})
323336
})
324337
})
325338

core/responsev2.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ func (r *ProxyResponseWriterV2) GetProxyResponse() (events.APIGatewayV2HTTPRespo
102102
}
103103

104104
headers := make(map[string]string)
105+
cookies := make([]string, 0)
105106

106107
for headerKey, headerValue := range http.Header(r.headers) {
108+
if strings.EqualFold("set-cookie", headerKey) {
109+
cookies = append(cookies, headerValue...)
110+
continue
111+
}
107112
headers[headerKey] = strings.Join(headerValue, ",")
108113
}
109114

@@ -112,5 +117,6 @@ func (r *ProxyResponseWriterV2) GetProxyResponse() (events.APIGatewayV2HTTPRespo
112117
Headers: headers,
113118
Body: output,
114119
IsBase64Encoded: isBase64,
120+
Cookies: cookies,
115121
}, nil
116122
}

core/responsev2_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,27 @@ var _ = Describe("ResponseWriterV2 tests", func() {
158158
})
159159

160160
It("Writes multi-value headers correctly", func() {
161+
response := NewProxyResponseWriterV2()
162+
response.Header().Add("Accepts", "foobar")
163+
response.Header().Add("Accepts", "barfoo")
164+
response.Write([]byte("hello"))
165+
proxyResponse, err := response.GetProxyResponse()
166+
Expect(err).To(BeNil())
167+
168+
Expect(2).To(Equal(len(proxyResponse.Headers)))
169+
Expect("foobar,barfoo").To(Equal(proxyResponse.Headers["Accepts"]))
170+
})
171+
172+
It("Writes cookies correctly", func() {
161173
response := NewProxyResponseWriterV2()
162174
response.Header().Add("Set-Cookie", "csrftoken=foobar")
163175
response.Header().Add("Set-Cookie", "session_id=barfoo")
164176
response.Write([]byte("hello"))
165177
proxyResponse, err := response.GetProxyResponse()
166178
Expect(err).To(BeNil())
167179

168-
Expect(2).To(Equal(len(proxyResponse.Headers)))
169-
Expect("csrftoken=foobar,session_id=barfoo").To(Equal(proxyResponse.Headers["Set-Cookie"]))
180+
Expect(2).To(Equal(len(proxyResponse.Cookies)))
181+
Expect(strings.Split("csrftoken=foobar,session_id=barfoo", ",")).To(Equal(proxyResponse.Cookies))
170182
})
171183
})
172184

0 commit comments

Comments
 (0)