Skip to content

Commit

Permalink
Fix headers in ApiGatewayProxyHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-bisa authored and Andrea Bisacchi committed Mar 12, 2024
1 parent 5a9f0d1 commit d508168
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,17 @@ object ApiGatewayProxyHandler {
event: ApiGatewayProxyEvent): F[Request[F]] = {
val queryString: String = getMultiValueQueryStringParameters(
event.multiValueQueryStringParameters)

val uriString: String = event.path + (if (queryString.nonEmpty) s"?$queryString" else "")

for {
method <- Method.fromString(event.httpMethod).liftTo[F]
uri <- Uri.fromString(uriString).liftTo[F]
headers = {
val builder = List.newBuilder[Header.Raw]
event.headers.foreach { h => h.foreachEntry(builder += Header.Raw(_, _)) }
event.multiValueHeaders.foreach { hMap =>
hMap.foreach {
case (key, values) =>
if (!event.headers.exists(_.contains(key))) {
values.foreach(value => builder += Header.Raw(key, value))
}
case (key, values) => values.foreach(value => builder += Header.Raw(key, value))
}
}
Headers(builder.result())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ApiGatewayProxyHandlerSuite extends CatsEffectSuite {
"X-Amz-Cf-Id" -> "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"Host" -> "1234567890.execute-api.us-east-1.amazonaws.com",
"Accept-Encoding" -> "gzip, deflate, sdch",
"X-MultiHeader" -> "foo",
"X-MultiHeader" -> "bar",
"X-Forwarded-Port" -> "443",
"Cache-Control" -> "max-age=0",
"CloudFront-Viewer-Country" -> "US",
Expand All @@ -57,7 +59,7 @@ class ApiGatewayProxyHandlerSuite extends CatsEffectSuite {
event <- event.as[ApiGatewayProxyEvent].liftTo[IO]
request <- ApiGatewayProxyHandler.decodeEvent[IO](event)
_ <- IO(assertEquals(request.method, Method.POST))
_ <- IO(assertEquals(request.uri, uri"/path/to/resource?foo=bar"))
_ <- IO(assertEquals(request.uri, uri"/path/to/resource?foo=bar&foo=baz"))
_ <- IO(assertEquals(request.headers, expectedHeaders))
responseBody <- request.bodyText.compile.string
_ <- IO(assertEquals(responseBody, expectedBody))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ object ApiGatewayProxyEventSuite {
},
"multiValueQueryStringParameters": {
"foo": [
"bar"
"bar",
"baz"
]
},
"pathParameters": {
Expand All @@ -70,7 +71,8 @@ object ApiGatewayProxyEventSuite {
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
"X-Forwarded-Proto": "https",
"X-MultiHeader": "foo"
},
"multiValueHeaders": {
"Accept": [
Expand Down Expand Up @@ -104,7 +106,7 @@ object ApiGatewayProxyEventSuite {
"US"
],
"Host": [
"0123456789.execute-api.us-east-1.amazonaws.com"
"1234567890.execute-api.us-east-1.amazonaws.com"
],
"Upgrade-Insecure-Requests": [
"1"
Expand All @@ -126,6 +128,10 @@ object ApiGatewayProxyEventSuite {
],
"X-Forwarded-Proto": [
"https"
],
"X-MultiHeader": [
"foo",
"bar"
]
},
"requestContext": {
Expand Down

0 comments on commit d508168

Please sign in to comment.