Skip to content

Commit 2d16463

Browse files
committed
Added tests to validate #262 and jackson annotation to isBase64Encoded field in the request object
1 parent 2a4de7f commit 2d16463

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import com.fasterxml.jackson.annotation.JsonIgnore;
1616
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
17+
import com.fasterxml.jackson.annotation.JsonProperty;
1718

1819
import java.util.HashMap;
1920
import java.util.Map;
@@ -167,7 +168,7 @@ public void setPath(String path) {
167168
this.path = path;
168169
}
169170

170-
171+
@JsonProperty("isBase64Encoded")
171172
public boolean isBase64Encoded() {
172173
return isBase64Encoded;
173174
}

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

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.amazonaws.serverless.proxy.model;
22

33
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
45
import org.junit.Test;
56

67
import java.io.IOException;
78

8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertNotNull;
9+
import static junit.framework.TestCase.assertTrue;
10+
import static org.junit.Assert.*;
1011

1112
public class AwsProxyRequestTest {
1213
private static final String CUSTOM_HEADER_KEY_LOWER_CASE = "custom-header";
@@ -67,13 +68,96 @@ public class AwsProxyRequestTest {
6768
" \"apiId\": \"apiId\"\n" +
6869
" },\n" +
6970
" \"body\": null,\n" +
70-
" \"isBase64Encoded\": false\n" +
71+
" \"isBase64Encoded\": true\n" +
7172
"}";
7273

7374
@Test
7475
public void deserialize_multiValuedHeaders_caseInsensitive() throws IOException {
75-
AwsProxyRequest req = new AwsProxyRequestBuilder().fromJsonString(REQUEST_JSON).build();
76+
AwsProxyRequest req = new AwsProxyRequestBuilder()
77+
.fromJsonString(getRequestJson(true, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build();
7678
assertNotNull(req.getMultiValueHeaders().get(CUSTOM_HEADER_KEY_LOWER_CASE.toUpperCase()));
7779
assertEquals(CUSTOM_HEADER_VALUE, req.getMultiValueHeaders().get(CUSTOM_HEADER_KEY_LOWER_CASE.toUpperCase()).get(0));
80+
assertTrue(req.isBase64Encoded());
81+
}
82+
83+
@Test
84+
public void deserialize_base64Encoded_readsBoolCorrectly() throws IOException {
85+
AwsProxyRequest req = new AwsProxyRequestBuilder()
86+
.fromJsonString(getRequestJson(true, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build();
87+
assertTrue(req.isBase64Encoded());
88+
req = new AwsProxyRequestBuilder()
89+
.fromJsonString(getRequestJson(false, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build();
90+
assertFalse(req.isBase64Encoded());
91+
}
92+
93+
@Test
94+
public void serialize_base64Encoded_fieldContainsIsPrefix() throws IOException {
95+
AwsProxyRequest req = new AwsProxyRequestBuilder()
96+
.fromJsonString(getRequestJson(true, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build();
97+
ObjectMapper mapper = new ObjectMapper();
98+
String serializedRequest = mapper.writeValueAsString(req);
99+
System.out.println(serializedRequest);
100+
assertTrue(serializedRequest.contains("\"isBase64Encoded\":true"));
101+
}
102+
103+
private String getRequestJson(boolean base64Encoded, String headerKey, String headerValue) {
104+
return "{\n" +
105+
" \"resource\": \"/api/{proxy+}\",\n" +
106+
" \"path\": \"/api/endpoint\",\n" +
107+
" \"httpMethod\": \"OPTIONS\",\n" +
108+
" \"headers\": {\n" +
109+
" \"Accept\": \"*/*\",\n" +
110+
" \"User-Agent\": \"PostmanRuntime/7.1.1\",\n" +
111+
" \"" + headerKey +"\":" + "\"" + headerValue + "\"\n" +
112+
" },\n" +
113+
" \"multiValueHeaders\": {\n" +
114+
" \"Accept\": [\n" +
115+
" \"*/*\"\n" +
116+
" ],\n" +
117+
" \"User-Agent\": [\n" +
118+
" \"PostmanRuntime/7.1.1\"\n" +
119+
" ],\n" +
120+
" \"" + headerKey + "\": [\n" +
121+
" \"" + headerValue + "\"\n" +
122+
" ]\n" +
123+
" },\n" +
124+
" \"queryStringParameters\": null,\n" +
125+
" \"multiValueQueryStringParameters\": null,\n" +
126+
" \"pathParameters\": {\n" +
127+
" \"proxy\": \"endpoint\"\n" +
128+
" },\n" +
129+
" \"stageVariables\": null,\n" +
130+
" \"requestContext\": {\n" +
131+
" \"resourceId\": null,\n" +
132+
" \"resourcePath\": \"/api/{proxy+}\",\n" +
133+
" \"httpMethod\": \"OPTIONS\",\n" +
134+
" \"extendedRequestId\": null,\n" +
135+
" \"requestTime\": \"15/Dec/2018:20:37:47 +0000\",\n" +
136+
" \"path\": \"/api/endpoint\",\n" +
137+
" \"accountId\": null,\n" +
138+
" \"protocol\": \"HTTP/1.1\",\n" +
139+
" \"stage\": \"stage_name\",\n" +
140+
" \"domainPrefix\": null,\n" +
141+
" \"requestTimeEpoch\": 1544906267828,\n" +
142+
" \"requestId\": null,\n" +
143+
" \"identity\": {\n" +
144+
" \"cognitoIdentityPoolId\": null,\n" +
145+
" \"accountId\": null,\n" +
146+
" \"cognitoIdentityId\": null,\n" +
147+
" \"caller\": null,\n" +
148+
" \"sourceIp\": \"54.240.196.171\",\n" +
149+
" \"accessKey\": null,\n" +
150+
" \"cognitoAuthenticationType\": null,\n" +
151+
" \"cognitoAuthenticationProvider\": null,\n" +
152+
" \"userArn\": null,\n" +
153+
" \"userAgent\": \"PostmanRuntime/7.1.1\",\n" +
154+
" \"user\": null\n" +
155+
" },\n" +
156+
" \"domainName\": \"https://apiId.execute-api.eu-central-1.amazonaws.com/\",\n" +
157+
" \"apiId\": \"apiId\"\n" +
158+
" },\n" +
159+
" \"body\": null,\n" +
160+
" \"isBase64Encoded\": " + (base64Encoded?"true":"false") + "\n" +
161+
"}";
78162
}
79163
}

0 commit comments

Comments
 (0)