Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy Content-Type from Netty HttpResponse to APIGatewayV2HTTPResponse #16007

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -92,12 +95,21 @@ public void handleMessage(Object msg) {
HttpResponse res = (HttpResponse) msg;
responseBuilder.setStatusCode(res.status().code());

Headers multiValueHeaders = new Headers();
responseBuilder.setMultiValueHeaders(multiValueHeaders);
final Map<String, String> headers = new HashMap<>();
responseBuilder.setHeaders(headers);
for (String name : res.headers().names()) {
patriot1burke marked this conversation as resolved.
Show resolved Hide resolved
for (String v : res.headers().getAll(name)) {
multiValueHeaders.add(name, v);
final List<String> allForName = res.headers().getAll(name);
if (allForName == null || allForName.isEmpty()) {
continue;
}
final StringBuilder sb = new StringBuilder();
for (Iterator<String> valueIterator = allForName.iterator(); valueIterator.hasNext();) {
sb.append(valueIterator.next());
if (valueIterator.hasNext()) {
sb.append(",");
}
}
headers.put(name, sb.toString());
}
}
if (msg instanceof HttpContent) {
Expand All @@ -122,7 +134,7 @@ public void handleMessage(Object msg) {
}
if (msg instanceof LastHttpContent) {
if (baos != null) {
if (isBinary(((Headers) responseBuilder.getMultiValueHeaders()).getFirst("Content-Type"))) {
if (isBinary(responseBuilder.getHeaders().get("Content-Type"))) {
responseBuilder.setIsBase64Encoded(true);
responseBuilder.setBody(Base64.getMimeEncoder().encodeToString(baos.toByteArray()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private void testGetText(String path) {
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(body(out), "hello");
Assertions.assertTrue(out.getMultiValueHeaders().get("Content-Type").get(0).startsWith("text/plain"));
Assertions.assertTrue(out.getHeaders().get("Content-Type").startsWith("text/plain"));
}

private APIGatewayV2HTTPEvent request(String path) {
Expand Down Expand Up @@ -77,7 +77,7 @@ private void testPostText(String path) {
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(body(out), "hello Bill");
Assertions.assertTrue(out.getMultiValueHeaders().get("Content-Type").get(0).startsWith("text/plain"));
Assertions.assertTrue(out.getHeaders().get("Content-Type").startsWith("text/plain"));
}

@Test
Expand All @@ -92,7 +92,7 @@ public void testPostBinary() throws Exception {
request.setIsBase64Encoded(true);
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(out.getMultiValueHeaders().get("Content-Type").get(0),
Assertions.assertEquals(out.getHeaders().get("Content-Type"),
MediaType.APPLICATION_OCTET_STREAM);
Assertions.assertTrue(out.getIsBase64Encoded());
byte[] rtn = Base64.decodeBase64(out.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void testGetText(String path) {
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(body(out), "hello");
Assertions.assertTrue(out.getMultiValueHeaders().get("Content-Type").get(0).startsWith("text/plain"));
Assertions.assertTrue(out.getHeaders().get("Content-Type").startsWith("text/plain"));
}

private APIGatewayV2HTTPEvent request(String path) {
Expand Down Expand Up @@ -93,7 +93,7 @@ private void testPostText(String path) {
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(body(out), "hello Bill");
Assertions.assertTrue(out.getMultiValueHeaders().get("Content-Type").get(0).startsWith("text/plain"));
Assertions.assertTrue(out.getHeaders().get("Content-Type").startsWith("text/plain"));
}

@Test
Expand All @@ -108,7 +108,7 @@ public void testPostBinary() throws Exception {
request.setIsBase64Encoded(true);
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(out.getMultiValueHeaders().get("Content-Type").get(0),
Assertions.assertEquals(out.getHeaders().get("Content-Type"),
MediaType.APPLICATION_OCTET_STREAM);
Assertions.assertTrue(out.getIsBase64Encoded());
byte[] rtn = Base64.decodeBase64(out.getBody());
Expand Down Expand Up @@ -138,7 +138,7 @@ public void testFunqy(String path) {
APIGatewayV2HTTPResponse out = LambdaClient.invoke(APIGatewayV2HTTPResponse.class, request);
Assertions.assertEquals(out.getStatusCode(), 200);
Assertions.assertEquals(body(out), "\"Make it funqy Bill\"");
Assertions.assertTrue(out.getMultiValueHeaders().get("Content-Type").get(0).startsWith("application/json"));
Assertions.assertTrue(out.getHeaders().get("Content-Type").startsWith("application/json"));
}

@Test
Expand Down