-
Notifications
You must be signed in to change notification settings - Fork 566
#1084: decode body if base64 is enable #1085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
deki
merged 5 commits into
aws:main
from
npeters:fix/#1084-ServerlessHttpServletRequest.content-decode-base64
Nov 25, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8fef6b5
#1084: decode body if base64 is enable
npeters 3a59eff
#1084: default content-type: application/json
npeters 6fa3f29
#1084: remove duplicated code with populateContentAndContentType
npeters 56be654
1084: remove remove duplicated code with HttpUtils
npeters 1050e94
default content-type: application/json
deki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,43 +306,46 @@ 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); | ||
} | ||
System.out.println(map); | ||
return mapper.writeValueAsBytes(map); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.