From 68658d83a132dcdd6745e548ff5590a6f2a4c3b9 Mon Sep 17 00:00:00 2001 From: sapessi Date: Wed, 31 Jan 2018 07:11:47 -0800 Subject: [PATCH] Fixes to address Jersey routing issue reported in #112. This is a new bug introduced with the fixes for #84. Added unit tests --- .../proxy/jersey/JerseyHandlerFilter.java | 14 +---------- .../proxy/jersey/JerseyAwsProxyTest.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 13 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 e36a044fe..ee682dbff 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 @@ -97,22 +97,10 @@ public void destroy() { @SuppressFBWarnings({ "SERVLET_HEADER", "SERVLET_QUERY_STRING" }) private ContainerRequest servletRequestToContainerRequest(ServletRequest request) { Timer.start("JERSEY_SERVLET_REQUEST_TO_CONTAINER"); - URI basePathUri; URI requestPathUri; String basePath = "/"; HttpServletRequest servletRequest = (HttpServletRequest)request; - try { - if (servletRequest.getContextPath().equals("")) { - basePathUri = URI.create(basePath); - } else { - basePathUri = new URI(servletRequest.getContextPath()); - } - } catch (URISyntaxException e) { - log.error("Could not read base path URI", e); - basePathUri = URI.create(basePath); - } - UriBuilder uriBuilder = UriBuilder.fromPath(servletRequest.getPathInfo()); uriBuilder.replaceQuery(AwsProxyHttpServletRequest.decodeValueIfEncoded(servletRequest.getQueryString())); @@ -125,7 +113,7 @@ private ContainerRequest servletRequestToContainerRequest(ServletRequest request apiGatewayProperties.setProperty(JERSEY_SERVLET_REQUEST_PROPERTY, servletRequest); ContainerRequest requestContext = new ContainerRequest( - basePathUri, + URI.create(basePath), // for routing within Jersey we always assume the base path is "/" requestPathUri, servletRequest.getMethod().toUpperCase(Locale.ENGLISH), (SecurityContext)servletRequest.getAttribute(JAX_SECURITY_CONTEXT_PROPERTY), 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 bbb28853c..055d63136 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 @@ -278,6 +278,30 @@ public void exception_mapException_mapToNotImplemented() { assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), response.getStatusCode()); } + @Test + public void stripBasePath_route_shouldRouteCorrectly() { + AwsProxyRequest request = new AwsProxyRequestBuilder("/custompath/echo/status-code", "GET") + .json() + .queryString("status", "201") + .build(); + handler.stripBasePath("/custompath"); + AwsProxyResponse output = handler.proxy(request, lambdaContext); + assertEquals(201, output.getStatusCode()); + handler.stripBasePath(""); + } + + @Test + public void stripBasePath_route_shouldReturn404() { + AwsProxyRequest request = new AwsProxyRequestBuilder("/custompath/echo/status-code", "GET") + .json() + .queryString("status", "201") + .build(); + handler.stripBasePath("/custom"); + AwsProxyResponse output = handler.proxy(request, lambdaContext); + assertEquals(404, output.getStatusCode()); + handler.stripBasePath(""); + } + private void validateMapResponseModel(AwsProxyResponse output) { validateMapResponseModel(output, CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE); }