001/*
002 * Copyright 2002-2017 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 javax.servlet.http.Cookie;
020
021import org.hamcrest.Matcher;
022
023import org.springframework.test.web.servlet.MvcResult;
024import org.springframework.test.web.servlet.ResultMatcher;
025
026import static org.hamcrest.MatcherAssert.*;
027import static org.springframework.test.util.AssertionErrors.*;
028
029/**
030 * Factory for response cookie assertions.
031 *
032 * <p>An instance of this class is typically accessed via
033 * {@link MockMvcResultMatchers#cookie}.
034 *
035 * @author Rossen Stoyanchev
036 * @author Thomas Bruyelle
037 * @since 3.2
038 */
039public class CookieResultMatchers {
040
041        /**
042         * Protected constructor.
043         * Use {@link MockMvcResultMatchers#cookie()}.
044         */
045        protected CookieResultMatchers() {
046        }
047
048
049        /**
050         * Assert a cookie value with the given Hamcrest {@link Matcher}.
051         */
052        public ResultMatcher value(final String name, final Matcher<? super String> matcher) {
053                return new ResultMatcher() {
054                        @Override
055                        public void match(MvcResult result) {
056                                Cookie cookie = getCookie(result, name);
057                                assertThat("Response cookie '" + name + "'", cookie.getValue(), matcher);
058                        }
059                };
060        }
061
062        /**
063         * Assert a cookie value.
064         */
065        public ResultMatcher value(final String name, final String expectedValue) {
066                return new ResultMatcher() {
067                        @Override
068                        public void match(MvcResult result) {
069                                Cookie cookie = getCookie(result, name);
070                                assertEquals("Response cookie", expectedValue, cookie.getValue());
071                        }
072                };
073        }
074
075        /**
076         * Assert a cookie exists. The existence check is irrespective of whether
077         * max age is 0 (i.e. expired).
078         */
079        public ResultMatcher exists(final String name) {
080                return new ResultMatcher() {
081                        @Override
082                        public void match(MvcResult result) {
083                                getCookie(result, name);
084                        }
085                };
086        }
087
088        /**
089         * Assert a cookie does not exist. Note that the existence check is
090         * irrespective of whether max age is 0, i.e. expired.
091         */
092        public ResultMatcher doesNotExist(final String name) {
093                return new ResultMatcher() {
094                        @Override
095                        public void match(MvcResult result) {
096                                Cookie cookie = result.getResponse().getCookie(name);
097                                assertTrue("Unexpected cookie with name '" + name + "'", cookie == null);
098                        }
099                };
100        }
101
102        /**
103         * Assert a cookie's maxAge with a Hamcrest {@link Matcher}.
104         */
105        public ResultMatcher maxAge(final String name, final Matcher<? super Integer> matcher) {
106                return new ResultMatcher() {
107                        @Override
108                        public void match(MvcResult result) {
109                                Cookie cookie = getCookie(result, name);
110                                assertThat("Response cookie '" + name + "' maxAge", cookie.getMaxAge(), matcher);
111                        }
112                };
113        }
114
115        /**
116         * Assert a cookie's maxAge value.
117         */
118        public ResultMatcher maxAge(final String name, final int maxAge) {
119                return new ResultMatcher() {
120                        @Override
121                        public void match(MvcResult result) {
122                                Cookie cookie = getCookie(result, name);
123                                assertEquals("Response cookie '" + name + "' maxAge", maxAge, cookie.getMaxAge());
124                        }
125                };
126        }
127
128        /**
129         * Assert a cookie path with a Hamcrest {@link Matcher}.
130         */
131        public ResultMatcher path(final String name, final Matcher<? super String> matcher) {
132                return new ResultMatcher() {
133                        @Override
134                        public void match(MvcResult result) throws Exception {
135                                Cookie cookie = getCookie(result, name);
136                                assertThat("Response cookie '" + name + "' path", cookie.getPath(), matcher);
137                        }
138                };
139        }
140
141        public ResultMatcher path(final String name, final String path) {
142                return new ResultMatcher() {
143                        @Override
144                        public void match(MvcResult result) throws Exception {
145                                Cookie cookie = getCookie(result, name);
146                                assertEquals("Response cookie '" + name + "' path", path, cookie.getPath());
147                        }
148                };
149        }
150
151        /**
152         * Assert a cookie's domain with a Hamcrest {@link Matcher}.
153         */
154        public ResultMatcher domain(final String name, final Matcher<? super String> matcher) {
155                return new ResultMatcher() {
156                        @Override
157                        public void match(MvcResult result) throws Exception {
158                                Cookie cookie = getCookie(result, name);
159                                assertThat("Response cookie '" + name + "' domain", cookie.getDomain(), matcher);
160                        }
161                };
162        }
163
164        /**
165         * Assert a cookie's domain value.
166         */
167        public ResultMatcher domain(final String name, final String domain) {
168                return new ResultMatcher() {
169                        @Override
170                        public void match(MvcResult result) throws Exception {
171                                Cookie cookie = getCookie(result, name);
172                                assertEquals("Response cookie '" + name + "' domain", domain, cookie.getDomain());
173                        }
174                };
175        }
176
177        /**
178         * Assert a cookie's comment with a Hamcrest {@link Matcher}.
179         */
180        public ResultMatcher comment(final String name, final Matcher<? super String> matcher) {
181                return new ResultMatcher() {
182                        @Override
183                        public void match(MvcResult result) throws Exception {
184                                Cookie cookie = getCookie(result, name);
185                                assertThat("Response cookie '" + name + "' comment", cookie.getComment(), matcher);
186                        }
187                };
188        }
189
190        /**
191         * Assert a cookie's comment value.
192         */
193        public ResultMatcher comment(final String name, final String comment) {
194                return new ResultMatcher() {
195                        @Override
196                        public void match(MvcResult result) throws Exception {
197                                Cookie cookie = getCookie(result, name);
198                                assertEquals("Response cookie '" + name + "' comment", comment, cookie.getComment());
199                        }
200                };
201        }
202
203        /**
204         * Assert a cookie's version with a Hamcrest {@link Matcher}
205         */
206        public ResultMatcher version(final String name, final Matcher<? super Integer> matcher) {
207                return new ResultMatcher() {
208                        @Override
209                        public void match(MvcResult result) throws Exception {
210                                Cookie cookie = getCookie(result, name);
211                                assertThat("Response cookie '" + name + "' version", cookie.getVersion(), matcher);
212                        }
213                };
214        }
215
216        /**
217         * Assert a cookie's version value.
218         */
219        public ResultMatcher version(final String name, final int version) {
220                return new ResultMatcher() {
221                        @Override
222                        public void match(MvcResult result) throws Exception {
223                                Cookie cookie = getCookie(result, name);
224                                assertEquals("Response cookie '" + name + "' version", version, cookie.getVersion());
225                        }
226                };
227        }
228
229        /**
230         * Assert whether the cookie must be sent over a secure protocol or not.
231         */
232        public ResultMatcher secure(final String name, final boolean secure) {
233                return new ResultMatcher() {
234                        @Override
235                        public void match(MvcResult result) throws Exception {
236                                Cookie cookie = getCookie(result, name);
237                                assertEquals("Response cookie '" + name + "' secure", secure, cookie.getSecure());
238                        }
239                };
240        }
241
242        /**
243         * Assert whether the cookie must be HTTP only.
244         * @since 4.3.9
245         */
246        public ResultMatcher httpOnly(final String name, final boolean httpOnly) {
247                return new ResultMatcher() {
248                        @Override
249                        public void match(MvcResult result) throws Exception {
250                                Cookie cookie = getCookie(result, name);
251                                assertEquals("Response cookie '" + name + "' httpOnly", httpOnly, cookie.isHttpOnly());
252                        }
253                };
254        }
255
256
257        private static Cookie getCookie(MvcResult result, String name) {
258                Cookie cookie = result.getResponse().getCookie(name);
259                assertTrue("No cookie with name '" + name + "'", cookie != null);
260                return cookie;
261        }
262
263}