Skip to content

Commit

Permalink
feat: add single value headers and query string params (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigpwned authored and deki committed Sep 21, 2022
1 parent c7fb0d1 commit effeade
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
*/
package com.amazonaws.serverless.proxy.model;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.Map;

/**
* Default implementation of the request object from an API Gateway AWS_PROXY integration
*/
Expand All @@ -33,7 +32,9 @@ public class AwsProxyRequest {
private String resource;
private AwsProxyRequestContext requestContext;
private MultiValuedTreeMap<String, String> multiValueQueryStringParameters;
private Map<String, String> queryStringParameters;
private Headers multiValueHeaders;
private SingleValueHeaders headers;
private Map<String, String> pathParameters;
private String httpMethod;
private Map<String, String> stageVariables;
Expand Down Expand Up @@ -113,21 +114,35 @@ public MultiValuedTreeMap<String, String> getMultiValueQueryStringParameters() {
return multiValueQueryStringParameters;
}


public void setMultiValueQueryStringParameters(
MultiValuedTreeMap<String, String> multiValueQueryStringParameters) {
this.multiValueQueryStringParameters = multiValueQueryStringParameters;
}

public Map<String, String> getQueryStringParameters() {
return queryStringParameters;
}

public void setQueryStringParameters(Map<String, String> queryStringParameters) {
this.queryStringParameters = queryStringParameters;
}

public Headers getMultiValueHeaders() {
return multiValueHeaders;
}


public void setMultiValueHeaders(Headers multiValueHeaders) {
this.multiValueHeaders = multiValueHeaders;
}

public SingleValueHeaders getHeaders() {
return headers;
}

public void setHeaders(SingleValueHeaders headers) {
this.headers = headers;
}


public Map<String, String> getPathParameters() {
return pathParameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.model;

import java.util.TreeMap;

public class SingleValueHeaders extends TreeMap<String, String> {

private static final long serialVersionUID = 42L;

public SingleValueHeaders() {
super(String.CASE_INSENSITIVE_ORDER);
}
}
Original file line number Diff line number Diff line change
@@ -1,75 +1,19 @@
package com.amazonaws.serverless.proxy.model;

import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import org.junit.Test;
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.*;

public class AwsProxyRequestTest {
private static final String CUSTOM_HEADER_KEY_LOWER_CASE = "custom-header";
private static final String CUSTOM_HEADER_VALUE = "123456";
public static final String REQUEST_JSON = "{\n" +
" \"resource\": \"/api/{proxy+}\",\n" +
" \"path\": \"/api/endpoint\",\n" +
" \"httpMethod\": \"OPTIONS\",\n" +
" \"headers\": {\n" +
" \"Accept\": \"*/*\",\n" +
" \"User-Agent\": \"PostmanRuntime/7.1.1\",\n" +
" \"" + CUSTOM_HEADER_KEY_LOWER_CASE +"\":" + "\"" + CUSTOM_HEADER_VALUE + "\"\n" +
" },\n" +
" \"multiValueHeaders\": {\n" +
" \"Accept\": [\n" +
" \"*/*\"\n" +
" ],\n" +
" \"User-Agent\": [\n" +
" \"PostmanRuntime/7.1.1\"\n" +
" ],\n" +
" \"" + CUSTOM_HEADER_KEY_LOWER_CASE + "\": [\n" +
" \"" + CUSTOM_HEADER_VALUE + "\"\n" +
" ]\n" +
" },\n" +
" \"queryStringParameters\": null,\n" +
" \"multiValueQueryStringParameters\": null,\n" +
" \"pathParameters\": {\n" +
" \"proxy\": \"endpoint\"\n" +
" },\n" +
" \"stageVariables\": null,\n" +
" \"requestContext\": {\n" +
" \"resourceId\": null,\n" +
" \"resourcePath\": \"/api/{proxy+}\",\n" +
" \"httpMethod\": \"OPTIONS\",\n" +
" \"extendedRequestId\": null,\n" +
" \"requestTime\": \"15/Dec/2018:20:37:47 +0000\",\n" +
" \"path\": \"/api/endpoint\",\n" +
" \"accountId\": null,\n" +
" \"protocol\": \"HTTP/1.1\",\n" +
" \"stage\": \"stage_name\",\n" +
" \"domainPrefix\": null,\n" +
" \"requestTimeEpoch\": 1544906267828,\n" +
" \"requestId\": null,\n" +
" \"identity\": {\n" +
" \"cognitoIdentityPoolId\": null,\n" +
" \"accountId\": null,\n" +
" \"cognitoIdentityId\": null,\n" +
" \"caller\": null,\n" +
" \"sourceIp\": \"54.240.196.171\",\n" +
" \"accessKey\": null,\n" +
" \"cognitoAuthenticationType\": null,\n" +
" \"cognitoAuthenticationProvider\": null,\n" +
" \"userArn\": null,\n" +
" \"userAgent\": \"PostmanRuntime/7.1.1\",\n" +
" \"user\": null\n" +
" },\n" +
" \"domainName\": \"https://apiId.execute-api.eu-central-1.amazonaws.com/\",\n" +
" \"apiId\": \"apiId\"\n" +
" },\n" +
" \"body\": null,\n" +
" \"isBase64Encoded\": true\n" +
"}";

@Test
public void deserialize_multiValuedHeaders_caseInsensitive() throws IOException {
Expand Down Expand Up @@ -160,4 +104,35 @@ private String getRequestJson(boolean base64Encoded, String headerKey, String he
" \"isBase64Encoded\": " + (base64Encoded?"true":"false") + "\n" +
"}";
}

@Test
public void deserialize_singleValuedHeaders() throws IOException {
AwsProxyRequest req =
new AwsProxyRequestBuilder().fromJsonString(getSingleValueRequestJson()).build();

assertThat(req.getHeaders().get("accept"), is("*"));
}

/**
* Captured from a live request to an ALB with a Lambda integration with
* lambda.multi_value_headers.enabled=false.
*/
private String getSingleValueRequestJson() {
return "{\n" + " \"requestContext\": {\n" + " \"elb\": {\n"
+ " \"targetGroupArn\": \"arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/prod-example-function/e77803ebb6d2c24\"\n"
+ " }\n" + " },\n" + " \"httpMethod\": \"PUT\",\n"
+ " \"path\": \"/path/to/resource\",\n" + " \"queryStringParameters\": {},\n"
+ " \"headers\": {\n" + " \"accept\": \"*\",\n"
+ " \"content-length\": \"17\",\n"
+ " \"content-type\": \"application/json\",\n"
+ " \"host\": \"stackoverflow.name\",\n"
+ " \"user-agent\": \"curl/7.77.0\",\n"
+ " \"x-amzn-trace-id\": \"Root=1-62e22402-3a5f246225e45edd7735c182\",\n"
+ " \"x-forwarded-for\": \"24.14.13.186\",\n"
+ " \"x-forwarded-port\": \"443\",\n"
+ " \"x-forwarded-proto\": \"https\",\n"
+ " \"x-jersey-tracing-accept\": \"true\"\n" + " },\n"
+ " \"body\": \"{\\\"alpha\\\":\\\"bravo\\\"}\",\n"
+ " \"isBase64Encoded\": false\n" + "} \n";
}
}

0 comments on commit effeade

Please sign in to comment.