Skip to content

Commit

Permalink
Replace MultiValueHeaders with normal Headers to match AWS APIGateway V2
Browse files Browse the repository at this point in the history
  • Loading branch information
richiethom committed Mar 27, 2021
1 parent 2fbb0a6 commit 4a5ad6e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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()) {
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 @@ -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 4a5ad6e

Please sign in to comment.