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

Fix routing issue with Jersey when stripBasePath is configured #113

Merged
merged 1 commit into from
Jan 31, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down