Skip to content

Commit 437e532

Browse files
authored
Merge pull request #751 from petli/750-return-multiple-cookies
#750: Return cookies through proxy response message to support multiple cookies
2 parents 840aa2f + 32774b7 commit 437e532

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.AspNetCore.Http.Features.Authentication;
1515
using System.Globalization;
1616
using Microsoft.Extensions.Primitives;
17+
using Microsoft.Net.Http.Headers;
1718

1819
namespace Amazon.Lambda.AspNetCoreServer
1920
{
@@ -198,6 +199,14 @@ protected override APIGatewayHttpApiV2ProxyResponse MarshallResponse(IHttpRespon
198199
response.Headers = new Dictionary<string, string>();
199200
foreach (var kvp in responseFeatures.Headers)
200201
{
202+
if (kvp.Key.Equals(HeaderNames.SetCookie, StringComparison.CurrentCultureIgnoreCase))
203+
{
204+
// Cookies must be passed through the proxy response property and not as a
205+
// header to be able to pass back multiple cookies in a single request.
206+
response.Cookies = kvp.Value.ToArray();
207+
continue;
208+
}
209+
201210
response.SetHeaderValues(kvp.Key, kvp.Value.ToArray(), false);
202211

203212
// Remember the Content-Type for possible later use

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestApiGatewayHttpApiV2Calls.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,18 @@ public async Task TestReturningCookie()
197197
{
198198
var response = await this.InvokeAPIGatewayRequest("cookies-get-returned-httpapi-v2-request.json");
199199

200-
Assert.StartsWith("TestCookie=TestValue", response.Headers["Set-Cookie"]);
200+
Assert.Collection(response.Cookies,
201+
actual => Assert.StartsWith("TestCookie=TestValue", actual));
202+
}
203+
204+
[Fact]
205+
public async Task TestReturningMultipleCookies()
206+
{
207+
var response = await this.InvokeAPIGatewayRequest("cookies-get-multiple-returned-httpapi-v2-request.json");
208+
209+
Assert.Collection(response.Cookies.OrderBy(s => s),
210+
actual => Assert.StartsWith("TestCookie1=TestValue1", actual),
211+
actual => Assert.StartsWith("TestCookie2=TestValue2", actual));
201212
}
202213

203214
[Fact]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "ANY /{proxy+}",
4+
"rawPath": "/api/cookietests/multiple",
5+
"rawQueryString": "",
6+
"headers": {
7+
"accept": "*/*",
8+
"accept-encoding": "gzip, deflate",
9+
"cache-control": "no-cache",
10+
"content-length": "41",
11+
"content-type": "application/json",
12+
"contentlength": "45",
13+
"host": "uo9pe23ec6.execute-api.us-east-1.amazonaws.com",
14+
"postman-token": "18165e8d-ecd0-4e4c-8fd2-4963a0af62d1",
15+
"user-agent": "PostmanRuntime/7.20.1",
16+
"x-amzn-trace-id": "Root=1-5e7ae61f-bb13a2d967a67dd51b8aca50",
17+
"x-forwarded-for": "192.182.149.40",
18+
"x-forwarded-port": "443",
19+
"x-forwarded-proto": "https"
20+
},
21+
"requestContext": {
22+
"accountId": "626492997873",
23+
"apiId": "uo9pe23ec6",
24+
"domainName": "uo9pe23ec6.execute-api.us-east-1.amazonaws.com",
25+
"domainPrefix": "uo9pe23ec6",
26+
"http": {
27+
"method": "GET",
28+
"path": "/api/cookietests/multiple",
29+
"protocol": "HTTP/1.1",
30+
"sourceIp": "192.182.149.40",
31+
"userAgent": "PostmanRuntime/7.20.1"
32+
},
33+
"requestId": "J7jk9g-7oAMEJGw=",
34+
"routeKey": "ANY /{proxy+}",
35+
"stage": "$default",
36+
"time": "25/Mar/2020:05:03:27 +0000",
37+
"timeEpoch": 1585112607686
38+
},
39+
"pathParameters": {
40+
"proxy": "api/cookietests/multiple"
41+
},
42+
"body": null,
43+
"isBase64Encoded": false
44+
}

Libraries/test/TestWebApp/Controllers/CookieTestsController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ public string Get()
2121
return String.Empty;
2222
}
2323

24+
[HttpGet("multiple")]
25+
public string GetMulti()
26+
{
27+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions
28+
{
29+
Expires = DateTime.Now.AddMinutes(5)
30+
};
31+
Response.Cookies.Append("TestCookie1", "TestValue1", cookieOptions);
32+
Response.Cookies.Append("TestCookie2", "TestValue2", cookieOptions);
33+
return String.Empty;
34+
}
35+
2436
[HttpGet("{id}")]
2537
public string Get(string id)
2638
{

0 commit comments

Comments
 (0)