|
| 1 | +/* |
| 2 | + * Copyright 2002-present 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.reactive.server.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 org.assertj.core.api.AbstractMapAssert; |
| 25 | +import org.assertj.core.api.Assertions; |
| 26 | + |
| 27 | +import org.springframework.http.ResponseCookie; |
| 28 | + |
| 29 | +/** |
| 30 | + * AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied |
| 31 | + * to {@link ResponseCookie cookies}. |
| 32 | + * |
| 33 | + * @author Rossen Stoyanchev |
| 34 | + * @since 7.0 |
| 35 | + */ |
| 36 | +public class ResponseCookieMapAssert |
| 37 | + extends AbstractMapAssert<ResponseCookieMapAssert, Map<String, ResponseCookie>, String, ResponseCookie> { |
| 38 | + |
| 39 | + |
| 40 | + public ResponseCookieMapAssert(ResponseCookie[] actual) { |
| 41 | + super(toMap(actual), ResponseCookieMapAssert.class); |
| 42 | + as("Cookies"); |
| 43 | + } |
| 44 | + |
| 45 | + private static Map<String, ResponseCookie> toMap(ResponseCookie[] cookies) { |
| 46 | + Map<String, ResponseCookie> map = new LinkedHashMap<>(); |
| 47 | + for (ResponseCookie cookie : cookies) { |
| 48 | + map.putIfAbsent(cookie.getName(), cookie); |
| 49 | + } |
| 50 | + return map; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * Verify that the actual cookies contain a cookie with the given {@code name}. |
| 56 | + * @param name the name of an expected cookie |
| 57 | + * @see #containsKey |
| 58 | + */ |
| 59 | + public ResponseCookieMapAssert containsCookie(String name) { |
| 60 | + return containsKey(name); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Verify that the actual cookies contain cookies with the given {@code names}. |
| 65 | + * @param names the names of expected cookies |
| 66 | + * @see #containsKeys |
| 67 | + */ |
| 68 | + public ResponseCookieMapAssert containsCookies(String... names) { |
| 69 | + return containsKeys(names); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Verify that the actual cookies do not contain a cookie with the given |
| 74 | + * {@code name}. |
| 75 | + * @param name the name of a cookie that should not be present |
| 76 | + * @see #doesNotContainKey |
| 77 | + */ |
| 78 | + public ResponseCookieMapAssert doesNotContainCookie(String name) { |
| 79 | + return doesNotContainKey(name); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Verify that the actual cookies do not contain any cookies with the given |
| 84 | + * {@code names}. |
| 85 | + * @param names the names of cookies that should not be present |
| 86 | + * @see #doesNotContainKeys |
| 87 | + */ |
| 88 | + public ResponseCookieMapAssert doesNotContainCookies(String... names) { |
| 89 | + return doesNotContainKeys(names); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 94 | + * that satisfies the given {@code cookieRequirements}. |
| 95 | + * @param name the name of an expected cookie |
| 96 | + * @param cookieRequirements the requirements for the cookie |
| 97 | + */ |
| 98 | + public ResponseCookieMapAssert hasCookieSatisfying(String name, Consumer<ResponseCookie> cookieRequirements) { |
| 99 | + return hasEntrySatisfying(name, cookieRequirements); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 104 | + * whose {@linkplain ResponseCookie#getValue() value} is equal to the given one. |
| 105 | + * @param name the name of the cookie |
| 106 | + * @param expected the expected value of the cookie |
| 107 | + */ |
| 108 | + public ResponseCookieMapAssert hasValue(String name, String expected) { |
| 109 | + return hasCookieSatisfying(name, cookie -> Assertions.assertThat(cookie.getValue()).isEqualTo(expected)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 114 | + * whose {@linkplain ResponseCookie#getMaxAge() max age} is equal to the given one. |
| 115 | + * @param name the name of the cookie |
| 116 | + * @param expected the expected max age of the cookie |
| 117 | + */ |
| 118 | + public ResponseCookieMapAssert hasMaxAge(String name, Duration expected) { |
| 119 | + return hasCookieSatisfying(name, cookie -> Assertions.assertThat(cookie.getMaxAge()).isEqualTo(expected)); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 124 | + * whose {@linkplain ResponseCookie#getPath() path} is equal to the given one. |
| 125 | + * @param name the name of the cookie |
| 126 | + * @param expected the expected path of the cookie |
| 127 | + */ |
| 128 | + public ResponseCookieMapAssert hasPath(String name, String expected) { |
| 129 | + return hasCookieSatisfying(name, cookie -> Assertions.assertThat(cookie.getPath()).isEqualTo(expected)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 134 | + * whose {@linkplain ResponseCookie#getDomain() domain} is equal to the given one. |
| 135 | + * @param name the name of the cookie |
| 136 | + * @param expected the expected domain of the cookie |
| 137 | + */ |
| 138 | + public ResponseCookieMapAssert hasDomain(String name, String expected) { |
| 139 | + return hasCookieSatisfying(name, cookie -> Assertions.assertThat(cookie.getDomain()).isEqualTo(expected)); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 144 | + * whose {@linkplain ResponseCookie#isSecure() secure flag} is equal to the give one. |
| 145 | + * @param name the name of the cookie |
| 146 | + * @param expected whether the cookie is secure |
| 147 | + */ |
| 148 | + public ResponseCookieMapAssert isSecure(String name, boolean expected) { |
| 149 | + return hasCookieSatisfying(name, cookie -> Assertions.assertThat(cookie.isSecure()).isEqualTo(expected)); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Verify that the actual cookies contain a cookie with the given {@code name} |
| 154 | + * whose {@linkplain ResponseCookie#isHttpOnly() http only flag} is equal to the given |
| 155 | + * one. |
| 156 | + * @param name the name of the cookie |
| 157 | + * @param expected whether the cookie is http only |
| 158 | + */ |
| 159 | + public ResponseCookieMapAssert isHttpOnly(String name, boolean expected) { |
| 160 | + return hasCookieSatisfying(name, cookie -> Assertions.assertThat(cookie.isHttpOnly()).isEqualTo(expected)); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments