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"));