Skip to content

Commit 2ebdb65

Browse files
committed
updated cookie encoding
1 parent 86b3f5e commit 2ebdb65

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/HttpContextExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ public static APIGatewayProxyRequest ToApiGatewayRequest(
8888
var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers);
8989
var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query);
9090

91+
var encodedCookies = request.Cookies.Select(c => $"{c.Key}={HttpUtility.UrlEncode(c.Value)}");
92+
var cookieString = string.Join("; ", encodedCookies);
93+
94+
// Add or update the Cookie header with the URL-encoded cookies
95+
if (!string.IsNullOrEmpty(cookieString))
96+
{
97+
headers["Cookie"] = cookieString;
98+
if (multiValueHeaders.ContainsKey("Cookie"))
99+
{
100+
multiValueHeaders["Cookie"] = [cookieString];
101+
}
102+
else
103+
{
104+
multiValueHeaders.Add("Cookie", [cookieString]);
105+
}
106+
}
107+
91108
var proxyRequest = new APIGatewayProxyRequest
92109
{
93110
Resource = matchedConfig.Path,

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpContextExtensionsTests.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Xunit;
66
using Moq;
77
using System.Text;
8+
using Microsoft.Extensions.Primitives;
9+
using System.Linq;
810

911
public class HttpContextExtensionsTests
1012
{
@@ -52,7 +54,7 @@ public void ToApiGatewayHttpV2Request_ShouldReturnValidApiGatewayHttpApiV2ProxyR
5254
request.QueryString = new QueryString("?status=pending");
5355
request.Headers["User-Agent"] = "TestAgent";
5456
request.Headers["Accept"] = "application/json";
55-
request.Headers["Cookie"] = "session=abc123; theme=dark; complex=this+has+spaces;";
57+
request.Headers["Cookie"] = "session=abc123; theme=dark; complex=this+has+special;";
5658

5759
var result = context.ToApiGatewayHttpV2Request();
5860

@@ -64,7 +66,7 @@ public void ToApiGatewayHttpV2Request_ShouldReturnValidApiGatewayHttpApiV2ProxyR
6466
Assert.Equal(3, result.Cookies.Length);
6567
Assert.Contains("session=abc123", result.Cookies);
6668
Assert.Contains("theme=dark", result.Cookies);
67-
Assert.Contains("complex=this%2bhas%2bspaces", result.Cookies);
69+
Assert.Contains("complex=this%2bhas%2bspecial", result.Cookies);
6870
Assert.Equal("123", result.PathParameters["userId"]);
6971
Assert.Equal("GET", result.RequestContext.Http.Method);
7072
Assert.Equal("/api/users/123/orders", result.RequestContext.Http.Path);
@@ -159,39 +161,37 @@ public void ToApiGatewayRequest_ShouldReturnValidApiGatewayProxyRequest()
159161
request.Path = "/api/users/123/orders";
160162
request.QueryString = new QueryString("?status=pending&tag=important&tag=urgent");
161163
request.Headers["User-Agent"] = "TestAgent";
162-
request.Headers["Accept"] = new Microsoft.Extensions.Primitives.StringValues(new[] { "text/html", "application/json" });
163-
request.Headers["Cookie"] = "session=abc123; theme=dark";
164+
request.Headers["Accept"] = new StringValues(new[] { "text/html", "application/json" });
165+
request.Headers["Cookie"] = "session=abc123; theme=dark; complex=this+has+special;";
164166
request.Headers["X-Custom-Header"] = "value1";
165167

166168
_mockHttpRequestUtility.Setup(x => x.ExtractHeaders(It.IsAny<IHeaderDictionary>()))
167169
.Returns((
168170
new Dictionary<string, string>
169171
{
170-
{ "User-Agent", "TestAgent" },
171-
{ "Accept", "application/json" },
172-
{ "Cookie", "session=abc123; theme=dark" },
173-
{ "X-Custom-Header", "value1" }
172+
{ "User-Agent", "TestAgent" },
173+
{ "Accept", "application/json" },
174+
{ "X-Custom-Header", "value1" }
174175
},
175176
new Dictionary<string, IList<string>>
176177
{
177-
{ "User-Agent", new List<string> { "TestAgent" } },
178-
{ "Accept", new List<string> { "text/html", "application/json" } },
179-
{ "Cookie", new List<string> { "session=abc123; theme=dark" } },
180-
{ "X-Custom-Header", new List<string> { "value1" } }
178+
{ "User-Agent", new List<string> { "TestAgent" } },
179+
{ "Accept", new List<string> { "text/html", "application/json" } },
180+
{ "X-Custom-Header", new List<string> { "value1" } }
181181
}
182182
));
183183

184184
_mockHttpRequestUtility.Setup(x => x.ExtractQueryStringParameters(It.IsAny<IQueryCollection>()))
185185
.Returns((
186186
new Dictionary<string, string>
187187
{
188-
{ "status", "pending" },
189-
{ "tag", "urgent" }
188+
{ "status", "pending" },
189+
{ "tag", "urgent" }
190190
},
191191
new Dictionary<string, IList<string>>
192192
{
193-
{ "status", new List<string> { "pending" } },
194-
{ "tag", new List<string> { "important", "urgent" } }
193+
{ "status", new List<string> { "pending" } },
194+
{ "tag", new List<string> { "important", "urgent" } }
195195
}
196196
));
197197

@@ -204,13 +204,11 @@ public void ToApiGatewayRequest_ShouldReturnValidApiGatewayProxyRequest()
204204

205205
Assert.Equal("TestAgent", result.Headers["User-Agent"]);
206206
Assert.Equal("application/json", result.Headers["Accept"]);
207-
Assert.Equal("session=abc123; theme=dark", result.Headers["Cookie"]);
208207
Assert.Equal("value1", result.Headers["X-Custom-Header"]);
209208

210-
Assert.Equal(new List<string> { "TestAgent" }, result.MultiValueHeaders["User-Agent"]);
211-
Assert.Equal(new List<string> { "text/html", "application/json" }, result.MultiValueHeaders["Accept"]);
212-
Assert.Equal(new List<string> { "session=abc123; theme=dark" }, result.MultiValueHeaders["Cookie"]);
213-
Assert.Equal(new List<string> { "value1" }, result.MultiValueHeaders["X-Custom-Header"]);
209+
var expectedCookieString = $"session={HttpUtility.UrlEncode("abc123")}; theme={HttpUtility.UrlEncode("dark")}; complex={HttpUtility.UrlEncode("this+has+special")}";
210+
Assert.Equal(expectedCookieString, result.Headers["Cookie"]);
211+
Assert.Equal([expectedCookieString], result.MultiValueHeaders["Cookie"]);
214212

215213
Assert.Equal("pending", result.QueryStringParameters["status"]);
216214
Assert.Equal("urgent", result.QueryStringParameters["tag"]);

0 commit comments

Comments
 (0)