Polish MockMvcResultMatchers[Tests]

This commit is contained in:
Sam Brannen 2016-10-12 21:57:47 +02:00
parent 8cf1933148
commit 509796a4b5
2 changed files with 47 additions and 59 deletions

View File

@ -21,7 +21,6 @@ import javax.xml.xpath.XPathExpressionException;
import org.hamcrest.Matcher;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.util.AntPathMatcher;
@ -81,67 +80,51 @@ public abstract class MockMvcResultMatchers {
/**
* 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
*/
public static ResultMatcher forwardedUrl(final String expectedUrl) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
}
};
public static ResultMatcher forwardedUrl(String expectedUrl) {
return result -> assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
}
/**
* Asserts the request was forwarded to the given URL.
* <p>This methods accepts {@link org.springframework.util.AntPathMatcher}
* expressions.
* @param urlPattern an AntPath expression to match against
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
* patterns.
* @param urlPattern an AntPath pattern to match against
* @since 4.0
* @see org.springframework.util.AntPathMatcher
*/
public static ResultMatcher forwardedUrlPattern(final String urlPattern) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern));
public static ResultMatcher forwardedUrlPattern(String urlPattern) {
return result -> {
assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern));
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.
* <p>This methods accepts only exact matches.
* <p>This method accepts only exact matches.
* @param expectedUrl the exact URL expected
*/
public static ResultMatcher redirectedUrl(final String expectedUrl) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
}
};
public static ResultMatcher redirectedUrl(String expectedUrl) {
return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
}
/**
* Asserts the request was redirected to the given URL.
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
* expressions.
* @param expectedUrl an AntPath expression to match against
* @see org.springframework.util.AntPathMatcher
* patterns.
* @param urlPattern an AntPath pattern to match against
* @since 4.0
* @see org.springframework.util.AntPathMatcher
*/
public static ResultMatcher redirectedUrlPattern(final String expectedUrl) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl));
assertTrue("Redirected URL",
pathMatcher.match(expectedUrl, result.getResponse().getRedirectedUrl()));
}
public static ResultMatcher redirectedUrlPattern(String urlPattern) {
return result -> {
assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern));
assertTrue("Redirected URL does not match the expected URL pattern",
pathMatcher.match(urlPattern, result.getResponse().getRedirectedUrl()));
};
}

View File

@ -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");
* 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
* limitations under the License.
*/
package org.springframework.test.web.servlet.result;
import org.junit.Test;
@ -20,45 +21,49 @@ import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
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 Sam Brannen
*/
public class MockMvcResultMatchersTests {
@Test
public void testRedirect() throws Exception {
MockMvcResultMatchers.redirectedUrl("/resource/1")
.match(getRedirectedUrlStubMvcResult("/resource/1"));
public void redirect() throws Exception {
redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1"));
}
@Test
public void testRedirectPattern() throws Exception {
MockMvcResultMatchers.redirectedUrlPattern("/resource/*")
.match(getRedirectedUrlStubMvcResult("/resource/1"));
public void redirectWithMatchingPattern() throws Exception {
redirectedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1"));
}
@Test( expected = java.lang.AssertionError.class)
public void testFailRedirectPattern() throws Exception {
MockMvcResultMatchers.redirectedUrlPattern("/resource/")
.match(getRedirectedUrlStubMvcResult("/resource/1"));
@Test(expected = AssertionError.class)
public void redirectWithNonMatchingPattern() throws Exception {
redirectedUrlPattern("/resource/").match(getRedirectedUrlStubMvcResult("/resource/1"));
}
@Test
public void testForward() throws Exception {
MockMvcResultMatchers.forwardedUrl("/api/resource/1")
.match(getForwardedUrlStubMvcResult("/api/resource/1"));
public void forward() throws Exception {
forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1"));
}
@Test
public void testForwardEscapedChars() throws Exception {
MockMvcResultMatchers.forwardedUrl("/api/resource/1?arg=value")
.match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
public void forwardWithQueryString() throws Exception {
forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
}
@Test
public void testForwardPattern() throws Exception {
MockMvcResultMatchers.forwardedUrlPattern("/api/**/?")
.match(getForwardedUrlStubMvcResult("/api/resource/1"));
public void forwardWithMatchingPattern() throws Exception {
forwardedUrlPattern("/api/**/?").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 {