From 9c3bd8ce853a3c0cd666842c7b29d3b140cf6036 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 15 Aug 2017 15:25:39 +0200 Subject: [PATCH] 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 --- .../servlet/result/MockMvcResultMatchers.java | 30 +++++++++++++++---- .../result/MockMvcResultMatchersTests.java | 4 +-- 2 files changed, 26 insertions(+), 8 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 49d42050647..e364d868444 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 @@ -81,14 +81,23 @@ public abstract class MockMvcResultMatchers { /** * Asserts the request was forwarded to the given URL. - *

This method accepts exact matches against the expanded URL template. + *

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. + *

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. - *

This method accepts exact matches against the expanded URL template. + *

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. + *

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); } /** 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 6ccd79a93c9..019b686b26a 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 @@ -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