Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/CookieTest.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static org.assertj.core.api.Assertions.assertThat;
21
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22
23
import java.io.ByteArrayInputStream;
24
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
26
import java.io.ObjectInputStream;
27
import java.io.ObjectOutputStream;
28
import java.util.Date;
29
import org.junit.jupiter.api.Tag;
30
import org.junit.jupiter.api.Test;
31
32
@Tag("UnitTests")
33
class CookieTest {
34
35
@Test
36
void testCanCreateAWellFormedCookie() {
37
new Cookie("Fish", "cod", "", "", null, false);
38
}
39
40
@Test
41
void testShouldThrowAnExceptionWhenSemiColonExistsInTheCookieAttribute() {
42
Cookie cookie = new Cookie("hi;hi", "value", null, null, null, false);
43
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(cookie::validate);
44
}
45
46
@Test
47
void testShouldThrowAnExceptionTheNameIsNull() {
48
Cookie cookie = new Cookie(null, "value", null, null, null, false);
49
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(cookie::validate);
50
}
51
52
@Test
53
void testCookiesShouldAllowSecureToBeSet() {
54
Cookie cookie = new Cookie("name", "value", "", "/", new Date(), true);
55
assertThat(cookie.isSecure()).isTrue();
56
}
57
58
@Test
59
void testSecureDefaultsToFalse() {
60
Cookie cookie = new Cookie("name", "value");
61
assertThat(cookie.isSecure()).isFalse();
62
}
63
64
@Test
65
void testCookiesShouldAllowHttpOnlyToBeSet() {
66
Cookie cookie = new Cookie("name", "value", "", "/", new Date(), false, true);
67
assertThat(cookie.isHttpOnly()).isTrue();
68
}
69
70
@Test
71
void testHttpOnlyDefaultsToFalse() {
72
Cookie cookie = new Cookie("name", "value");
73
assertThat(cookie.isHttpOnly()).isFalse();
74
}
75
76
@Test
77
void testCookiesShouldAllowSameSiteToBeSet() {
78
Cookie cookie = new Cookie("name", "value", "", "/", new Date(), false, true, "Lax");
79
assertThat(cookie.getSameSite()).isEqualTo("Lax");
80
assertThat(cookie.toJson().get("sameSite")).isEqualTo("Lax");
81
82
Cookie builderCookie = new Cookie.Builder("name", "value").sameSite("Lax").build();
83
assertThat(builderCookie.getSameSite()).isEqualTo("Lax");
84
assertThat(builderCookie.toJson().get("sameSite")).isEqualTo("Lax");
85
}
86
87
@Test
88
void testCookieSerializes() throws IOException, ClassNotFoundException {
89
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
90
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
91
Cookie cookieToSerialize = new Cookie("Fish", "cod", "", "", null, false, true, "Lax");
92
93
objectOutputStream.writeObject(cookieToSerialize);
94
byte[] serializedCookie = byteArrayOutputStream.toByteArray();
95
96
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedCookie);
97
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
98
Cookie deserializedCookie = (Cookie) objectInputStream.readObject();
99
assertThat(cookieToSerialize).isEqualTo(deserializedCookie);
100
}
101
}
102
103