Skip to content

Commit

Permalink
Merge pull request #26819 from mar-celohenrique/fix/exception_paramet…
Browse files Browse the repository at this point in the history
…er_value_not_present

Add condition to avoid exception when parameter value is not present
  • Loading branch information
gsmet authored Jul 28, 2022
2 parents 326dee7 + a8914bf commit 80194e8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ public void handleHttpRequests(RoutingContext ctx) {
if (param.contains("=")) {
String[] keyval = param.split("=");
try {
String value = "";

if (keyval.length == 2) {
value = URLDecoder.decode(keyval[1], StandardCharsets.UTF_8.name());
}

event.getMultiValueQueryStringParameters().add(
URLDecoder.decode(keyval[0], StandardCharsets.UTF_8.name()),
URLDecoder.decode(keyval[1], StandardCharsets.UTF_8.name()));
value);
} catch (UnsupportedEncodingException e) {
log.error("Failed to parse query string", e);
ctx.response().setStatusCode(400).end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,47 @@ public void testDateHeaders() throws Exception {
lambdaResponse.close();
}

@Test
public void testQueryParameters() throws Exception {
Client client = ClientBuilder.newBuilder().build();
WebTarget base = client.target("http://localhost:" + MockEventServer.DEFAULT_PORT);
Future<Response> lambdaInvoke = base.queryParam("foo", "bar")
.queryParam("bar", "").request().async()
.get();

Response next = base.path(MockEventServer.NEXT_INVOCATION).request().get();
Assertions.assertEquals(200, next.getStatus());
String requestId = next.getHeaderString(AmazonLambdaApi.LAMBDA_RUNTIME_AWS_REQUEST_ID);
String traceId = next.getHeaderString(AmazonLambdaApi.LAMBDA_TRACE_HEADER_KEY);
Assertions.assertNotNull(requestId);
Assertions.assertNotNull(traceId);
String json = next.readEntity(String.class);
AwsProxyRequest event = eventReader.readValue(json);
Assertions.assertEquals("bar", event.getMultiValueQueryStringParameters().getFirst("foo"));
Assertions.assertEquals("", event.getMultiValueQueryStringParameters().getFirst("bar"));
next.close();

AwsProxyResponse res = new AwsProxyResponse();
res.setStatusCode(201);
res.setMultiValueHeaders(new Headers());
res.getMultiValueHeaders().add("Content-Type", "text/plain");
res.getMultiValueHeaders().add("Last-Modified", "Tue, 26 Oct 2021 01:01:01 GMT");
res.getMultiValueHeaders().add("Expires", "Tue, 26 Oct 2021 01:01:01 GMT");
res.getMultiValueHeaders().add("Date", "Tue, 26 Oct 2021 01:01:01 GMT");
res.setBody("Hi");
Response sendResponse = base.path(MockEventServer.INVOCATION).path(requestId).path("response")
.request().post(Entity.json(resWriter.writeValueAsString(res)));
Assertions.assertEquals(204, sendResponse.getStatus());
sendResponse.close();

Response lambdaResponse = lambdaInvoke.get();
Assertions.assertEquals(201, lambdaResponse.getStatus());
Assertions.assertEquals("Hi", lambdaResponse.readEntity(String.class));
Assertions.assertTrue(lambdaResponse.getStringHeaders().containsKey("Content-Type"));
Assertions.assertEquals("Tue, 26 Oct 2021 01:01:01 GMT", lambdaResponse.getStringHeaders().getFirst("Last-Modified"));
Assertions.assertEquals("Tue, 26 Oct 2021 01:01:01 GMT", lambdaResponse.getStringHeaders().getFirst("Expires"));
Assertions.assertEquals("Tue, 26 Oct 2021 01:01:01 GMT", lambdaResponse.getStringHeaders().getFirst("Date"));
lambdaResponse.close();
}

}

0 comments on commit 80194e8

Please sign in to comment.