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
master
Sam Brannen 8 years ago
parent 509796a4b5
commit a795fd4714
  1. 25
      spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java
  2. 10
      spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.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.
* <p>This method accepts only exact matches.
* @param expectedUrl the exact URL expected
* <p>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.
* <p>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());
* <p>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());
}
/**

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

Loading…
Cancel
Save