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