Improve failure messages for redirect/forward in MockMvc

This commit is contained in:
Sam Brannen
2019-07-17 16:53:33 +02:00
parent 7779aa3487
commit e92cbe1938
2 changed files with 54 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -106,15 +106,16 @@ public abstract class MockMvcResultMatchers {
* Asserts the request was forwarded to the given URL.
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
* patterns.
* @param urlPattern an AntPath pattern to match against
* @param urlPattern an Ant-style path pattern to match against
* @since 4.0
* @see org.springframework.util.AntPathMatcher
*/
public static ResultMatcher forwardedUrlPattern(String urlPattern) {
return result -> {
assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern));
assertTrue("'" + urlPattern + "' is not an Ant-style path pattern",
pathMatcher.isPattern(urlPattern));
String url = result.getResponse().getForwardedUrl();
assertTrue("Forwarded URL does not match the expected URL pattern",
assertTrue("Forwarded URL '" + url + "' does not match the expected URL pattern '" + urlPattern + "'",
(url != null && pathMatcher.match(urlPattern, url)));
};
}
@@ -144,15 +145,16 @@ public abstract class MockMvcResultMatchers {
* Asserts the request was redirected to the given URL.
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
* patterns.
* @param urlPattern an AntPath pattern to match against
* @param urlPattern an Ant-style path pattern to match against
* @since 4.0
* @see org.springframework.util.AntPathMatcher
*/
public static ResultMatcher redirectedUrlPattern(String urlPattern) {
return result -> {
assertTrue("No Ant-style path pattern", pathMatcher.isPattern(urlPattern));
assertTrue("'" + urlPattern + "' is not an Ant-style path pattern",
pathMatcher.isPattern(urlPattern));
String url = result.getResponse().getRedirectedUrl();
assertTrue("Redirected URL does not match the expected URL pattern",
assertTrue("Redirected URL '" + url + "' does not match the expected URL pattern '" + urlPattern + "'",
(url != null && pathMatcher.match(urlPattern, url)));
};
}