66import java .io .ByteArrayOutputStream ;
77import java .io .InputStream ;
88import java .nio .charset .StandardCharsets ;
9- import java .util .Arrays ;
10- import java .util .Collection ;
11- import java .util .HashMap ;
12- import java .util .Map ;
9+ import java .util .*;
1310
1411import com .amazonaws .serverless .exceptions .ContainerInitializationException ;
1512import org .junit .jupiter .params .ParameterizedTest ;
1613import org .junit .jupiter .params .provider .MethodSource ;
17- import org .springframework .cloud .function .serverless .web .ServerlessServletContext ;
1814import org .springframework .util .CollectionUtils ;
1915
2016import com .amazonaws .serverless .proxy .spring .servletapp .MessageData ;
@@ -214,7 +210,7 @@ public static Collection<String> data() {
214210 public void validateComplesrequest (String jsonEvent ) throws Exception {
215211 initServletAppTest ();
216212 InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" ,
217- "/foo/male/list/24" , "{\" name\" :\" bob\" }" , null ));
213+ "/foo/male/list/24" , "{\" name\" :\" bob\" }" , false , null ));
218214 ByteArrayOutputStream output = new ByteArrayOutputStream ();
219215 handler .handleRequest (targetStream , output , null );
220216 Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
@@ -229,7 +225,7 @@ public void validateComplesrequest(String jsonEvent) throws Exception {
229225 @ ParameterizedTest
230226 public void testAsyncPost (String jsonEvent ) throws Exception {
231227 initServletAppTest ();
232- InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/async" , "{\" name\" :\" bob\" }" , null ));
228+ InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/async" , "{\" name\" :\" bob\" }" ,false , null ));
233229 ByteArrayOutputStream output = new ByteArrayOutputStream ();
234230 handler .handleRequest (targetStream , output , null );
235231 Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
@@ -242,7 +238,7 @@ public void testAsyncPost(String jsonEvent) throws Exception {
242238 public void testValidate400 (String jsonEvent ) throws Exception {
243239 initServletAppTest ();
244240 UserData ud = new UserData ();
245- InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/validate" , mapper .writeValueAsString (ud ), null ));
241+ InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/validate" , mapper .writeValueAsString (ud ),false , null ));
246242 ByteArrayOutputStream output = new ByteArrayOutputStream ();
247243 handler .handleRequest (targetStream , output , null );
248244 Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
@@ -258,27 +254,48 @@ public void testValidate200(String jsonEvent) throws Exception {
258254 ud .setFirstName ("bob" );
259255 ud .setLastName ("smith" );
260256261- InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/validate" , mapper .writeValueAsString (ud ), null ));
257+ InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/validate" , mapper .writeValueAsString (ud ),false , null ));
262258 ByteArrayOutputStream output = new ByteArrayOutputStream ();
263259 handler .handleRequest (targetStream , output , null );
264260 Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
265261 assertEquals (200 , result .get ("statusCode" ));
266262 assertEquals ("VALID" , result .get ("body" ));
267263 }
268264
265+ @ MethodSource ("data" )
266+ @ ParameterizedTest
267+ public void testValidate200Base64 (String jsonEvent ) throws Exception {
268+ initServletAppTest ();
269+ UserData ud = new UserData ();
270+ ud .setFirstName ("bob" );
271+ ud .setLastName ("smith" );
272+ 273+ InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/validate" ,
274+ Base64 .getMimeEncoder ().encodeToString (mapper .writeValueAsString (ud ).getBytes ()),true , null ));
275+
276+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
277+ handler .handleRequest (targetStream , output , null );
278+ Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
279+ assertEquals (200 , result .get ("statusCode" ));
280+ assertEquals ("VALID" , result .get ("body" ));
281+ }
282+
283+
269284 @ MethodSource ("data" )
270285 @ ParameterizedTest
271286 public void messageObject_parsesObject_returnsCorrectMessage (String jsonEvent ) throws Exception {
272287 initServletAppTest ();
273288 InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/message" ,
274- mapper .writeValueAsString (new MessageData ("test message" )), null ));
289+ mapper .writeValueAsString (new MessageData ("test message" )),false , null ));
275290 ByteArrayOutputStream output = new ByteArrayOutputStream ();
276291 handler .handleRequest (targetStream , output , null );
277292 Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
278293 assertEquals (200 , result .get ("statusCode" ));
279294 assertEquals ("test message" , result .get ("body" ));
280295 }
281296
297+
298+
282299 @ SuppressWarnings ({"unchecked" })
283300 @ MethodSource ("data" )
284301 @ ParameterizedTest
@@ -289,40 +306,42 @@ void messageObject_propertiesInContentType_returnsCorrectMessage(String jsonEven
289306 headers .put (HttpHeaders .CONTENT_TYPE , "application/json;v=1" );
290307 headers .put (HttpHeaders .ACCEPT , "application/json;v=1" );
291308 InputStream targetStream = new ByteArrayInputStream (this .generateHttpRequest (jsonEvent , "POST" , "/message" ,
292- mapper .writeValueAsString (new MessageData ("test message" )), headers ));
309+ mapper .writeValueAsString (new MessageData ("test message" )),false , headers ));
293310
294311 ByteArrayOutputStream output = new ByteArrayOutputStream ();
295312 handler .handleRequest (targetStream , output , null );
296313 Map result = mapper .readValue (output .toString (StandardCharsets .UTF_8 ), Map .class );
297314 assertEquals ("test message" , result .get ("body" ));
298315 }
299316
300- private byte [] generateHttpRequest (String jsonEvent , String method , String path , String body , Map headers ) throws Exception {
317+ private byte [] generateHttpRequest (String jsonEvent , String method , String path , String body ,boolean isBase64Encoded , Map headers ) throws Exception {
301318 Map requestMap = mapper .readValue (jsonEvent , Map .class );
302319 if (requestMap .get ("version" ).equals ("2.0" )) {
303- return generateHttpRequest2 (requestMap , method , path , body , headers );
320+ return generateHttpRequest2 (requestMap , method , path , body , isBase64Encoded , headers );
304321 }
305- return generateHttpRequest (requestMap , method , path , body , headers );
322+ return generateHttpRequest (requestMap , method , path , body ,isBase64Encoded , headers );
306323 }
307324
308325 @ SuppressWarnings ({ "unchecked" })
309- private byte [] generateHttpRequest (Map requestMap , String method , String path , String body , Map headers ) throws Exception {
326+ private byte [] generateHttpRequest (Map requestMap , String method , String path , String body ,boolean isBase64Encoded , Map headers ) throws Exception {
310327 requestMap .put ("path" , path );
311328 requestMap .put ("httpMethod" , method );
312329 requestMap .put ("body" , body );
330+ requestMap .put ("isBase64Encoded" , isBase64Encoded );
313331 if (!CollectionUtils .isEmpty (headers )) {
314332 requestMap .put ("headers" , headers );
315333 }
316334 return mapper .writeValueAsBytes (requestMap );
317335 }
318336
319337 @ SuppressWarnings ({ "unchecked" })
320- private byte [] generateHttpRequest2 (Map requestMap , String method , String path , String body , Map headers ) throws Exception {
338+ private byte [] generateHttpRequest2 (Map requestMap , String method , String path , String body ,boolean isBase64Encoded , Map headers ) throws Exception {
321339 Map map = mapper .readValue (API_GATEWAY_EVENT_V2 , Map .class );
322340 Map http = (Map ) ((Map ) map .get ("requestContext" )).get ("http" );
323341 http .put ("path" , path );
324342 http .put ("method" , method );
325343 map .put ("body" , body );
344+ map .put ("isBase64Encoded" , isBase64Encoded );
326345 if (!CollectionUtils .isEmpty (headers )) {
327346 map .put ("headers" , headers );
328347 }
0 commit comments