Browse Source

Improve failure messages for redirect/forward in MockMvc

pull/23315/head
Sam Brannen 5 years ago
parent
commit
e92cbe1938
  1. 16
      spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java
  2. 76
      spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java

16
spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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)));
};
}

76
spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java

@ -21,6 +21,7 @@ import org.junit.Test; @@ -21,6 +21,7 @@ import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.StubMvcResult;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrlPattern;
@ -39,99 +40,112 @@ public class MockMvcResultMatchersTests { @@ -39,99 +40,112 @@ public class MockMvcResultMatchersTests {
@Test
public void redirect() throws Exception {
redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1"));
assertThatCode(() -> redirectedUrl("/resource/1").match(redirectedUrlStub("/resource/1")))
.doesNotThrowAnyException();
}
@Test
public void redirectNonMatching() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrl("/resource/2").match(getRedirectedUrlStubMvcResult("/resource/1")));
public void redirectNonMatching() throws Exception {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> redirectedUrl("/resource/2").match(redirectedUrlStub("/resource/1")))
.withMessageEndingWith("expected:</resource/2> but was:</resource/1>");
}
@Test
public void redirectNonMatchingBecauseNotRedirect() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrl("/resource/1").match(getForwardedUrlStubMvcResult("/resource/1")));
public void redirectNonMatchingBecauseNotRedirect() throws Exception {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> redirectedUrl("/resource/1").match(forwardedUrlStub("/resource/1")))
.withMessageEndingWith("expected:</resource/1> but was:<null>");
}
@Test
public void redirectWithUrlTemplate() throws Exception {
redirectedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(getRedirectedUrlStubMvcResult("/orders/1/items/2"));
assertThatCode(() -> redirectedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(redirectedUrlStub("/orders/1/items/2")))
.doesNotThrowAnyException();
}
@Test
public void redirectWithMatchingPattern() throws Exception {
redirectedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1"));
assertThatCode(() -> redirectedUrlPattern("/resource/*").match(redirectedUrlStub("/resource/1")))
.doesNotThrowAnyException();
}
@Test
public void redirectWithNonMatchingPattern() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrlPattern("/resource/").match(getRedirectedUrlStubMvcResult("/resource/1")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> redirectedUrlPattern("/resource/").match(redirectedUrlStub("/resource/1")))
.withMessage("'/resource/' is not an Ant-style path pattern");
}
@Test
public void redirectWithNonMatchingPatternBecauseNotRedirect() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrlPattern("/resource/*").match(getForwardedUrlStubMvcResult("/resource/1")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> redirectedUrlPattern("/resource/*").match(forwardedUrlStub("/resource/1")))
.withMessage("Redirected URL 'null' does not match the expected URL pattern '/resource/*'");
}
@Test
public void forward() throws Exception {
forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1"));
assertThatCode(() -> forwardedUrl("/api/resource/1").match(forwardedUrlStub("/api/resource/1")))
.doesNotThrowAnyException();
}
@Test
public void forwardNonMatching() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("api/resource/2").match(getForwardedUrlStubMvcResult("api/resource/1")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> forwardedUrlPattern("api/resource/2").match(forwardedUrlStub("api/resource/1")))
.withMessage("'api/resource/2' is not an Ant-style path pattern");
}
@Test
public void forwardNonMatchingBecauseNotForward() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("api/resource/1").match(getRedirectedUrlStubMvcResult("api/resource/1")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> forwardedUrlPattern("/resource/*").match(redirectedUrlStub("/resource/1")))
.withMessage("Forwarded URL 'null' does not match the expected URL pattern '/resource/*'");
}
@Test
public void forwardWithQueryString() throws Exception {
forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
assertThatCode(() -> forwardedUrl("/api/resource/1?arg=value").match(forwardedUrlStub("/api/resource/1?arg=value")))
.doesNotThrowAnyException();
}
@Test
public void forwardWithUrlTemplate() throws Exception {
forwardedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(getForwardedUrlStubMvcResult("/orders/1/items/2"));
assertThatCode(() -> forwardedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(forwardedUrlStub("/orders/1/items/2")))
.doesNotThrowAnyException();
}
@Test
public void forwardWithMatchingPattern() throws Exception {
forwardedUrlPattern("/api/**/?").match(getForwardedUrlStubMvcResult("/api/resource/1"));
assertThatCode(() -> forwardedUrlPattern("/api/**/?").match(forwardedUrlStub("/api/resource/1")))
.doesNotThrowAnyException();
}
@Test
public void forwardWithNonMatchingPattern() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("/resource/").match(getForwardedUrlStubMvcResult("/resource/1")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> forwardedUrlPattern("/resource/").match(forwardedUrlStub("/resource/1")))
.withMessage("'/resource/' is not an Ant-style path pattern");
}
@Test
public void forwardWithNonMatchingPatternBecauseNotForward() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> forwardedUrlPattern("/resource/*").match(redirectedUrlStub("/resource/1")))
.withMessage("Forwarded URL 'null' does not match the expected URL pattern '/resource/*'");
}
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
private StubMvcResult redirectedUrlStub(String redirectUrl) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.sendRedirect(redirectUrl);
StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
return mvcResult;
return new StubMvcResult(null, null, null, null, null, null, response);
}
private StubMvcResult getForwardedUrlStubMvcResult(String forwardedUrl) {
private StubMvcResult forwardedUrlStub(String forwardedUrl) {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setForwardedUrl(forwardedUrl);
StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
return mvcResult;
return new StubMvcResult(null, null, null, null, null, null, response);
}
}

Loading…
Cancel
Save