Skip to content

Commit

Permalink
Path RequestPredicate should honor servlet path
Browse files Browse the repository at this point in the history
Closes gh-23841
  • Loading branch information
poutsma committed Oct 31, 2019
1 parent 95af079 commit 3858a69
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriBuilder;
import org.springframework.web.util.UrlPathHelper;

/**
* {@code ServerRequest} implementation based on a {@link HttpServletRequest}.
Expand Down Expand Up @@ -111,6 +113,16 @@ public UriBuilder uriBuilder() {
return ServletUriComponentsBuilder.fromRequest(servletRequest());
}

@Override
public String path() {
String path = (String) servletRequest().getAttribute(HandlerMapping.LOOKUP_PATH);
if (path == null) {
UrlPathHelper helper = new UrlPathHelper();
path = helper.getLookupPathForRequest(servletRequest());
}
return path;
}

@Override
public Headers headers() {
return this.headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ private void initMessageConverters() {
@Nullable
@Override
protected Object getHandlerInternal(@NotNull HttpServletRequest servletRequest) throws Exception {
String lookupPath = getUrlPathHelper().getLookupPathForRequest(servletRequest);
servletRequest.setAttribute(LOOKUP_PATH, lookupPath);
if (this.routerFunction != null) {
ServerRequest request = ServerRequest.create(servletRequest, this.messageConverters);
servletRequest.setAttribute(RouterFunctions.REQUEST_ATTRIBUTE, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ public void path() {
assertThat(predicate.test(request)).isFalse();
}

@Test
public void servletPath() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo/bar");
servletRequest.setServletPath("/foo");
ServerRequest request = new DefaultServerRequest(servletRequest, emptyList());
RequestPredicate predicate = RequestPredicates.path("/bar");
assertThat(predicate.test(request)).isTrue();

servletRequest = new MockHttpServletRequest("GET", "/foo");
request = new DefaultServerRequest(servletRequest, emptyList());
assertThat(predicate.test(request)).isFalse();
}

@Test
public void pathNoLeadingSlash() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/path");
Expand Down

0 comments on commit 3858a69

Please sign in to comment.