Skip to content

Commit

Permalink
Merge pull request #16007 from richiethom/quarkus_15909
Browse files Browse the repository at this point in the history
Copy Content-Type from Netty HttpResponse to APIGatewayV2HTTPResponse
  • Loading branch information
patriot1burke committed Mar 29, 2021
2 parents ac45657 + 7dbfae1 commit 674b4cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 @@ -94,12 +97,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()) {
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 @@ -124,7 +136,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

0 comments on commit 674b4cf

Please sign in to comment.