forked from yugabyte/serverless-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamLambdaHandlerTest.java
More file actions
90 lines (69 loc) · 3.22 KB
/
StreamLambdaHandlerTest.java
File metadata and controls
90 lines (69 loc) · 3.22 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
package com.yugabyte;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.services.lambda.runtime.Context;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.*;
public class StreamLambdaHandlerTest {
private static StreamLambdaHandler handler;
private static Context lambdaContext;
@BeforeClass
public static void setUp() {
handler = new StreamLambdaHandler();
lambdaContext = new MockLambdaContext();
}
@Test
public void ping_streamRequest_respondsWithHello() {
InputStream requestStream = new AwsProxyRequestBuilder("/ping", HttpMethod.GET)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.buildStream();
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
handle(requestStream, responseStream);
AwsProxyResponse response = readResponse(responseStream);
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode());
assertFalse(response.isBase64Encoded());
assertTrue(response.getBody().contains("pong"));
assertTrue(response.getBody().contains("Hello, World!"));
assertTrue(response.getMultiValueHeaders().containsKey(HttpHeaders.CONTENT_TYPE));
assertTrue(response.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.APPLICATION_JSON));
}
@Test
public void invalidResource_streamRequest_responds404() {
InputStream requestStream = new AwsProxyRequestBuilder("/pong", HttpMethod.GET)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.buildStream();
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
handle(requestStream, responseStream);
AwsProxyResponse response = readResponse(responseStream);
assertNotNull(response);
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode());
}
private void handle(InputStream is, ByteArrayOutputStream os) {
try {
handler.handleRequest(is, os, lambdaContext);
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
private AwsProxyResponse readResponse(ByteArrayOutputStream responseStream) {
try {
return LambdaContainerHandler.getObjectMapper().readValue(responseStream.toByteArray(), AwsProxyResponse.class);
} catch (IOException e) {
e.printStackTrace();
fail("Error while parsing response: " + e.getMessage());
}
return null;
}
}