Merge pull request #379 from bclozel/SPR-10789
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 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.
|
||||||
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.test.web.servlet.result;
|
package org.springframework.test.web.servlet.result;
|
||||||
|
|
||||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
@@ -25,6 +23,9 @@ 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.MvcResult;
|
||||||
import org.springframework.test.web.servlet.ResultMatcher;
|
import org.springframework.test.web.servlet.ResultMatcher;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
|
import static org.springframework.test.util.AssertionErrors.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static, factory methods for {@link ResultMatcher}-based result actions.
|
* Static, factory methods for {@link ResultMatcher}-based result actions.
|
||||||
@@ -33,10 +34,13 @@ import org.springframework.test.web.servlet.ResultMatcher;
|
|||||||
* favorite. To navigate, open the Preferences and type "favorites".
|
* favorite. To navigate, open the Preferences and type "favorites".
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Brian Clozel
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public abstract class MockMvcResultMatchers {
|
public abstract class MockMvcResultMatchers {
|
||||||
|
|
||||||
|
private static final AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||||
|
|
||||||
|
|
||||||
private MockMvcResultMatchers() {
|
private MockMvcResultMatchers() {
|
||||||
}
|
}
|
||||||
@@ -78,9 +82,12 @@ public abstract class MockMvcResultMatchers {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts the request was forwarded to the given URL.
|
* Asserts the request was forwarded to the given URL.
|
||||||
|
* This methods accepts only exact matches.
|
||||||
|
* @param expectedUrl the exact URL expected
|
||||||
*/
|
*/
|
||||||
public static ResultMatcher forwardedUrl(final String expectedUrl) {
|
public static ResultMatcher forwardedUrl(final String expectedUrl) {
|
||||||
return new ResultMatcher() {
|
return new ResultMatcher() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void match(MvcResult result) {
|
public void match(MvcResult result) {
|
||||||
assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
|
assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
|
||||||
@@ -88,11 +95,34 @@ public abstract class MockMvcResultMatchers {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts the request was forwarded to the given URL.
|
||||||
|
* This methods accepts {@link org.springframework.util.AntPathMatcher} expressions.
|
||||||
|
*
|
||||||
|
* @param urlPattern an AntPath expression to match against
|
||||||
|
* @see org.springframework.util.AntPathMatcher
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public static ResultMatcher forwardedUrlPattern(final String urlPattern) {
|
||||||
|
return new ResultMatcher() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void match(MvcResult result) {
|
||||||
|
assertTrue("AntPath expression", 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.
|
* Asserts the request was redirected to the given URL.
|
||||||
|
* This methods accepts only exact matches.
|
||||||
|
* @param expectedUrl the exact URL expected
|
||||||
*/
|
*/
|
||||||
public static ResultMatcher redirectedUrl(final String expectedUrl) {
|
public static ResultMatcher redirectedUrl(final String expectedUrl) {
|
||||||
return new ResultMatcher() {
|
return new ResultMatcher() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void match(MvcResult result) {
|
public void match(MvcResult result) {
|
||||||
assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
|
assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
|
||||||
@@ -100,6 +130,26 @@ public abstract class MockMvcResultMatchers {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts the request was redirected to the given URL.
|
||||||
|
* This methods accepts {@link org.springframework.util.AntPathMatcher} expressions.
|
||||||
|
*
|
||||||
|
* @param expectedUrl an AntPath expression to match against
|
||||||
|
* @see org.springframework.util.AntPathMatcher
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access to response status assertions.
|
* Access to response status assertions.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2013 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* 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;
|
||||||
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
|
import org.springframework.test.web.servlet.StubMvcResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Brian Clozel
|
||||||
|
*/
|
||||||
|
public class MockMvcResultMatchersTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedirect() throws Exception {
|
||||||
|
MockMvcResultMatchers.redirectedUrl("/resource/1")
|
||||||
|
.match(getRedirectedUrlStubMvcResult("/resource/1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedirectPattern() throws Exception {
|
||||||
|
MockMvcResultMatchers.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
|
||||||
|
public void testForward() throws Exception {
|
||||||
|
MockMvcResultMatchers.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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testForwardPattern() throws Exception {
|
||||||
|
MockMvcResultMatchers.forwardedUrlPattern("/api/**/?")
|
||||||
|
.match(getForwardedUrlStubMvcResult("/api/resource/1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
response.sendRedirect(redirectUrl);
|
||||||
|
StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
|
||||||
|
return mvcResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
private StubMvcResult getForwardedUrlStubMvcResult(String forwardedUrl) {
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
response.setForwardedUrl(forwardedUrl);
|
||||||
|
StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
|
||||||
|
return mvcResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 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.
|
||||||
@@ -18,7 +18,9 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
|
|||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrlPattern;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
|
||||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
|
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -46,11 +48,20 @@ public class UrlAssertionTests {
|
|||||||
this.mockMvc.perform(get("/persons")).andExpect(redirectedUrl("/persons/1"));
|
this.mockMvc.perform(get("/persons")).andExpect(redirectedUrl("/persons/1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedirectPattern() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/persons")).andExpect(redirectedUrlPattern("/persons/*"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForward() throws Exception {
|
public void testForward() throws Exception {
|
||||||
this.mockMvc.perform(get("/")).andExpect(forwardedUrl("/home"));
|
this.mockMvc.perform(get("/")).andExpect(forwardedUrl("/home"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testForwardPattern() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/")).andExpect(forwardedUrlPattern("/ho?e"));
|
||||||
|
}
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
private static class SimpleController {
|
private static class SimpleController {
|
||||||
|
|||||||
Reference in New Issue
Block a user