001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet.result;
018
019import java.util.Map;
020import javax.xml.xpath.XPathExpressionException;
021
022import org.hamcrest.Matcher;
023
024import org.springframework.test.web.servlet.MvcResult;
025import org.springframework.test.web.servlet.ResultMatcher;
026import org.springframework.util.AntPathMatcher;
027
028import static org.springframework.test.util.AssertionErrors.*;
029
030/**
031 * Static factory methods for {@link ResultMatcher}-based result actions.
032 *
033 * <h3>Eclipse Users</h3>
034 * <p>Consider adding this class as a Java editor favorite. To navigate to
035 * this setting, open the Preferences and type "favorites".
036 *
037 * @author Rossen Stoyanchev
038 * @author Brian Clozel
039 * @author Sam Brannen
040 * @since 3.2
041 */
042public abstract class MockMvcResultMatchers {
043
044        private static final AntPathMatcher pathMatcher = new AntPathMatcher();
045
046
047        /**
048         * Access to request-related assertions.
049         */
050        public static RequestResultMatchers request() {
051                return new RequestResultMatchers();
052        }
053
054        /**
055         * Access to assertions for the handler that handled the request.
056         */
057        public static HandlerResultMatchers handler() {
058                return new HandlerResultMatchers();
059        }
060
061        /**
062         * Access to model-related assertions.
063         */
064        public static ModelResultMatchers model() {
065                return new ModelResultMatchers();
066        }
067
068        /**
069         * Access to assertions on the selected view.
070         */
071        public static ViewResultMatchers view() {
072                return new ViewResultMatchers();
073        }
074
075        /**
076         * Access to flash attribute assertions.
077         */
078        public static FlashAttributeResultMatchers flash() {
079                return new FlashAttributeResultMatchers();
080        }
081
082        /**
083         * Asserts the request was forwarded to the given URL.
084         * <p>This methods accepts only exact matches.
085         * @param expectedUrl the exact URL expected
086         */
087        public static ResultMatcher forwardedUrl(final String expectedUrl) {
088                return new ResultMatcher() {
089                        @Override
090                        public void match(MvcResult result) {
091                                assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
092                        }
093                };
094        }
095
096        /**
097         * Asserts the request was forwarded to the given URL.
098         * <p>This methods accepts {@link org.springframework.util.AntPathMatcher}
099         * expressions.
100         * @param urlPattern an AntPath expression to match against
101         * @since 4.0
102         * @see org.springframework.util.AntPathMatcher
103         */
104        public static ResultMatcher forwardedUrlPattern(final String urlPattern) {
105                return new ResultMatcher() {
106                        @Override
107                        public void match(MvcResult result) {
108                                assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern));
109                                assertTrue("Forwarded URL does not match the expected URL pattern",
110                                                pathMatcher.match(urlPattern, result.getResponse().getForwardedUrl()));
111                        }
112                };
113        }
114
115        /**
116         * Asserts the request was redirected to the given URL.
117         * <p>This methods accepts only exact matches.
118         * @param expectedUrl the exact URL expected
119         */
120        public static ResultMatcher redirectedUrl(final String expectedUrl) {
121                return new ResultMatcher() {
122                        @Override
123                        public void match(MvcResult result) {
124                                assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
125                        }
126                };
127        }
128
129        /**
130         * Asserts the request was redirected to the given URL.
131         * <p>This method accepts {@link org.springframework.util.AntPathMatcher}
132         * expressions.
133         * @param expectedUrl an AntPath expression to match against
134         * @see org.springframework.util.AntPathMatcher
135         * @since 4.0
136         */
137        public static ResultMatcher redirectedUrlPattern(final String expectedUrl) {
138                return new ResultMatcher() {
139                        @Override
140                        public void match(MvcResult result) {
141                                assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl));
142                                assertTrue("Redirected URL",
143                                                pathMatcher.match(expectedUrl, result.getResponse().getRedirectedUrl()));
144                        }
145                };
146        }
147
148        /**
149         * Access to response status assertions.
150         */
151        public static StatusResultMatchers status() {
152                return new StatusResultMatchers();
153        }
154
155        /**
156         * Access to response header assertions.
157         */
158        public static HeaderResultMatchers header() {
159                return new HeaderResultMatchers();
160        }
161
162        /**
163         * Access to response body assertions.
164         */
165        public static ContentResultMatchers content() {
166                return new ContentResultMatchers();
167        }
168
169        /**
170         * Access to response body assertions using a
171         * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
172         * to inspect a specific subset of the body.
173         * <p>The JSON path expression can be a parameterized string using
174         * formatting specifiers as defined in
175         * {@link String#format(String, Object...)}.
176         * @param expression the JSON path expression, optionally parameterized with arguments
177         * @param args arguments to parameterize the JSON path expression with
178         */
179        public static JsonPathResultMatchers jsonPath(String expression, Object... args) {
180                return new JsonPathResultMatchers(expression, args);
181        }
182
183        /**
184         * Access to response body assertions using a
185         * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
186         * to inspect a specific subset of the body and a Hamcrest matcher for
187         * asserting the value found at the JSON path.
188         * @param expression the JSON path expression
189         * @param matcher a matcher for the value expected at the JSON path
190         */
191        public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
192                return new JsonPathResultMatchers(expression).value(matcher);
193        }
194
195        /**
196         * Access to response body assertions using an XPath expression to
197         * inspect a specific subset of the body.
198         * <p>The XPath expression can be a parameterized string using formatting
199         * specifiers as defined in {@link String#format(String, Object...)}.
200         * @param expression the XPath expression, optionally parameterized with arguments
201         * @param args arguments to parameterize the XPath expression with
202         */
203        public static XpathResultMatchers xpath(String expression, Object... args) throws XPathExpressionException {
204                return new XpathResultMatchers(expression, null, args);
205        }
206
207        /**
208         * Access to response body assertions using an XPath expression to
209         * inspect a specific subset of the body.
210         * <p>The XPath expression can be a parameterized string using formatting
211         * specifiers as defined in {@link String#format(String, Object...)}.
212         * @param expression the XPath expression, optionally parameterized with arguments
213         * @param namespaces namespaces referenced in the XPath expression
214         * @param args arguments to parameterize the XPath expression with
215         */
216        public static XpathResultMatchers xpath(String expression, Map<String, String> namespaces, Object... args)
217                        throws XPathExpressionException {
218
219                return new XpathResultMatchers(expression, namespaces, args);
220        }
221
222        /**
223         * Access to response cookie assertions.
224         */
225        public static CookieResultMatchers cookie() {
226                return new CookieResultMatchers();
227        }
228
229}