Skip to content

Commit

Permalink
Merge branch '2.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Oct 19, 2018
2 parents 4e27d17 + 2110215 commit 660b912
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.pattern.PathPattern;

/**
* Factory methods for {@link Tag Tags} associated with a request-response exchange that
Expand All @@ -39,6 +40,8 @@
*/
public final class WebMvcTags {

private static final String DATA_REST_PATH_PATTERN_ATTRIBUTE = "org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.EFFECTIVE_REPOSITORY_RESOURCE_LOOKUP_PATH";

private static final Tag URI_NOT_FOUND = Tag.of("uri", "NOT_FOUND");

private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");
Expand Down Expand Up @@ -138,6 +141,11 @@ private static HttpStatus extractStatus(HttpServletResponse response) {
}

private static String getMatchingPattern(HttpServletRequest request) {
PathPattern dataRestPathPattern = (PathPattern) request
.getAttribute(DATA_REST_PATH_PATTERN_ATTRIBUTE);
if (dataRestPathPattern != null) {
return dataRestPathPattern.getPatternString();
}
return (String) request
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.pattern.PathPatternParser;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -39,6 +40,17 @@ public class WebMvcTagsTests {

private final MockHttpServletResponse response = new MockHttpServletResponse();

@Test
public void uriTagIsDataRestsEffectiveRepositoryLookupPathWhenAvailable() {
this.request.setAttribute(
"org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.EFFECTIVE_REPOSITORY_RESOURCE_LOOKUP_PATH",
new PathPatternParser().parse("/api/cities"));
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,
"/api/{repository}");
Tag tag = WebMvcTags.uri(this.request, this.response);
assertThat(tag.getValue()).isEqualTo("/api/cities");
}

@Test
public void uriTagValueIsBestMatchingPatternWhenAvailable() {
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,
Expand Down

0 comments on commit 660b912

Please sign in to comment.