From a795fd47142bd3b206ce244b94b1fd1dd0adc2e9 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 13 Oct 2016 16:50:40 +0200 Subject: [PATCH] Add support for URI templates in redirectedUrl() & forwardedUrl() Prior to this commit, the redirectedUrl() and forwardedUrl() methods in MockMvcResultMatchers supported fully constructed URLs and URLs containing Ant-style path patterns. However, URI templates were not supported for these result matchers even though the request path can be built using URL templates -- for example, via the get() and post() methods in MockMvcRequestBuilders. This commit addresses this shortcoming by allowing users to supply a URL template instead of a fully constructed URL to redirectedUrl() and forwardedUrl(). To populate the URL template, template variables may be supplied to redirectedUrl() and forwardedUrl() as var-args. Issue: SPR-14790 --- .../servlet/result/MockMvcResultMatchers.java | 25 ++++++++++++------- .../result/MockMvcResultMatchersTests.java | 10 ++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java index 7f5f4d29ab..994e6ce310 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java @@ -23,6 +23,7 @@ import org.hamcrest.Matcher; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.util.AntPathMatcher; +import org.springframework.web.util.UriComponentsBuilder; import static org.springframework.test.util.AssertionErrors.*; @@ -80,11 +81,14 @@ public abstract class MockMvcResultMatchers { /** * Asserts the request was forwarded to the given URL. - *

This method accepts only exact matches. - * @param expectedUrl the exact URL expected + *

This method accepts exact matches against the expanded URL template. + * @param urlTemplate a URL template; the expanded URL will be encoded + * @param urlVars zero or more URL variables to populate the template + * @see UriComponentsBuilder#fromUriString(String) */ - public static ResultMatcher forwardedUrl(String expectedUrl) { - return result -> assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl()); + public static ResultMatcher forwardedUrl(String urlTemplate, Object... urlVars) { + String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVars).encode().toUriString(); + return result -> assertEquals("Forwarded URL", uri, result.getResponse().getForwardedUrl()); } /** @@ -105,11 +109,14 @@ public abstract class MockMvcResultMatchers { /** * Asserts the request was redirected to the given URL. - *

This method accepts only exact matches. - * @param expectedUrl the exact URL expected - */ - public static ResultMatcher redirectedUrl(String expectedUrl) { - return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); + *

This method accepts exact matches against the expanded URL template. + * @param urlTemplate a URL template; the expanded URL will be encoded + * @param urlVars zero or more URL variables to populate the template + * @see UriComponentsBuilder#fromUriString(String) + */ + public static ResultMatcher redirectedUrl(String urlTemplate, Object... urlVars) { + String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVars).encode().toUriString(); + return result -> assertEquals("Redirected URL", uri, result.getResponse().getRedirectedUrl()); } /** diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java index a4e4563843..6ccd79a93c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java @@ -36,6 +36,11 @@ public class MockMvcResultMatchersTests { redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1")); } + @Test + public void redirectWithUrlTemplate() throws Exception { + redirectedUrl("/orders/{orderId}/items/{itemId}", 1, 2).match(getRedirectedUrlStubMvcResult("/orders/1/items/2")); + } + @Test public void redirectWithMatchingPattern() throws Exception { redirectedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1")); @@ -56,6 +61,11 @@ public class MockMvcResultMatchersTests { forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value")); } + @Test + public void forwardWithUrlTemplate() throws Exception { + forwardedUrl("/orders/{orderId}/items/{itemId}", 1, 2).match(getForwardedUrlStubMvcResult("/orders/1/items/2")); + } + @Test public void forwardWithMatchingPattern() throws Exception { forwardedUrlPattern("/api/**/?").match(getForwardedUrlStubMvcResult("/api/resource/1"));