forked from aws/serverless-java-container
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws#1084: decode body if base64 is enable
- Loading branch information
Showing
2 changed files
with
89 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,11 @@ | |
import java.io.ByteArrayOutputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.*; | ||
|
||
import com.amazonaws.serverless.exceptions.ContainerInitializationException; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.cloud.function.serverless.web.ServerlessServletContext; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import com.amazonaws.serverless.proxy.spring.servletapp.MessageData; | ||
|
@@ -214,7 +210,7 @@ public static Collection<String> data() { | |
public void validateComplesrequest(String jsonEvent) throws Exception { | ||
initServletAppTest(); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", | ||
"/foo/male/list/24", "{\"name\":\"bob\"}", null)); | ||
"/foo/male/list/24", "{\"name\":\"bob\"}", false,null)); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
|
@@ -229,7 +225,7 @@ public void validateComplesrequest(String jsonEvent) throws Exception { | |
@ParameterizedTest | ||
public void testAsyncPost(String jsonEvent) throws Exception { | ||
initServletAppTest(); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/async", "{\"name\":\"bob\"}", null)); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/async", "{\"name\":\"bob\"}",false, null)); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
|
@@ -242,7 +238,7 @@ public void testAsyncPost(String jsonEvent) throws Exception { | |
public void testValidate400(String jsonEvent) throws Exception { | ||
initServletAppTest(); | ||
UserData ud = new UserData(); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/validate", mapper.writeValueAsString(ud), null)); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/validate", mapper.writeValueAsString(ud),false, null)); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
|
@@ -258,27 +254,48 @@ public void testValidate200(String jsonEvent) throws Exception { | |
ud.setFirstName("bob"); | ||
ud.setLastName("smith"); | ||
ud.setEmail("[email protected]"); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/validate", mapper.writeValueAsString(ud), null)); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/validate", mapper.writeValueAsString(ud),false, null)); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
assertEquals(200, result.get("statusCode")); | ||
assertEquals("VALID", result.get("body")); | ||
} | ||
|
||
@MethodSource("data") | ||
@ParameterizedTest | ||
public void testValidate200Base64(String jsonEvent) throws Exception { | ||
initServletAppTest(); | ||
UserData ud = new UserData(); | ||
ud.setFirstName("bob"); | ||
ud.setLastName("smith"); | ||
ud.setEmail("[email protected]"); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/validate", | ||
Base64.getMimeEncoder().encodeToString(mapper.writeValueAsString(ud).getBytes()),true, null)); | ||
|
||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
assertEquals(200, result.get("statusCode")); | ||
assertEquals("VALID", result.get("body")); | ||
} | ||
|
||
|
||
@MethodSource("data") | ||
@ParameterizedTest | ||
public void messageObject_parsesObject_returnsCorrectMessage(String jsonEvent) throws Exception { | ||
initServletAppTest(); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/message", | ||
mapper.writeValueAsString(new MessageData("test message")), null)); | ||
mapper.writeValueAsString(new MessageData("test message")),false, null)); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
assertEquals(200, result.get("statusCode")); | ||
assertEquals("test message", result.get("body")); | ||
} | ||
|
||
|
||
|
||
@SuppressWarnings({"unchecked" }) | ||
@MethodSource("data") | ||
@ParameterizedTest | ||
|
@@ -289,40 +306,42 @@ void messageObject_propertiesInContentType_returnsCorrectMessage(String jsonEven | |
headers.put(HttpHeaders.CONTENT_TYPE, "application/json;v=1"); | ||
headers.put(HttpHeaders.ACCEPT, "application/json;v=1"); | ||
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST", "/message", | ||
mapper.writeValueAsString(new MessageData("test message")), headers)); | ||
mapper.writeValueAsString(new MessageData("test message")),false, headers)); | ||
|
||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
handler.handleRequest(targetStream, output, null); | ||
Map result = mapper.readValue(output.toString(StandardCharsets.UTF_8), Map.class); | ||
assertEquals("test message", result.get("body")); | ||
} | ||
|
||
private byte[] generateHttpRequest(String jsonEvent, String method, String path, String body, Map headers) throws Exception { | ||
private byte[] generateHttpRequest(String jsonEvent, String method, String path, String body,boolean isBase64Encoded, Map headers) throws Exception { | ||
Map requestMap = mapper.readValue(jsonEvent, Map.class); | ||
if (requestMap.get("version").equals("2.0")) { | ||
return generateHttpRequest2(requestMap, method, path, body, headers); | ||
return generateHttpRequest2(requestMap, method, path, body, isBase64Encoded,headers); | ||
} | ||
return generateHttpRequest(requestMap, method, path, body, headers); | ||
return generateHttpRequest(requestMap, method, path, body,isBase64Encoded, headers); | ||
} | ||
|
||
@SuppressWarnings({ "unchecked"}) | ||
private byte[] generateHttpRequest(Map requestMap, String method, String path, String body, Map headers) throws Exception { | ||
private byte[] generateHttpRequest(Map requestMap, String method, String path, String body,boolean isBase64Encoded, Map headers) throws Exception { | ||
requestMap.put("path", path); | ||
requestMap.put("httpMethod", method); | ||
requestMap.put("body", body); | ||
requestMap.put("isBase64Encoded", isBase64Encoded); | ||
if (!CollectionUtils.isEmpty(headers)) { | ||
requestMap.put("headers", headers); | ||
} | ||
return mapper.writeValueAsBytes(requestMap); | ||
} | ||
|
||
@SuppressWarnings({ "unchecked"}) | ||
private byte[] generateHttpRequest2(Map requestMap, String method, String path, String body, Map headers) throws Exception { | ||
private byte[] generateHttpRequest2(Map requestMap, String method, String path, String body,boolean isBase64Encoded, Map headers) throws Exception { | ||
Map map = mapper.readValue(API_GATEWAY_EVENT_V2, Map.class); | ||
Map http = (Map) ((Map) map.get("requestContext")).get("http"); | ||
http.put("path", path); | ||
http.put("method", method); | ||
map.put("body", body); | ||
map.put("isBase64Encoded", isBase64Encoded); | ||
if (!CollectionUtils.isEmpty(headers)) { | ||
map.put("headers", headers); | ||
} | ||
|