Skip to content

Commit effeade

Browse files
sigpwneddeki
authored andcommitted
feat: add single value headers and query string params (#315)
1 parent c7fb0d1 commit effeade

File tree

3 files changed

+83
-69
lines changed

3 files changed

+83
-69
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/AwsProxyRequest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
*/
1313
package com.amazonaws.serverless.proxy.model;
1414

15+
import java.util.HashMap;
16+
import java.util.Map;
1517
import com.fasterxml.jackson.annotation.JsonIgnore;
1618
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1719
import com.fasterxml.jackson.annotation.JsonProperty;
1820

19-
import java.util.HashMap;
20-
import java.util.Map;
21-
2221
/**
2322
* Default implementation of the request object from an API Gateway AWS_PROXY integration
2423
*/
@@ -33,7 +32,9 @@ public class AwsProxyRequest {
3332
private String resource;
3433
private AwsProxyRequestContext requestContext;
3534
private MultiValuedTreeMap<String, String> multiValueQueryStringParameters;
35+
private Map<String, String> queryStringParameters;
3636
private Headers multiValueHeaders;
37+
private SingleValueHeaders headers;
3738
private Map<String, String> pathParameters;
3839
private String httpMethod;
3940
private Map<String, String> stageVariables;
@@ -113,21 +114,35 @@ public MultiValuedTreeMap<String, String> getMultiValueQueryStringParameters() {
113114
return multiValueQueryStringParameters;
114115
}
115116

116-
117117
public void setMultiValueQueryStringParameters(
118118
MultiValuedTreeMap<String, String> multiValueQueryStringParameters) {
119119
this.multiValueQueryStringParameters = multiValueQueryStringParameters;
120120
}
121121

122+
public Map<String, String> getQueryStringParameters() {
123+
return queryStringParameters;
124+
}
125+
126+
public void setQueryStringParameters(Map<String, String> queryStringParameters) {
127+
this.queryStringParameters = queryStringParameters;
128+
}
129+
122130
public Headers getMultiValueHeaders() {
123131
return multiValueHeaders;
124132
}
125133

126-
127134
public void setMultiValueHeaders(Headers multiValueHeaders) {
128135
this.multiValueHeaders = multiValueHeaders;
129136
}
130137

138+
public SingleValueHeaders getHeaders() {
139+
return headers;
140+
}
141+
142+
public void setHeaders(SingleValueHeaders headers) {
143+
this.headers = headers;
144+
}
145+
131146

132147
public Map<String, String> getPathParameters() {
133148
return pathParameters;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0/
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
package com.amazonaws.serverless.proxy.model;
14+
15+
import java.util.TreeMap;
16+
17+
public class SingleValueHeaders extends TreeMap<String, String> {
18+
19+
private static final long serialVersionUID = 42L;
20+
21+
public SingleValueHeaders() {
22+
super(String.CASE_INSENSITIVE_ORDER);
23+
}
24+
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/model/AwsProxyRequestTest.java

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,19 @@
11
package com.amazonaws.serverless.proxy.model;
22

3+
import static junit.framework.TestCase.assertTrue;
4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertNotNull;
9+
import java.io.IOException;
10+
import org.junit.Test;
311
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
412
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import org.junit.Test;
6-
7-
import java.io.IOException;
8-
9-
import static junit.framework.TestCase.assertTrue;
10-
import static org.junit.Assert.*;
1113

1214
public class AwsProxyRequestTest {
1315
private static final String CUSTOM_HEADER_KEY_LOWER_CASE = "custom-header";
1416
private static final String CUSTOM_HEADER_VALUE = "123456";
15-
public static final String REQUEST_JSON = "{\n" +
16-
" \"resource\": \"/api/{proxy+}\",\n" +
17-
" \"path\": \"/api/endpoint\",\n" +
18-
" \"httpMethod\": \"OPTIONS\",\n" +
19-
" \"headers\": {\n" +
20-
" \"Accept\": \"*/*\",\n" +
21-
" \"User-Agent\": \"PostmanRuntime/7.1.1\",\n" +
22-
" \"" + CUSTOM_HEADER_KEY_LOWER_CASE +"\":" + "\"" + CUSTOM_HEADER_VALUE + "\"\n" +
23-
" },\n" +
24-
" \"multiValueHeaders\": {\n" +
25-
" \"Accept\": [\n" +
26-
" \"*/*\"\n" +
27-
" ],\n" +
28-
" \"User-Agent\": [\n" +
29-
" \"PostmanRuntime/7.1.1\"\n" +
30-
" ],\n" +
31-
" \"" + CUSTOM_HEADER_KEY_LOWER_CASE + "\": [\n" +
32-
" \"" + CUSTOM_HEADER_VALUE + "\"\n" +
33-
" ]\n" +
34-
" },\n" +
35-
" \"queryStringParameters\": null,\n" +
36-
" \"multiValueQueryStringParameters\": null,\n" +
37-
" \"pathParameters\": {\n" +
38-
" \"proxy\": \"endpoint\"\n" +
39-
" },\n" +
40-
" \"stageVariables\": null,\n" +
41-
" \"requestContext\": {\n" +
42-
" \"resourceId\": null,\n" +
43-
" \"resourcePath\": \"/api/{proxy+}\",\n" +
44-
" \"httpMethod\": \"OPTIONS\",\n" +
45-
" \"extendedRequestId\": null,\n" +
46-
" \"requestTime\": \"15/Dec/2018:20:37:47 +0000\",\n" +
47-
" \"path\": \"/api/endpoint\",\n" +
48-
" \"accountId\": null,\n" +
49-
" \"protocol\": \"HTTP/1.1\",\n" +
50-
" \"stage\": \"stage_name\",\n" +
51-
" \"domainPrefix\": null,\n" +
52-
" \"requestTimeEpoch\": 1544906267828,\n" +
53-
" \"requestId\": null,\n" +
54-
" \"identity\": {\n" +
55-
" \"cognitoIdentityPoolId\": null,\n" +
56-
" \"accountId\": null,\n" +
57-
" \"cognitoIdentityId\": null,\n" +
58-
" \"caller\": null,\n" +
59-
" \"sourceIp\": \"54.240.196.171\",\n" +
60-
" \"accessKey\": null,\n" +
61-
" \"cognitoAuthenticationType\": null,\n" +
62-
" \"cognitoAuthenticationProvider\": null,\n" +
63-
" \"userArn\": null,\n" +
64-
" \"userAgent\": \"PostmanRuntime/7.1.1\",\n" +
65-
" \"user\": null\n" +
66-
" },\n" +
67-
" \"domainName\": \"https://apiId.execute-api.eu-central-1.amazonaws.com/\",\n" +
68-
" \"apiId\": \"apiId\"\n" +
69-
" },\n" +
70-
" \"body\": null,\n" +
71-
" \"isBase64Encoded\": true\n" +
72-
"}";
7317

7418
@Test
7519
public void deserialize_multiValuedHeaders_caseInsensitive() throws IOException {
@@ -160,4 +104,35 @@ private String getRequestJson(boolean base64Encoded, String headerKey, String he
160104
" \"isBase64Encoded\": " + (base64Encoded?"true":"false") + "\n" +
161105
"}";
162106
}
107+
108+
@Test
109+
public void deserialize_singleValuedHeaders() throws IOException {
110+
AwsProxyRequest req =
111+
new AwsProxyRequestBuilder().fromJsonString(getSingleValueRequestJson()).build();
112+
113+
assertThat(req.getHeaders().get("accept"), is("*"));
114+
}
115+
116+
/**
117+
* Captured from a live request to an ALB with a Lambda integration with
118+
* lambda.multi_value_headers.enabled=false.
119+
*/
120+
private String getSingleValueRequestJson() {
121+
return "{\n" + " \"requestContext\": {\n" + " \"elb\": {\n"
122+
+ " \"targetGroupArn\": \"arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/prod-example-function/e77803ebb6d2c24\"\n"
123+
+ " }\n" + " },\n" + " \"httpMethod\": \"PUT\",\n"
124+
+ " \"path\": \"/path/to/resource\",\n" + " \"queryStringParameters\": {},\n"
125+
+ " \"headers\": {\n" + " \"accept\": \"*\",\n"
126+
+ " \"content-length\": \"17\",\n"
127+
+ " \"content-type\": \"application/json\",\n"
128+
+ " \"host\": \"stackoverflow.name\",\n"
129+
+ " \"user-agent\": \"curl/7.77.0\",\n"
130+
+ " \"x-amzn-trace-id\": \"Root=1-62e22402-3a5f246225e45edd7735c182\",\n"
131+
+ " \"x-forwarded-for\": \"24.14.13.186\",\n"
132+
+ " \"x-forwarded-port\": \"443\",\n"
133+
+ " \"x-forwarded-proto\": \"https\",\n"
134+
+ " \"x-jersey-tracing-accept\": \"true\"\n" + " },\n"
135+
+ " \"body\": \"{\\\"alpha\\\":\\\"bravo\\\"}\",\n"
136+
+ " \"isBase64Encoded\": false\n" + "} \n";
137+
}
163138
}

0 commit comments

Comments
 (0)