Skip to content

Commit bb10753

Browse files
authored
[java][bidi] Add cookie support for network module (#13325)
Fixes #13311
1 parent 3792243 commit bb10753

File tree

3 files changed

+210
-12
lines changed

3 files changed

+210
-12
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
package org.openqa.selenium.bidi.network;
18+
19+
import java.util.Optional;
20+
import org.openqa.selenium.json.JsonInput;
21+
22+
public class Cookie {
23+
public enum SameSite {
24+
STRICT("strict"),
25+
LAX("lax"),
26+
NONE("none");
27+
28+
private final String type;
29+
30+
SameSite(String type) {
31+
this.type = type;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return type;
37+
}
38+
39+
public static SameSite findByName(String name) {
40+
SameSite result = null;
41+
for (SameSite type : values()) {
42+
if (type.toString().equalsIgnoreCase(name)) {
43+
result = type;
44+
break;
45+
}
46+
}
47+
return result;
48+
}
49+
}
50+
51+
private final String name;
52+
private final BytesValue value;
53+
private final String domain;
54+
private final String path;
55+
private final long size;
56+
private final boolean isSecure;
57+
private final boolean isHttpOnly;
58+
private final SameSite sameSite;
59+
private final Optional<Long> expiry;
60+
61+
public Cookie(
62+
String name,
63+
BytesValue value,
64+
String domain,
65+
String path,
66+
long size,
67+
boolean isSecure,
68+
boolean httpOnly,
69+
SameSite sameSite,
70+
Optional<Long> expiry) {
71+
this.name = name;
72+
this.value = value;
73+
this.domain = domain;
74+
this.path = path;
75+
this.size = size;
76+
this.isSecure = isSecure;
77+
this.isHttpOnly = httpOnly;
78+
this.sameSite = sameSite;
79+
this.expiry = expiry;
80+
}
81+
82+
public static Cookie fromJson(JsonInput input) {
83+
String name = null;
84+
BytesValue value = null;
85+
String domain = null;
86+
String path = null;
87+
long size = 0;
88+
boolean isSecure = false;
89+
boolean isHttpOnly = false;
90+
SameSite sameSite = null;
91+
Optional<Long> expiry = Optional.empty();
92+
93+
input.beginObject();
94+
while (input.hasNext()) {
95+
switch (input.nextName()) {
96+
case "name":
97+
name = input.read(String.class);
98+
break;
99+
case "value":
100+
value = input.read(BytesValue.class);
101+
break;
102+
case "domain":
103+
domain = input.read(String.class);
104+
break;
105+
case "path":
106+
path = input.read(String.class);
107+
break;
108+
case "size":
109+
size = input.read(Long.class);
110+
break;
111+
case "secure":
112+
isSecure = input.read(Boolean.class);
113+
break;
114+
case "isHttpOnly":
115+
isHttpOnly = input.read(Boolean.class);
116+
break;
117+
case "sameSite":
118+
String sameSiteValue = input.read(String.class);
119+
sameSite = SameSite.findByName(sameSiteValue);
120+
break;
121+
case "expiry":
122+
expiry = input.read(Long.class);
123+
break;
124+
default:
125+
input.skipValue();
126+
}
127+
}
128+
129+
input.endObject();
130+
131+
return new Cookie(name, value, domain, path, size, isSecure, isHttpOnly, sameSite, expiry);
132+
}
133+
134+
public String getName() {
135+
return name;
136+
}
137+
138+
public BytesValue getValue() {
139+
return value;
140+
}
141+
142+
public String getDomain() {
143+
return domain;
144+
}
145+
146+
public String getPath() {
147+
return path;
148+
}
149+
150+
public long getSize() {
151+
return size;
152+
}
153+
154+
public boolean isSecure() {
155+
return isSecure;
156+
}
157+
158+
public boolean isHttpOnly() {
159+
return isHttpOnly;
160+
}
161+
162+
public SameSite getSameSite() {
163+
return sameSite;
164+
}
165+
166+
public Optional<Long> getExpiry() {
167+
return expiry;
168+
}
169+
}

java/src/org/openqa/selenium/bidi/network/RequestData.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
22-
import org.openqa.selenium.Cookie;
2322
import org.openqa.selenium.json.JsonInput;
2423
import org.openqa.selenium.json.TypeToken;
2524

@@ -32,7 +31,6 @@ public class RequestData {
3231

3332
private final List<Header> headers;
3433

35-
// TODO: add from json method there
3634
private final List<Cookie> cookies;
3735

3836
private final long headersSize;

java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2121
import static org.openqa.selenium.testing.Safely.safelyCall;
22+
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
23+
import static org.openqa.selenium.testing.drivers.Browser.IE;
24+
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2225

2326
import java.util.concurrent.CompletableFuture;
2427
import java.util.concurrent.ExecutionException;
@@ -27,31 +30,29 @@
2730
import org.junit.jupiter.api.AfterEach;
2831
import org.junit.jupiter.api.BeforeEach;
2932
import org.junit.jupiter.api.Test;
33+
import org.openqa.selenium.Cookie;
3034
import org.openqa.selenium.bidi.Network;
3135
import org.openqa.selenium.environment.webserver.AppServer;
3236
import org.openqa.selenium.environment.webserver.NettyAppServer;
33-
import org.openqa.selenium.firefox.FirefoxDriver;
34-
import org.openqa.selenium.firefox.FirefoxOptions;
35-
import org.openqa.selenium.testing.drivers.Browser;
37+
import org.openqa.selenium.testing.JupiterTestBase;
38+
import org.openqa.selenium.testing.NotYetImplemented;
39+
import org.openqa.selenium.testing.Pages;
3640

37-
class NetworkEventsTest {
41+
class NetworkEventsTest extends JupiterTestBase {
3842

3943
private String page;
4044
private AppServer server;
41-
private FirefoxDriver driver;
4245

4346
@BeforeEach
4447
public void setUp() {
45-
FirefoxOptions options = (FirefoxOptions) Browser.FIREFOX.getCapabilities();
46-
options.setCapability("webSocketUrl", true);
47-
48-
driver = new FirefoxDriver(options);
49-
5048
server = new NettyAppServer();
5149
server.start();
5250
}
5351

5452
@Test
53+
@NotYetImplemented(SAFARI)
54+
@NotYetImplemented(IE)
55+
@NotYetImplemented(EDGE)
5556
void canListenToBeforeRequestSentEvent()
5657
throws ExecutionException, InterruptedException, TimeoutException {
5758
try (Network network = new Network(driver)) {
@@ -71,6 +72,9 @@ void canListenToBeforeRequestSentEvent()
7172
}
7273

7374
@Test
75+
@NotYetImplemented(SAFARI)
76+
@NotYetImplemented(IE)
77+
@NotYetImplemented(EDGE)
7478
void canListenToResponseStartedEvent()
7579
throws ExecutionException, InterruptedException, TimeoutException {
7680
try (Network network = new Network(driver)) {
@@ -92,6 +96,9 @@ void canListenToResponseStartedEvent()
9296
}
9397

9498
@Test
99+
@NotYetImplemented(SAFARI)
100+
@NotYetImplemented(IE)
101+
@NotYetImplemented(EDGE)
95102
void canListenToResponseCompletedEvent()
96103
throws ExecutionException, InterruptedException, TimeoutException {
97104
try (Network network = new Network(driver)) {
@@ -112,6 +119,30 @@ void canListenToResponseCompletedEvent()
112119
}
113120
}
114121

122+
@Test
123+
@NotYetImplemented(SAFARI)
124+
@NotYetImplemented(IE)
125+
@NotYetImplemented(EDGE)
126+
void canListenToResponseCompletedEventWithCookie()
127+
throws ExecutionException, InterruptedException, TimeoutException {
128+
try (Network network = new Network(driver)) {
129+
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
130+
131+
driver.get(new Pages(server).blankPage);
132+
driver.manage().addCookie(new Cookie("foo", "bar"));
133+
network.onBeforeRequestSent(future::complete);
134+
driver.navigate().refresh();
135+
136+
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
137+
String windowHandle = driver.getWindowHandle();
138+
assertThat(requestSent.getBrowsingContextId()).isEqualTo(windowHandle);
139+
assertThat(requestSent.getRequest().getCookies().size()).isEqualTo(1);
140+
assertThat(requestSent.getRequest().getCookies().get(0).getName()).isEqualTo("foo");
141+
assertThat(requestSent.getRequest().getCookies().get(0).getValue().getValue())
142+
.isEqualTo("bar");
143+
}
144+
}
145+
115146
@AfterEach
116147
public void quitDriver() {
117148
if (driver != null) {

0 commit comments

Comments
 (0)