|
2 | 2 |
|
3 | 3 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; |
4 | 4 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.BeforeClass; |
5 | 7 | import org.junit.Test; |
6 | 8 |
|
7 | 9 | import java.util.Collections; |
|
12 | 14 | import static com.github.stefanbirkner.systemlambda.SystemLambda.*; |
13 | 15 |
|
14 | 16 | public class AppTest { |
15 | | - @Test |
16 | | - public void successfulResponse() throws Exception { |
17 | | - App app = withEnvironmentVariable("GATE_APP_NAME", "annie") |
18 | | - .execute(App::new); |
19 | | - |
20 | | - // Create the Input |
21 | | - final HashMap<String, String> input_headers = new HashMap<>(); |
22 | | - input_headers.put("Content-Type", "text/plain"); |
23 | | - APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent() |
24 | | - .withHttpMethod("POST") |
25 | | - .withHeaders(Collections.unmodifiableMap(input_headers)) |
26 | | - .withBody("This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision."); |
27 | | - |
28 | | - // Context |
29 | | - final TestContext context = new TestContext(); |
30 | | - |
31 | | - // Invoke the App |
32 | | - final APIGatewayProxyResponseEvent result = app.handleRequest(input, context); |
33 | | - |
34 | | - // Assert Results |
35 | | - assertEquals(result.getStatusCode().intValue(), 200); |
36 | | - assertEquals(result.getHeaders().get("Content-Type"), "application/xml"); |
37 | | - final String content = result.getBody(); |
38 | | - assertNotNull(content); |
39 | | - assertTrue(content.contains("GateDocument version=\"3\"")); |
40 | | - } |
| 17 | + |
| 18 | + @BeforeClass |
| 19 | + public static void setUpClass() throws Exception { |
| 20 | + app = withEnvironmentVariable("GATE_APP_NAME", "annie") |
| 21 | + .execute(App::new); |
| 22 | + } |
| 23 | + |
| 24 | + private static App app = null; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void successfulResponse() throws Exception { |
| 28 | + // Create the Input |
| 29 | + final HashMap<String, String> input_headers = new HashMap<>(); |
| 30 | + input_headers.put("Content-Type", "text/plain"); |
| 31 | + APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent() |
| 32 | + .withHttpMethod("POST") |
| 33 | + .withHeaders(Collections.unmodifiableMap(input_headers)) |
| 34 | + .withBody("This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision."); |
| 35 | + |
| 36 | + // Context |
| 37 | + final TestContext context = new TestContext(); |
| 38 | + |
| 39 | + // Invoke the App |
| 40 | + final APIGatewayProxyResponseEvent result = app.handleRequest(input, context); |
| 41 | + |
| 42 | + // Assert Results |
| 43 | + assertEquals(result.getStatusCode().intValue(), 200); |
| 44 | + assertEquals(result.getHeaders().get("Content-Type"), "application/xml"); |
| 45 | + final String content = result.getBody(); |
| 46 | + assertNotNull(content); |
| 47 | + assertTrue(content.contains("GateDocument version=\"3\"")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testMissingContentType() throws Exception { |
| 52 | + APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent() |
| 53 | + .withHttpMethod("POST") |
| 54 | + .withHeaders(Collections.unmodifiableMap(new HashMap<>())) |
| 55 | + .withBody("I am still valid text..."); |
| 56 | + final TestContext context = new TestContext(); |
| 57 | + final APIGatewayProxyResponseEvent result = app.handleRequest(input, context); |
| 58 | + assertEquals(result.getStatusCode().intValue(), 200); |
| 59 | + } |
41 | 60 | } |
0 commit comments