-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathSpringDelegatingLambdaContainerHandlerTests.java
More file actions
349 lines (324 loc) · 14.5 KB
/
SpringDelegatingLambdaContainerHandlerTests.java
File metadata and controls
349 lines (324 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.amazonaws.serverless.proxy.spring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
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.util.CollectionUtils;
import com.amazonaws.serverless.proxy.spring.servletapp.MessageData;
import com.amazonaws.serverless.proxy.spring.servletapp.ServletApplication;
import com.amazonaws.serverless.proxy.spring.servletapp.UserData;
import tools.jackson.databind.ObjectMapper;
import jakarta.ws.rs.core.HttpHeaders;
@SuppressWarnings("rawtypes")
public class SpringDelegatingLambdaContainerHandlerTests {
private static final String API_GATEWAY_EVENT = """
{
"version": "1.0",
"resource": "$default",
"path": "/async",
"httpMethod": "POST",
"headers": {
"Content-Length": "45",
"Content-Type": "application/json",
"Host": "i76bfh111.execute-api.eu-west-3.amazonaws.com",
"User-Agent": "curl/7.79.1",
"X-Amzn-Trace-Id": "Root=1-64087690-2151375b219d3ba3389ea84e",
"X-Forwarded-For": "109.210.252.44",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"accept": "*/*"
},
"multiValueHeaders": {
"Content-Length": [
"45"
],
"Content-Type": [
"application/json"
],
"Host": [
"i76bfhczs0.execute-api.eu-west-3.amazonaws.com"
],
"User-Agent": [
"curl/7.79.1"
],
"X-Amzn-Trace-Id": [
"Root=1-64087690-2151375b219d3ba3389ea84e"
],
"X-Forwarded-For": [
"109.210.252.44"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
],
"accept": [
"*/*"
]
},
"queryStringParameters": {
"abc": "xyz",
"name": "Ricky",
"foo": "baz"
},
"multiValueQueryStringParameters": {
"abc": [
"xyz"
],
"name": [
"Ricky"
],
"foo": [
"bar",
"baz"
]
},
"requestContext": {
"accountId": "123456789098",
"apiId": "i76bfhczs0",
"domainName": "i76bfhc111.execute-api.eu-west-3.amazonaws.com",
"domainPrefix": "i76bfhczs0",
"extendedRequestId": "Bdd2ngt5iGYEMIg=",
"httpMethod": "POST",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAmr": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "109.210.252.44",
"user": null,
"userAgent": "curl/7.79.1",
"userArn": null
},
"path": "/pets",
"protocol": "HTTP/1.1",
"requestId": "Bdd2ngt5iGYEMIg=",
"requestTime": "08/Mar/2023:11:50:40 +0000",
"requestTimeEpoch": 1678276240455,
"resourceId": "$default",
"resourcePath": "$default",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "{\\"name\\":\\"bob\\"}",
"isBase64Encoded": false
}""";
private static final String API_GATEWAY_EVENT_V2 = """
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/my/path",
"rawQueryString": "parameter1=value1¶meter1=value2&name=Ricky¶meter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"header1": "value1",
"header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"name": "Ricky",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/my/path",
"protocol": "HTTP/1.1",
"sourceIp": "IP",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390
},
"body": "Hello from Lambda",
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": false,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}""";
private SpringDelegatingLambdaContainerHandler handler;
private ObjectMapper mapper = new ObjectMapper();
public void initServletAppTest() throws ContainerInitializationException {
this.handler = new SpringDelegatingLambdaContainerHandler(ServletApplication.class);
}
public static Collection<String> data() {
return Arrays.asList(API_GATEWAY_EVENT, API_GATEWAY_EVENT_V2);
}
@MethodSource("data")
@ParameterizedTest
public void validateComplesrequest(String jsonEvent) throws Exception {
initServletAppTest();
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST",
"/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);
assertEquals(200, result.get("statusCode"));
String[] responseBody = ((String) result.get("body")).split("/");
assertEquals("male", responseBody[0]);
assertEquals("24", responseBody[1]);
assertEquals("Ricky", responseBody[2]);
}
@MethodSource("data")
@ParameterizedTest
public void testValidate400(String jsonEvent) throws Exception {
initServletAppTest();
UserData ud = new UserData();
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(400, result.get("statusCode"));
assertEquals("3", result.get("body"));
}
@MethodSource("data")
@ParameterizedTest
public void testValidate200(String jsonEvent) throws Exception {
initServletAppTest();
UserData ud = new UserData();
ud.setFirstName("bob");
ud.setLastName("smith");
ud.setEmail("foo@bar.com");
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("foo@bar.com");
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")),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"));
}
@MethodSource("data")
@ParameterizedTest
public void voidPost_returns200(String jsonEvent) throws Exception {
initServletAppTest();
InputStream targetStream = new ByteArrayInputStream(this.generateHttpRequest(jsonEvent, "POST",
"/void-post", "{\"key\":\"value\"}", 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"));
}
@SuppressWarnings({"unchecked" })
@MethodSource("data")
@ParameterizedTest
void messageObject_propertiesInContentType_returnsCorrectMessage(String jsonEvent) throws Exception {
initServletAppTest();
Map headers = new HashMap<>();
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")),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,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, isBase64Encoded,headers);
}
return generateHttpRequest(requestMap, method, path, body,isBase64Encoded, headers);
}
@SuppressWarnings({ "unchecked"})
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,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);
}
return mapper.writeValueAsBytes(map);
}
}