Skip to content

Commit

Permalink
Revert "updated cookie encoding"
Browse files Browse the repository at this point in the history
This reverts commit 2ebdb65.
  • Loading branch information
gcbeattyAWS committed Dec 10, 2024
1 parent 2ebdb65 commit 63c9b2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,6 @@ public static APIGatewayProxyRequest ToApiGatewayRequest(
var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers);
var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query);

var encodedCookies = request.Cookies.Select(c => $"{c.Key}={HttpUtility.UrlEncode(c.Value)}");
var cookieString = string.Join("; ", encodedCookies);

// Add or update the Cookie header with the URL-encoded cookies
if (!string.IsNullOrEmpty(cookieString))
{
headers["Cookie"] = cookieString;
if (multiValueHeaders.ContainsKey("Cookie"))
{
multiValueHeaders["Cookie"] = [cookieString];
}
else
{
multiValueHeaders.Add("Cookie", [cookieString]);
}
}

var proxyRequest = new APIGatewayProxyRequest
{
Resource = matchedConfig.Path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using Xunit;
using Moq;
using System.Text;
using Microsoft.Extensions.Primitives;
using System.Linq;

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

var result = context.ToApiGatewayHttpV2Request();

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

_mockHttpRequestUtility.Setup(x => x.ExtractHeaders(It.IsAny<IHeaderDictionary>()))
.Returns((
new Dictionary<string, string>
{
{ "User-Agent", "TestAgent" },
{ "Accept", "application/json" },
{ "X-Custom-Header", "value1" }
{ "User-Agent", "TestAgent" },
{ "Accept", "application/json" },
{ "Cookie", "session=abc123; theme=dark" },
{ "X-Custom-Header", "value1" }
},
new Dictionary<string, IList<string>>
{
{ "User-Agent", new List<string> { "TestAgent" } },
{ "Accept", new List<string> { "text/html", "application/json" } },
{ "X-Custom-Header", new List<string> { "value1" } }
{ "User-Agent", new List<string> { "TestAgent" } },
{ "Accept", new List<string> { "text/html", "application/json" } },
{ "Cookie", new List<string> { "session=abc123; theme=dark" } },
{ "X-Custom-Header", new List<string> { "value1" } }
}
));

_mockHttpRequestUtility.Setup(x => x.ExtractQueryStringParameters(It.IsAny<IQueryCollection>()))
.Returns((
new Dictionary<string, string>
{
{ "status", "pending" },
{ "tag", "urgent" }
{ "status", "pending" },
{ "tag", "urgent" }
},
new Dictionary<string, IList<string>>
{
{ "status", new List<string> { "pending" } },
{ "tag", new List<string> { "important", "urgent" } }
{ "status", new List<string> { "pending" } },
{ "tag", new List<string> { "important", "urgent" } }
}
));

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

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

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

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

0 comments on commit 63c9b2a

Please sign in to comment.