Separate forwarded~ and redirectedUrlTemplate methods

This commit restores and existing redirectedUrl and forwardedUrl
methods (simple String comparison) and creates separate
redirectedUrlTemplate and forwardedUrlTemplate methods that expand
and also encode before comparing.

Issue: SPR-15834
This commit is contained in:
Rossen Stoyanchev 2017-08-15 15:25:39 +02:00
parent 8bdaba913c
commit 9c3bd8ce85
2 changed files with 26 additions and 8 deletions

View File

@ -81,14 +81,23 @@ public abstract class MockMvcResultMatchers {
/**
* Asserts the request was forwarded to the given URL.
* <p>This method accepts exact matches against the expanded URL template.
* <p>This method accepts only exact matches.
* @param expectedUrl the exact URL expected
*/
public static ResultMatcher forwardedUrl(String expectedUrl) {
return result -> assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
}
/**
* Asserts the request was forwarded to the given URL template.
* <p>This method accepts exact matches against the expanded and encoded URL template.
* @param urlTemplate a URL template; the expanded URL will be encoded
* @param uriVars zero or more URI variables to populate the template
* @see UriComponentsBuilder#fromUriString(String)
*/
public static ResultMatcher forwardedUrl(String urlTemplate, Object... uriVars) {
public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... uriVars) {
String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
return result -> assertEquals("Forwarded URL", uri, result.getResponse().getForwardedUrl());
return forwardedUrl(uri);
}
/**
@ -110,14 +119,23 @@ public abstract class MockMvcResultMatchers {
/**
* Asserts the request was redirected to the given URL.
* <p>This method accepts exact matches against the expanded URL template.
* <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());
}
/**
* Asserts the request was redirected to the given URL template.
* <p>This method accepts exact matches against the expanded and encoded URL template.
* @param urlTemplate a URL template; the expanded URL will be encoded
* @param uriVars zero or more URI variables to populate the template
* @see UriComponentsBuilder#fromUriString(String)
*/
public static ResultMatcher redirectedUrl(String urlTemplate, Object... uriVars) {
public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars) {
String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
return result -> assertEquals("Redirected URL", uri, result.getResponse().getRedirectedUrl());
return redirectedUrl(uri);
}
/**

View File

@ -38,7 +38,7 @@ public class MockMvcResultMatchersTests {
@Test
public void redirectWithUrlTemplate() throws Exception {
redirectedUrl("/orders/{orderId}/items/{itemId}", 1, 2).match(getRedirectedUrlStubMvcResult("/orders/1/items/2"));
redirectedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(getRedirectedUrlStubMvcResult("/orders/1/items/2"));
}
@Test
@ -63,7 +63,7 @@ public class MockMvcResultMatchersTests {
@Test
public void forwardWithUrlTemplate() throws Exception {
forwardedUrl("/orders/{orderId}/items/{itemId}", 1, 2).match(getForwardedUrlStubMvcResult("/orders/1/items/2"));
forwardedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(getForwardedUrlStubMvcResult("/orders/1/items/2"));
}
@Test