Path: blob/trunk/java/test/org/openqa/selenium/CookieTest.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.assertj.core.api.Assertions.assertThatExceptionOfType;2122import java.io.ByteArrayInputStream;23import java.io.ByteArrayOutputStream;24import java.io.IOException;25import java.io.ObjectInputStream;26import java.io.ObjectOutputStream;27import java.util.Date;28import org.junit.jupiter.api.Tag;29import org.junit.jupiter.api.Test;3031@Tag("UnitTests")32class CookieTest {3334@Test35void testCanCreateAWellFormedCookie() {36new Cookie("Fish", "cod", "", "", null, false);37}3839@Test40void testShouldThrowAnExceptionWhenSemiColonExistsInTheCookieAttribute() {41Cookie cookie = new Cookie("hi;hi", "value", null, null, null, false);42assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(cookie::validate);43}4445@Test46void testShouldThrowAnExceptionTheNameIsNull() {47Cookie cookie = new Cookie(null, "value", null, null, null, false);48assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(cookie::validate);49}5051@Test52void testCookiesShouldAllowSecureToBeSet() {53Cookie cookie = new Cookie("name", "value", "", "/", new Date(), true);54assertThat(cookie.isSecure()).isTrue();55}5657@Test58void testSecureDefaultsToFalse() {59Cookie cookie = new Cookie("name", "value");60assertThat(cookie.isSecure()).isFalse();61}6263@Test64void testCookiesShouldAllowHttpOnlyToBeSet() {65Cookie cookie = new Cookie("name", "value", "", "/", new Date(), false, true);66assertThat(cookie.isHttpOnly()).isTrue();67}6869@Test70void testHttpOnlyDefaultsToFalse() {71Cookie cookie = new Cookie("name", "value");72assertThat(cookie.isHttpOnly()).isFalse();73}7475@Test76void testCookiesShouldAllowSameSiteToBeSet() {77Cookie cookie = new Cookie("name", "value", "", "/", new Date(), false, true, "Lax");78assertThat(cookie.getSameSite()).isEqualTo("Lax");79assertThat(cookie.toJson().get("sameSite")).isEqualTo("Lax");8081Cookie builderCookie = new Cookie.Builder("name", "value").sameSite("Lax").build();82assertThat(builderCookie.getSameSite()).isEqualTo("Lax");83assertThat(builderCookie.toJson().get("sameSite")).isEqualTo("Lax");84}8586@Test87void testCookieSerializes() throws IOException, ClassNotFoundException {88ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();89ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);90Cookie cookieToSerialize = new Cookie("Fish", "cod", "", "", null, false, true, "Lax");9192objectOutputStream.writeObject(cookieToSerialize);93byte[] serializedCookie = byteArrayOutputStream.toByteArray();9495ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedCookie);96ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);97Cookie deserializedCookie = (Cookie) objectInputStream.readObject();98assertThat(cookieToSerialize).isEqualTo(deserializedCookie);99}100}101102103