From ba7a33c88966135e23e573c16c1e2e8ea11da519 Mon Sep 17 00:00:00 2001 From: sapessi Date: Wed, 31 Oct 2018 10:29:52 -0700 Subject: [PATCH] Changed ContainerRequest creation to use the method for the headers. Simple add was sending to an unimplemented addNull if the list didn't already exist. Also added unit test for issue #208 --- .../proxy/jersey/JerseyHandlerFilter.java | 5 +++-- .../proxy/jersey/EchoJerseyResource.java | 12 ++++++++++++ .../proxy/jersey/JerseyAwsProxyTest.java | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyHandlerFilter.java b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyHandlerFilter.java index 8dbf9a8d7..88fa48c25 100644 --- a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyHandlerFilter.java +++ b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyHandlerFilter.java @@ -30,6 +30,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; import java.util.Locale; import java.util.concurrent.CountDownLatch; @@ -167,8 +169,7 @@ private ContainerRequest servletRequestToContainerRequest(ServletRequest request while (headerNames.hasMoreElements()) { String headerKey = headerNames.nextElement(); - //requestContext.header(headerKey, servletRequest.getHeader(headerKey)); - requestContext.getHeaders().add(headerKey, servletRequest.getHeader(headerKey)); + requestContext.getHeaders().addAll(headerKey, Collections.list(servletRequest.getHeaders(headerKey))); } Timer.stop("JERSEY_SERVLET_REQUEST_TO_CONTAINER"); diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java index d33105baf..1b25bd924 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java @@ -17,6 +17,8 @@ import com.amazonaws.serverless.proxy.jersey.model.MapResponseModel; import com.amazonaws.serverless.proxy.jersey.model.SingleValueModel; +import org.glassfish.jersey.server.ContainerRequest; + import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -212,4 +214,14 @@ public Response encodedPathParam(@Encoded @PathParam("resource") String resource sv.setValue(resource); return Response.ok(sv).build(); } + + @Path("/referer-header") @GET + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response referer(@HeaderParam("Referer") String referer) { + System.out.println("Received referer: " + referer); + SingleValueModel sv = new SingleValueModel(); + sv.setValue(referer); + return Response.ok(sv).build(); + } } diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java index 6b8977d01..3187797f2 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java @@ -288,6 +288,20 @@ public void emptyStream_putNullBody_expectPutToSucceed() { validateSingleValueModel(resp, CUSTOM_HEADER_KEY); } + @Test + public void refererHeader_headerParam_expectCorrectInjection() { + String refererValue = "test-referer"; + AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/referer-header", "GET") + .nullBody() + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) + .header("Referer", refererValue) + .build(); + + AwsProxyResponse resp = handler.proxy(request, lambdaContext); + assertEquals(200, resp.getStatusCode()); + validateSingleValueModel(resp, refererValue); + } + private void validateMapResponseModel(AwsProxyResponse output) { validateMapResponseModel(output, CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE); }