Skip to content

Commit

Permalink
fix: Request Parameters not picked up for v1 payload format with mult…
Browse files Browse the repository at this point in the history
…ivalue headers enabled (#1256)
  • Loading branch information
deki committed Jan 29, 2025
1 parent 5f02ec1 commit 8176641
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import com.amazonaws.serverless.proxy.internal.HttpUtils;
import org.apache.commons.io.Charsets;
Expand Down Expand Up @@ -118,7 +120,11 @@ private static HttpServletRequest generateRequest1(String request, Context lambd

ServerlessHttpServletRequest httpRequest = new ServerlessHttpServletRequest(servletContext, v1Request.getHttpMethod(), v1Request.getPath());

populateQueryStringparameters(v1Request.getQueryStringParameters(), httpRequest);
populateQueryStringParameters(v1Request.getQueryStringParameters(), httpRequest);
if (v1Request.getMultiValueQueryStringParameters() != null) {
MultiValueMapAdapter<String, String> queryStringParameters = new MultiValueMapAdapter(v1Request.getMultiValueQueryStringParameters());
queryStringParameters.forEach((k, v) -> httpRequest.setParameter(k, StringUtils.collectionToCommaDelimitedString(v)));
}

if (v1Request.getMultiValueHeaders() != null) {
MultiValueMapAdapter headers = new MultiValueMapAdapter(v1Request.getMultiValueHeaders());
Expand Down Expand Up @@ -152,7 +158,7 @@ private static HttpServletRequest generateRequest2(String request, Context lambd

ServerlessHttpServletRequest httpRequest = new ServerlessHttpServletRequest(servletContext,
v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath());
populateQueryStringparameters(v2Request.getQueryStringParameters(), httpRequest);
populateQueryStringParameters(v2Request.getQueryStringParameters(), httpRequest);

v2Request.getHeaders().forEach(httpRequest::setHeader);

Expand All @@ -172,7 +178,7 @@ private static HttpServletRequest generateRequest2(String request, Context lambd
return httpRequest;
}

private static void populateQueryStringparameters(Map<String, String> requestParameters, ServerlessHttpServletRequest httpRequest) {
private static void populateQueryStringParameters(Map<String, String> requestParameters, ServerlessHttpServletRequest httpRequest) {
if (!CollectionUtils.isEmpty(requestParameters)) {
for (Entry<String, String> entry : requestParameters.entrySet()) {
httpRequest.setParameter(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public class AwsSpringHttpProcessingUtilsTests {
+ " },\n"
+ " \"queryStringParameters\": {\n"
+ " \"abc\": \"xyz\",\n"
+ " \"foo\": \"baz\"\n"
+ " \"parameter1\": \"value2\"\n"
+ " },\n"
+ " \"multiValueQueryStringParameters\": {\n"
+ " \"abc\": [\n"
+ " \"xyz\"\n"
+ " ],\n"
+ " \"foo\": [\n"
+ " \"bar\",\n"
+ " \"baz\"\n"
+ " \"parameter1\": [\n"
+ " \"value1\",\n"
+ " \"value2\"\n"
+ " ]\n"
+ " },\n"
+ " \"requestContext\": {\n"
Expand Down Expand Up @@ -184,10 +184,35 @@ public class AwsSpringHttpProcessingUtilsTests {
" }\n" +
"}";

private static final String ALB_EVENT = "{\n" +
" \"requestContext\": {\n" +
" \"elb\": {\n" +
" \"targetGroupArn\": \"arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/6d0ecf831eec9f09\"\n" +
" }\n" +
" },\n" +
" \"httpMethod\": \"POST\",\n" +
" \"path\": \"/async\",\n" +
" \"multiValueQueryStringParameters\": { \"parameter1\": [\"value1\", \"value2\"]},\n" +
" \"multiValueHeaders\": {\n" +
" \"accept\": [\"text/html,application/xhtml+xml\"],\n" +
" \"accept-language\": [\"en-US,en;q=0.8\"],\n" +
" \"content-type\": [\"text/plain\"],\n" +
" \"cookie\": [\"cookies\"],\n" +
" \"host\": [\"lambda-846800462-us-east-2.elb.amazonaws.com\"],\n" +
" \"User-Agent\": [\"curl/7.79.1\"],\n" +
" \"x-amzn-trace-id\": [\"Root=1-5bdb40ca-556d8b0c50dc66f0511bf520\"],\n" +
" \"x-forwarded-for\": [\"72.21.198.66\"],\n" +
" \"x-forwarded-port\": [\"443\"],\n" +
" \"x-forwarded-proto\": [\"https\"]\n" +
" },\n" +
" \"isBase64Encoded\": false,\n" +
" \"body\": \"request_body\"\n" +
"}";

private final ObjectMapper mapper = new ObjectMapper();

public static Collection<String> data() {
return Arrays.asList(new String[]{API_GATEWAY_EVENT, API_GATEWAY_EVENT_V2});
return Arrays.asList(new String[]{API_GATEWAY_EVENT, API_GATEWAY_EVENT_V2, ALB_EVENT});
}

@MethodSource("data")
Expand All @@ -197,10 +222,17 @@ public void validateHttpServletRequestGenerationWithInputStream(String jsonEvent
ServerlessServletContext servletContext = new ServerlessServletContext();
HttpServletRequest request = AwsSpringHttpProcessingUtils.generateHttpServletRequest(inputStream, null, servletContext, mapper);
// spot check some headers
assertRequest(request);
}

private static void assertRequest(HttpServletRequest request) {
assertEquals("curl/7.79.1", request.getHeader("User-Agent"));
assertEquals("443", request.getHeader("X-Forwarded-Port"));
assertEquals("POST", request.getMethod());
assertEquals("/async", request.getRequestURI());
assertEquals("value1,value2", request.getParameter("parameter1"));
// to be fixed: assertArrayEquals(new String[]{"value1","value2"}, request.getParameterValues("parameter1"));

}

@MethodSource("data")
Expand All @@ -209,10 +241,7 @@ public void validateHttpServletRequestGenerationWithJson(String jsonEvent) {
ServerlessServletContext servletContext = new ServerlessServletContext();
HttpServletRequest request = AwsSpringHttpProcessingUtils.generateHttpServletRequest(jsonEvent, null, servletContext, mapper);
// spot check some headers
assertEquals("curl/7.79.1", request.getHeader("User-Agent"));
assertEquals("443", request.getHeader("X-Forwarded-Port"));
assertEquals("POST", request.getMethod());
assertEquals("/async", request.getRequestURI());
assertRequest(request);
}

@MethodSource("data")
Expand Down

0 comments on commit 8176641

Please sign in to comment.