|
3 | 3 | import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
|
4 | 4 | import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
|
5 | 5 | import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
|
| 6 | +import com.amazonaws.serverless.proxy.model.AwsProxyRequest; |
6 | 7 | import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
|
7 |
| -import com.amazonaws.serverless.proxy.spring.servletapp.LambdaHandler; |
8 |
| -import com.amazonaws.serverless.proxy.spring.servletapp.MessageController; |
9 |
| -import com.amazonaws.serverless.proxy.spring.servletapp.MessageData; |
10 |
| -import com.amazonaws.serverless.proxy.spring.servletapp.UserData; |
| 8 | +import com.amazonaws.serverless.proxy.model.ContainerConfig; |
| 9 | +import com.amazonaws.serverless.proxy.spring.servletapp.*; |
11 | 10 | import com.fasterxml.jackson.core.JsonProcessingException;
|
12 | 11 | import org.junit.Assert;
|
13 | 12 | import org.junit.Test;
|
|
17 | 16 | import javax.ws.rs.core.HttpHeaders;
|
18 | 17 | import javax.ws.rs.core.MediaType;
|
19 | 18 |
|
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
20 | 22 | import java.util.Arrays;
|
21 | 23 | import java.util.Collection;
|
| 24 | +import java.util.stream.Collectors; |
22 | 25 |
|
23 | 26 | import static org.junit.Assert.assertEquals;
|
24 | 27 | import static org.junit.Assert.assertNotNull;
|
@@ -108,4 +111,85 @@ public void echoMessage_fileNameLikeParameter_returnsMessage() {
|
108 | 111 | assertEquals(200, resp.getStatusCode());
|
109 | 112 | assertEquals("test.test.test", resp.getBody());
|
110 | 113 | }
|
| 114 | + |
| 115 | + @Test |
| 116 | + public void getUtf8String_returnsValidUtf8String() { |
| 117 | + // We expect strings to come back as UTF-8 correctly because Spring itself will call the setCharacterEncoding |
| 118 | + // method on the response to set it to UTF- |
| 119 | + LambdaContainerHandler.getContainerConfig().setDefaultContentCharset(ContainerConfig.DEFAULT_CONTENT_CHARSET); |
| 120 | + AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/content-type/utf8", "GET") |
| 121 | + .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN); |
| 122 | + AwsProxyResponse resp = handler.handleRequest(req, lambdaContext); |
| 123 | + assertNotNull(resp); |
| 124 | + assertEquals(200, resp.getStatusCode()); |
| 125 | + assertEquals("text/plain; charset=UTF-8", resp.getMultiValueHeaders().get(HttpHeaders.CONTENT_TYPE).stream().collect(Collectors.joining(","))); |
| 126 | + assertEquals(MessageController.UTF8_RESPONSE, resp.getBody()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void getUtf8Json_returnsValidUtf8String() { |
| 131 | + LambdaContainerHandler.getContainerConfig().setDefaultContentCharset(ContainerConfig.DEFAULT_CONTENT_CHARSET); |
| 132 | + AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/content-type/jsonutf8", "GET"); |
| 133 | + AwsProxyResponse resp = handler.handleRequest(req, lambdaContext); |
| 134 | + assertNotNull(resp); |
| 135 | + assertEquals(200, resp.getStatusCode()); |
| 136 | + assertEquals("{\"s\":\""+MessageController.UTF8_RESPONSE+"\"}", resp.getBody()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void stream_getUtf8String_returnsValidUtf8String() throws IOException { |
| 141 | + LambdaContainerHandler.getContainerConfig().setDefaultContentCharset(ContainerConfig.DEFAULT_CONTENT_CHARSET); |
| 142 | + LambdaStreamHandler streamHandler = new LambdaStreamHandler(type); |
| 143 | + AwsProxyRequestBuilder reqBuilder = new AwsProxyRequestBuilder("/content-type/utf8", "GET") |
| 144 | + .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN); |
| 145 | + InputStream req = null; |
| 146 | + switch (type) { |
| 147 | + case "ALB": |
| 148 | + req = reqBuilder.alb().buildStream(); |
| 149 | + break; |
| 150 | + case "API_GW": |
| 151 | + req = reqBuilder.buildStream(); |
| 152 | + break; |
| 153 | + case "HTTP_API": |
| 154 | + req = reqBuilder.toHttpApiV2RequestStream(); |
| 155 | + } |
| 156 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 157 | + streamHandler.handleRequest(req, out, lambdaContext); |
| 158 | + AwsProxyResponse resp = LambdaContainerHandler.getObjectMapper().readValue(out.toByteArray(), AwsProxyResponse.class); |
| 159 | + assertNotNull(resp); |
| 160 | + assertEquals(200, resp.getStatusCode()); |
| 161 | + assertEquals(MessageController.UTF8_RESPONSE, resp.getBody()); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void stream_getUtf8Json_returnsValidUtf8String() throws IOException { |
| 166 | + LambdaContainerHandler.getContainerConfig().setDefaultContentCharset(ContainerConfig.DEFAULT_CONTENT_CHARSET); |
| 167 | + LambdaStreamHandler streamHandler = new LambdaStreamHandler(type); |
| 168 | + AwsProxyRequestBuilder reqBuilder = new AwsProxyRequestBuilder("/content-type/jsonutf8", "GET"); |
| 169 | + InputStream req = null; |
| 170 | + switch (type) { |
| 171 | + case "ALB": |
| 172 | + req = reqBuilder.alb().buildStream(); |
| 173 | + break; |
| 174 | + case "API_GW": |
| 175 | + req = reqBuilder.buildStream(); |
| 176 | + break; |
| 177 | + case "HTTP_API": |
| 178 | + req = reqBuilder.toHttpApiV2RequestStream(); |
| 179 | + } |
| 180 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 181 | + streamHandler.handleRequest(req, out, lambdaContext); |
| 182 | + AwsProxyResponse resp = LambdaContainerHandler.getObjectMapper().readValue(out.toByteArray(), AwsProxyResponse.class); |
| 183 | + assertNotNull(resp); |
| 184 | + assertEquals(200, resp.getStatusCode()); |
| 185 | + assertEquals("{\"s\":\""+MessageController.UTF8_RESPONSE+"\"}", resp.getBody()); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void springExceptionMapping_throw404Ex_expectMappedTo404() { |
| 190 | + AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/ex/customstatus", "GET"); |
| 191 | + AwsProxyResponse resp = handler.handleRequest(req, lambdaContext); |
| 192 | + assertNotNull(resp); |
| 193 | + assertEquals(404, resp.getStatusCode()); |
| 194 | + } |
111 | 195 | }
|
0 commit comments