|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 CodeLibs Project and the Others. |
| 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 | + * http://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, |
| 13 | + * either express or implied. See the License for the specific language |
| 14 | + * governing permissions and limitations under the License. |
| 15 | + */ |
| 16 | +package org.codelibs.fess.crawler.client.http.config; |
| 17 | + |
| 18 | +import java.util.Date; |
| 19 | + |
| 20 | +import org.dbflute.utflute.core.PlainTestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test class for CookieConfig. |
| 24 | + * Tests all getters, setters, and toString functionality. |
| 25 | + */ |
| 26 | +public class CookieConfigTest extends PlainTestCase { |
| 27 | + |
| 28 | + /** |
| 29 | + * Test default values on new instance |
| 30 | + */ |
| 31 | + public void test_defaultValues() { |
| 32 | + CookieConfig config = new CookieConfig(); |
| 33 | + |
| 34 | + assertNull(config.getName()); |
| 35 | + assertNull(config.getValue()); |
| 36 | + assertNull(config.getDomain()); |
| 37 | + assertNull(config.getPath()); |
| 38 | + assertNull(config.getExpiryDate()); |
| 39 | + assertFalse(config.isSecure()); |
| 40 | + assertFalse(config.isHttpOnly()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test name getter and setter |
| 45 | + */ |
| 46 | + public void test_name() { |
| 47 | + CookieConfig config = new CookieConfig(); |
| 48 | + |
| 49 | + config.setName("sessionId"); |
| 50 | + assertEquals("sessionId", config.getName()); |
| 51 | + |
| 52 | + config.setName("JSESSIONID"); |
| 53 | + assertEquals("JSESSIONID", config.getName()); |
| 54 | + |
| 55 | + config.setName(null); |
| 56 | + assertNull(config.getName()); |
| 57 | + |
| 58 | + config.setName(""); |
| 59 | + assertEquals("", config.getName()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test value getter and setter |
| 64 | + */ |
| 65 | + public void test_value() { |
| 66 | + CookieConfig config = new CookieConfig(); |
| 67 | + |
| 68 | + config.setValue("abc123"); |
| 69 | + assertEquals("abc123", config.getValue()); |
| 70 | + |
| 71 | + config.setValue("value with spaces and special chars !@#$%"); |
| 72 | + assertEquals("value with spaces and special chars !@#$%", config.getValue()); |
| 73 | + |
| 74 | + config.setValue(null); |
| 75 | + assertNull(config.getValue()); |
| 76 | + |
| 77 | + config.setValue(""); |
| 78 | + assertEquals("", config.getValue()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test domain getter and setter |
| 83 | + */ |
| 84 | + public void test_domain() { |
| 85 | + CookieConfig config = new CookieConfig(); |
| 86 | + |
| 87 | + config.setDomain("example.com"); |
| 88 | + assertEquals("example.com", config.getDomain()); |
| 89 | + |
| 90 | + config.setDomain(".example.com"); |
| 91 | + assertEquals(".example.com", config.getDomain()); |
| 92 | + |
| 93 | + config.setDomain("subdomain.example.com"); |
| 94 | + assertEquals("subdomain.example.com", config.getDomain()); |
| 95 | + |
| 96 | + config.setDomain(null); |
| 97 | + assertNull(config.getDomain()); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test path getter and setter |
| 102 | + */ |
| 103 | + public void test_path() { |
| 104 | + CookieConfig config = new CookieConfig(); |
| 105 | + |
| 106 | + config.setPath("/"); |
| 107 | + assertEquals("/", config.getPath()); |
| 108 | + |
| 109 | + config.setPath("/app/context"); |
| 110 | + assertEquals("/app/context", config.getPath()); |
| 111 | + |
| 112 | + config.setPath("/path/with/many/segments"); |
| 113 | + assertEquals("/path/with/many/segments", config.getPath()); |
| 114 | + |
| 115 | + config.setPath(null); |
| 116 | + assertNull(config.getPath()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test expiryDate getter and setter |
| 121 | + */ |
| 122 | + public void test_expiryDate() { |
| 123 | + CookieConfig config = new CookieConfig(); |
| 124 | + |
| 125 | + Date now = new Date(); |
| 126 | + config.setExpiryDate(now); |
| 127 | + assertEquals(now, config.getExpiryDate()); |
| 128 | + |
| 129 | + Date future = new Date(System.currentTimeMillis() + 86400000L); // 1 day in future |
| 130 | + config.setExpiryDate(future); |
| 131 | + assertEquals(future, config.getExpiryDate()); |
| 132 | + |
| 133 | + Date past = new Date(0L); // epoch |
| 134 | + config.setExpiryDate(past); |
| 135 | + assertEquals(past, config.getExpiryDate()); |
| 136 | + |
| 137 | + config.setExpiryDate(null); |
| 138 | + assertNull(config.getExpiryDate()); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test secure flag getter and setter |
| 143 | + */ |
| 144 | + public void test_secure() { |
| 145 | + CookieConfig config = new CookieConfig(); |
| 146 | + |
| 147 | + assertFalse(config.isSecure()); |
| 148 | + |
| 149 | + config.setSecure(true); |
| 150 | + assertTrue(config.isSecure()); |
| 151 | + |
| 152 | + config.setSecure(false); |
| 153 | + assertFalse(config.isSecure()); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Test httpOnly flag getter and setter |
| 158 | + */ |
| 159 | + public void test_httpOnly() { |
| 160 | + CookieConfig config = new CookieConfig(); |
| 161 | + |
| 162 | + assertFalse(config.isHttpOnly()); |
| 163 | + |
| 164 | + config.setHttpOnly(true); |
| 165 | + assertTrue(config.isHttpOnly()); |
| 166 | + |
| 167 | + config.setHttpOnly(false); |
| 168 | + assertFalse(config.isHttpOnly()); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Test full configuration with all properties set |
| 173 | + */ |
| 174 | + public void test_fullConfiguration() { |
| 175 | + CookieConfig config = new CookieConfig(); |
| 176 | + Date expiry = new Date(System.currentTimeMillis() + 3600000L); |
| 177 | + |
| 178 | + config.setName("auth_token"); |
| 179 | + config.setValue("xyz789"); |
| 180 | + config.setDomain("secure.example.com"); |
| 181 | + config.setPath("/secure"); |
| 182 | + config.setExpiryDate(expiry); |
| 183 | + config.setSecure(true); |
| 184 | + config.setHttpOnly(true); |
| 185 | + |
| 186 | + assertEquals("auth_token", config.getName()); |
| 187 | + assertEquals("xyz789", config.getValue()); |
| 188 | + assertEquals("secure.example.com", config.getDomain()); |
| 189 | + assertEquals("/secure", config.getPath()); |
| 190 | + assertEquals(expiry, config.getExpiryDate()); |
| 191 | + assertTrue(config.isSecure()); |
| 192 | + assertTrue(config.isHttpOnly()); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Test toString method |
| 197 | + */ |
| 198 | + public void test_toString() { |
| 199 | + CookieConfig config = new CookieConfig(); |
| 200 | + config.setName("testCookie"); |
| 201 | + config.setDomain("example.com"); |
| 202 | + config.setPath("/"); |
| 203 | + config.setSecure(true); |
| 204 | + config.setHttpOnly(false); |
| 205 | + |
| 206 | + String result = config.toString(); |
| 207 | + |
| 208 | + assertNotNull(result); |
| 209 | + assertTrue(result.contains("CookieConfig")); |
| 210 | + assertTrue(result.contains("name=testCookie")); |
| 211 | + assertTrue(result.contains("domain=example.com")); |
| 212 | + assertTrue(result.contains("path=/")); |
| 213 | + assertTrue(result.contains("secure=true")); |
| 214 | + assertTrue(result.contains("httpOnly=false")); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Test toString with null values |
| 219 | + */ |
| 220 | + public void test_toString_withNullValues() { |
| 221 | + CookieConfig config = new CookieConfig(); |
| 222 | + |
| 223 | + String result = config.toString(); |
| 224 | + |
| 225 | + assertNotNull(result); |
| 226 | + assertTrue(result.contains("CookieConfig")); |
| 227 | + assertTrue(result.contains("name=null")); |
| 228 | + assertTrue(result.contains("domain=null")); |
| 229 | + assertTrue(result.contains("path=null")); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Test toString does not include value (security) |
| 234 | + */ |
| 235 | + public void test_toString_excludesValue() { |
| 236 | + CookieConfig config = new CookieConfig(); |
| 237 | + config.setName("session"); |
| 238 | + config.setValue("sensitive_value_123"); |
| 239 | + |
| 240 | + String result = config.toString(); |
| 241 | + |
| 242 | + // Value should not be included in toString for security |
| 243 | + assertFalse(result.contains("sensitive_value_123")); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Test special characters in cookie name |
| 248 | + */ |
| 249 | + public void test_specialCharactersInName() { |
| 250 | + CookieConfig config = new CookieConfig(); |
| 251 | + |
| 252 | + config.setName("cookie_name_with-dashes"); |
| 253 | + assertEquals("cookie_name_with-dashes", config.getName()); |
| 254 | + |
| 255 | + config.setName("cookie.name.with.dots"); |
| 256 | + assertEquals("cookie.name.with.dots", config.getName()); |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Test Unicode characters in value |
| 261 | + */ |
| 262 | + public void test_unicodeValue() { |
| 263 | + CookieConfig config = new CookieConfig(); |
| 264 | + |
| 265 | + config.setValue("value_with_日本語_characters"); |
| 266 | + assertEquals("value_with_日本語_characters", config.getValue()); |
| 267 | + |
| 268 | + config.setValue("émojis_🍪_cookie"); |
| 269 | + assertEquals("émojis_🍪_cookie", config.getValue()); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Test multiple instances are independent |
| 274 | + */ |
| 275 | + public void test_multipleInstances() { |
| 276 | + CookieConfig config1 = new CookieConfig(); |
| 277 | + CookieConfig config2 = new CookieConfig(); |
| 278 | + |
| 279 | + config1.setName("cookie1"); |
| 280 | + config1.setValue("value1"); |
| 281 | + config1.setSecure(true); |
| 282 | + |
| 283 | + config2.setName("cookie2"); |
| 284 | + config2.setValue("value2"); |
| 285 | + config2.setSecure(false); |
| 286 | + |
| 287 | + assertEquals("cookie1", config1.getName()); |
| 288 | + assertEquals("value1", config1.getValue()); |
| 289 | + assertTrue(config1.isSecure()); |
| 290 | + |
| 291 | + assertEquals("cookie2", config2.getName()); |
| 292 | + assertEquals("value2", config2.getValue()); |
| 293 | + assertFalse(config2.isSecure()); |
| 294 | + } |
| 295 | +} |
0 commit comments