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
This commit is contained in:
Sam Brannen
2016-10-13 16:50:40 +02:00
parent 509796a4b5
commit a795fd4714
2 changed files with 25 additions and 8 deletions

View File

@@ -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
* <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 expectedUrl) {
return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
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());
}
/**