Override toString() in all RequestMatcher

It makes it easier to debug having custom
toString().

Fixes: gh-5446
This commit is contained in:
Rob Winch 2018-06-15 11:27:28 -05:00
parent 71986e5f42
commit c3177a84a3
7 changed files with 83 additions and 0 deletions

View File

@ -123,6 +123,24 @@ public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtrac
return this.servletPath;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Mvc [pattern='").append(this.pattern).append("'");
if (this.servletPath != null) {
sb.append(", servletPath='").append(this.servletPath).append("'");
}
if (this.method != null) {
sb.append(", ").append(this.method);
}
sb.append("]");
return sb.toString();
}
private class DefaultMatcher implements RequestMatcher, RequestVariablesExtractor {
private final UrlPathHelper pathHelper = new UrlPathHelper();

View File

@ -44,6 +44,11 @@ public final class AnyRequestMatcher implements RequestMatcher {
return 1;
}
@Override
public String toString() {
return "any request";
}
private AnyRequestMatcher() {
}
}

View File

@ -65,4 +65,11 @@ public class ELRequestMatcher implements RequestMatcher {
return new StandardEvaluationContext(new ELRequestMatcherContext(request));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("EL [el=\"").append(this.expression.getExpressionString()).append("\"");
sb.append("]");
return sb.toString();
}
}

View File

@ -130,4 +130,18 @@ public final class RegexRequestMatcher implements RequestMatcher {
return null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Regex [pattern='").append(this.pattern).append("'");
if (this.httpMethod != null) {
sb.append(", ").append(this.httpMethod);
}
sb.append("]");
return sb.toString();
}
}

View File

@ -216,4 +216,31 @@ public class MvcRequestMatcherTests {
new HttpRequestMethodNotSupportedException(this.request.getMethod()));
assertThat(this.matcher.matches(this.request)).isTrue();
}
@Test
public void toStringWhenAll() {
this.matcher.setMethod(HttpMethod.GET);
this.matcher.setServletPath("/spring");
assertThat(this.matcher.toString()).isEqualTo("Mvc [pattern='/path', servletPath='/spring', GET]");
}
@Test
public void toStringWhenHttpMethod() {
this.matcher.setMethod(HttpMethod.GET);
assertThat(this.matcher.toString()).isEqualTo("Mvc [pattern='/path', GET]");
}
@Test
public void toStringWhenServletPath() {
this.matcher.setServletPath("/spring");
assertThat(this.matcher.toString()).isEqualTo("Mvc [pattern='/path', servletPath='/spring']");
}
@Test
public void toStringWhenOnlyPattern() {
assertThat(this.matcher.toString()).isEqualTo("Mvc [pattern='/path']");
}
}

View File

@ -90,4 +90,10 @@ public class ELRequestMatcherTests {
assertThat(requestMatcher.matches(request)).isFalse();
}
@Test
public void toStringThenFormatted() {
ELRequestMatcher requestMatcher = new ELRequestMatcher(
"hasHeader('User-Agent','MSIE')");
assertThat(requestMatcher.toString()).isEqualTo("EL [el=\"hasHeader('User-Agent','MSIE')\"]");
}
}

View File

@ -108,6 +108,12 @@ public class RegexRequestMatcherTests {
assertThat(matcher.matches(request)).isFalse();
}
@Test
public void toStringThenFormatted() {
RegexRequestMatcher matcher = new RegexRequestMatcher("/blah", "GET");
assertThat(matcher.toString()).isEqualTo("Regex [pattern='/blah', GET]");
}
private HttpServletRequest createRequestWithNullMethod(String path) {
when(request.getQueryString()).thenReturn("doesntMatter");
when(request.getServletPath()).thenReturn(path);