Polish MockMvcResultMatchers[Tests]
This commit is contained in:
parent
8cf1933148
commit
509796a4b5
|
|
@ -21,7 +21,6 @@ import javax.xml.xpath.XPathExpressionException;
|
||||||
|
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
|
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
|
||||||
import org.springframework.test.web.servlet.ResultMatcher;
|
import org.springframework.test.web.servlet.ResultMatcher;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
|
|
@ -81,67 +80,51 @@ public abstract class MockMvcResultMatchers {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts the request was forwarded to the given URL.
|
* Asserts the request was forwarded to the given URL.
|
||||||
* <p>This methods accepts only exact matches.
|
* <p>This method accepts only exact matches.
|
||||||
* @param expectedUrl the exact URL expected
|
* @param expectedUrl the exact URL expected
|
||||||
*/
|
*/
|
||||||
public static ResultMatcher forwardedUrl(final String expectedUrl) {
|
public static ResultMatcher forwardedUrl(String expectedUrl) {
|
||||||
return new ResultMatcher() {
|
return result -> assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
|
||||||
@Override
|
|
||||||
public void match(MvcResult result) {
|
|
||||||
assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts the request was forwarded to the given URL.
|
* Asserts the request was forwarded to the given URL.
|
||||||
* <p>This methods accepts {@link org.springframework.util.AntPathMatcher}
|
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
|
||||||
* expressions.
|
* patterns.
|
||||||
* @param urlPattern an AntPath expression to match against
|
* @param urlPattern an AntPath pattern to match against
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
* @see org.springframework.util.AntPathMatcher
|
* @see org.springframework.util.AntPathMatcher
|
||||||
*/
|
*/
|
||||||
public static ResultMatcher forwardedUrlPattern(final String urlPattern) {
|
public static ResultMatcher forwardedUrlPattern(String urlPattern) {
|
||||||
return new ResultMatcher() {
|
return result -> {
|
||||||
@Override
|
assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern));
|
||||||
public void match(MvcResult result) {
|
assertTrue("Forwarded URL does not match the expected URL pattern",
|
||||||
assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern));
|
pathMatcher.match(urlPattern, result.getResponse().getForwardedUrl()));
|
||||||
assertTrue("Forwarded URL does not match the expected URL pattern",
|
|
||||||
pathMatcher.match(urlPattern, result.getResponse().getForwardedUrl()));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts the request was redirected to the given URL.
|
* Asserts the request was redirected to the given URL.
|
||||||
* <p>This methods accepts only exact matches.
|
* <p>This method accepts only exact matches.
|
||||||
* @param expectedUrl the exact URL expected
|
* @param expectedUrl the exact URL expected
|
||||||
*/
|
*/
|
||||||
public static ResultMatcher redirectedUrl(final String expectedUrl) {
|
public static ResultMatcher redirectedUrl(String expectedUrl) {
|
||||||
return new ResultMatcher() {
|
return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
|
||||||
@Override
|
|
||||||
public void match(MvcResult result) {
|
|
||||||
assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts the request was redirected to the given URL.
|
* Asserts the request was redirected to the given URL.
|
||||||
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
|
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
|
||||||
* expressions.
|
* patterns.
|
||||||
* @param expectedUrl an AntPath expression to match against
|
* @param urlPattern an AntPath pattern to match against
|
||||||
* @see org.springframework.util.AntPathMatcher
|
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
|
* @see org.springframework.util.AntPathMatcher
|
||||||
*/
|
*/
|
||||||
public static ResultMatcher redirectedUrlPattern(final String expectedUrl) {
|
public static ResultMatcher redirectedUrlPattern(String urlPattern) {
|
||||||
return new ResultMatcher() {
|
return result -> {
|
||||||
@Override
|
assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern));
|
||||||
public void match(MvcResult result) {
|
assertTrue("Redirected URL does not match the expected URL pattern",
|
||||||
assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl));
|
pathMatcher.match(urlPattern, result.getResponse().getRedirectedUrl()));
|
||||||
assertTrue("Redirected URL",
|
|
||||||
pathMatcher.match(expectedUrl, result.getResponse().getRedirectedUrl()));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.web.servlet.result;
|
package org.springframework.test.web.servlet.result;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -20,45 +21,49 @@ import org.junit.Test;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
import org.springframework.test.web.servlet.StubMvcResult;
|
import org.springframework.test.web.servlet.StubMvcResult;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Unit tests for {@link MockMvcResultMatchers}.
|
||||||
|
*
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
|
* @author Sam Brannen
|
||||||
*/
|
*/
|
||||||
public class MockMvcResultMatchersTests {
|
public class MockMvcResultMatchersTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRedirect() throws Exception {
|
public void redirect() throws Exception {
|
||||||
MockMvcResultMatchers.redirectedUrl("/resource/1")
|
redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1"));
|
||||||
.match(getRedirectedUrlStubMvcResult("/resource/1"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRedirectPattern() throws Exception {
|
public void redirectWithMatchingPattern() throws Exception {
|
||||||
MockMvcResultMatchers.redirectedUrlPattern("/resource/*")
|
redirectedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1"));
|
||||||
.match(getRedirectedUrlStubMvcResult("/resource/1"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test( expected = java.lang.AssertionError.class)
|
@Test(expected = AssertionError.class)
|
||||||
public void testFailRedirectPattern() throws Exception {
|
public void redirectWithNonMatchingPattern() throws Exception {
|
||||||
MockMvcResultMatchers.redirectedUrlPattern("/resource/")
|
redirectedUrlPattern("/resource/").match(getRedirectedUrlStubMvcResult("/resource/1"));
|
||||||
.match(getRedirectedUrlStubMvcResult("/resource/1"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForward() throws Exception {
|
public void forward() throws Exception {
|
||||||
MockMvcResultMatchers.forwardedUrl("/api/resource/1")
|
forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1"));
|
||||||
.match(getForwardedUrlStubMvcResult("/api/resource/1"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForwardEscapedChars() throws Exception {
|
public void forwardWithQueryString() throws Exception {
|
||||||
MockMvcResultMatchers.forwardedUrl("/api/resource/1?arg=value")
|
forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
|
||||||
.match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForwardPattern() throws Exception {
|
public void forwardWithMatchingPattern() throws Exception {
|
||||||
MockMvcResultMatchers.forwardedUrlPattern("/api/**/?")
|
forwardedUrlPattern("/api/**/?").match(getForwardedUrlStubMvcResult("/api/resource/1"));
|
||||||
.match(getForwardedUrlStubMvcResult("/api/resource/1"));
|
}
|
||||||
|
|
||||||
|
@Test(expected = AssertionError.class)
|
||||||
|
public void forwardWithNonMatchingPattern() throws Exception {
|
||||||
|
forwardedUrlPattern("/resource/").match(getForwardedUrlStubMvcResult("/resource/1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
|
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue