|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.web.servlet.assertj; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +import jakarta.servlet.http.Cookie; |
| 25 | +import org.assertj.core.api.AbstractMapAssert; |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | + |
| 28 | +/** |
| 29 | + * AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to |
| 30 | + * {@link Cookie cookies}. |
| 31 | + * |
| 32 | + * @author Brian Clozel |
| 33 | + * @author Stephane Nicoll |
| 34 | + * @since 6.2 |
| 35 | + */ |
| 36 | +public class CookieMapAssert extends AbstractMapAssert<CookieMapAssert, Map<String, Cookie>, String, Cookie> { |
| 37 | + |
| 38 | + public CookieMapAssert(Cookie[] actual) { |
| 39 | + super(mapCookies(actual), CookieMapAssert.class); |
| 40 | + as("Cookies"); |
| 41 | + } |
| 42 | + |
| 43 | + private static Map<String, Cookie> mapCookies(Cookie[] cookies) { |
| 44 | + Map<String, Cookie> map = new LinkedHashMap<>(); |
| 45 | + for (Cookie cookie : cookies) { |
| 46 | + map.putIfAbsent(cookie.getName(), cookie); |
| 47 | + } |
| 48 | + return map; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Verify that the actual cookies contain a cookie with the given {@code name}. |
| 53 | + * @param name the name of an expected cookie |
| 54 | + * @see #containsKey |
| 55 | + */ |
| 56 | + public CookieMapAssert containsCookie(String name) { |
| 57 | + return containsKey(name); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Verify that the actual cookies contain the cookies with the given |
| 62 | + * {@code names}. |
| 63 | + * @param names the names of expected cookies |
| 64 | + * @see #containsKeys |
| 65 | + */ |
| 66 | + public CookieMapAssert containsCookies(String... names) { |
| 67 | + return containsKeys(names); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Verify that the actual cookies do not contain a cookie with the |
| 72 | + * given {@code name}. |
| 73 | + * @param name the name of a cookie that should not be present |
| 74 | + * @see #doesNotContainKey |
| 75 | + */ |
| 76 | + public CookieMapAssert doesNotContainCookie(String name) { |
| 77 | + return doesNotContainKey(name); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Verify that the actual cookies do not contain any of the cookies with |
| 82 | + * the given {@code names}. |
| 83 | + * @param names the names of cookies that should not be present |
| 84 | + * @see #doesNotContainKeys |
| 85 | + */ |
| 86 | + public CookieMapAssert doesNotContainCookies(String... names) { |
| 87 | + return doesNotContainKeys(names); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Verify that the actual cookies contain a cookie with the given |
| 92 | + * {@code name} that satisfy given {@code cookieRequirements}. |
| 93 | + * the specified names. |
| 94 | + * @param name the name of an expected cookie |
| 95 | + * @param cookieRequirements the requirements for the cookie |
| 96 | + */ |
| 97 | + public CookieMapAssert hasCookieSatisfying(String name, Consumer<Cookie> cookieRequirements) { |
| 98 | + return hasEntrySatisfying(name, cookieRequirements); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Verify that the actual cookies contain a cookie with the given |
| 103 | + * {@code name} whose {@linkplain Cookie#getValue() value} is equal to the |
| 104 | + * given one. |
| 105 | + * @param name the name of the cookie |
| 106 | + * @param expected the expected value of the cookie |
| 107 | + */ |
| 108 | + public CookieMapAssert hasValue(String name, String expected) { |
| 109 | + return hasCookieSatisfying(name, cookie -> |
| 110 | + Assertions.assertThat(cookie.getValue()).isEqualTo(expected)); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Verify that the actual cookies contain a cookie with the given |
| 115 | + * {@code name} whose {@linkplain Cookie#getMaxAge() max age} is equal to |
| 116 | + * the given one. |
| 117 | + * @param name the name of the cookie |
| 118 | + * @param expected the expected max age of the cookie |
| 119 | + */ |
| 120 | + public CookieMapAssert hasMaxAge(String name, Duration expected) { |
| 121 | + return hasCookieSatisfying(name, cookie -> |
| 122 | + Assertions.assertThat(Duration.ofSeconds(cookie.getMaxAge())).isEqualTo(expected)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Verify that the actual cookies contain a cookie with the given |
| 127 | + * {@code name} whose {@linkplain Cookie#getPath() path} is equal to |
| 128 | + * the given one. |
| 129 | + * @param name the name of the cookie |
| 130 | + * @param expected the expected path of the cookie |
| 131 | + */ |
| 132 | + public CookieMapAssert hasPath(String name, String expected) { |
| 133 | + return hasCookieSatisfying(name, cookie -> |
| 134 | + Assertions.assertThat(cookie.getPath()).isEqualTo(expected)); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Verify that the actual cookies contain a cookie with the given |
| 139 | + * {@code name} whose {@linkplain Cookie#getDomain() domain} is equal to |
| 140 | + * the given one. |
| 141 | + * @param name the name of the cookie |
| 142 | + * @param expected the expected path of the cookie |
| 143 | + */ |
| 144 | + public CookieMapAssert hasDomain(String name, String expected) { |
| 145 | + return hasCookieSatisfying(name, cookie -> |
| 146 | + Assertions.assertThat(cookie.getDomain()).isEqualTo(expected)); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Verify that the actual cookies contain a cookie with the given |
| 151 | + * {@code name} whose {@linkplain Cookie#getSecure() secure flag} is equal |
| 152 | + * to the given one. |
| 153 | + * @param name the name of the cookie |
| 154 | + * @param expected whether the cookie is secure |
| 155 | + */ |
| 156 | + public CookieMapAssert isSecure(String name, boolean expected) { |
| 157 | + return hasCookieSatisfying(name, cookie -> |
| 158 | + Assertions.assertThat(cookie.getSecure()).isEqualTo(expected)); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Verify that the actual cookies contain a cookie with the given |
| 163 | + * {@code name} whose {@linkplain Cookie#isHttpOnly() http only flag} is |
| 164 | + * equal to the given one. |
| 165 | + * @param name the name of the cookie |
| 166 | + * @param expected whether the cookie is http only |
| 167 | + */ |
| 168 | + public CookieMapAssert isHttpOnly(String name, boolean expected) { |
| 169 | + return hasCookieSatisfying(name, cookie -> |
| 170 | + Assertions.assertThat(cookie.isHttpOnly()).isEqualTo(expected)); |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments