Skip to content

Commit

Permalink
[java][bidi] Add cookie support for network module (#13325)
Browse files Browse the repository at this point in the history
Fixes #13311
  • Loading branch information
pujagani authored Dec 18, 2023
1 parent 3792243 commit bb10753
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 12 deletions.
169 changes: 169 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/Cookie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.bidi.network;

import java.util.Optional;
import org.openqa.selenium.json.JsonInput;

public class Cookie {
public enum SameSite {
STRICT("strict"),
LAX("lax"),
NONE("none");

private final String type;

SameSite(String type) {
this.type = type;
}

@Override
public String toString() {
return type;
}

public static SameSite findByName(String name) {
SameSite result = null;
for (SameSite type : values()) {
if (type.toString().equalsIgnoreCase(name)) {
result = type;
break;
}
}
return result;
}
}

private final String name;
private final BytesValue value;
private final String domain;
private final String path;
private final long size;
private final boolean isSecure;
private final boolean isHttpOnly;
private final SameSite sameSite;
private final Optional<Long> expiry;

public Cookie(
String name,
BytesValue value,
String domain,
String path,
long size,
boolean isSecure,
boolean httpOnly,
SameSite sameSite,
Optional<Long> expiry) {
this.name = name;
this.value = value;
this.domain = domain;
this.path = path;
this.size = size;
this.isSecure = isSecure;
this.isHttpOnly = httpOnly;
this.sameSite = sameSite;
this.expiry = expiry;
}

public static Cookie fromJson(JsonInput input) {
String name = null;
BytesValue value = null;
String domain = null;
String path = null;
long size = 0;
boolean isSecure = false;
boolean isHttpOnly = false;
SameSite sameSite = null;
Optional<Long> expiry = Optional.empty();

input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "name":
name = input.read(String.class);
break;
case "value":
value = input.read(BytesValue.class);
break;
case "domain":
domain = input.read(String.class);
break;
case "path":
path = input.read(String.class);
break;
case "size":
size = input.read(Long.class);
break;
case "secure":
isSecure = input.read(Boolean.class);
break;
case "isHttpOnly":
isHttpOnly = input.read(Boolean.class);
break;
case "sameSite":
String sameSiteValue = input.read(String.class);
sameSite = SameSite.findByName(sameSiteValue);
break;
case "expiry":
expiry = input.read(Long.class);
break;
default:
input.skipValue();
}
}

input.endObject();

return new Cookie(name, value, domain, path, size, isSecure, isHttpOnly, sameSite, expiry);
}

public String getName() {
return name;
}

public BytesValue getValue() {
return value;
}

public String getDomain() {
return domain;
}

public String getPath() {
return path;
}

public long getSize() {
return size;
}

public boolean isSecure() {
return isSecure;
}

public boolean isHttpOnly() {
return isHttpOnly;
}

public SameSite getSameSite() {
return sameSite;
}

public Optional<Long> getExpiry() {
return expiry;
}
}
2 changes: 0 additions & 2 deletions java/src/org/openqa/selenium/bidi/network/RequestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.TypeToken;

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

private final List<Header> headers;

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

private final long headersSize;
Expand Down
51 changes: 41 additions & 10 deletions java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.openqa.selenium.testing.Safely.safelyCall;
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
import static org.openqa.selenium.testing.drivers.Browser.IE;
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -27,31 +30,29 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.bidi.Network;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.Pages;

class NetworkEventsTest {
class NetworkEventsTest extends JupiterTestBase {

private String page;
private AppServer server;
private FirefoxDriver driver;

@BeforeEach
public void setUp() {
FirefoxOptions options = (FirefoxOptions) Browser.FIREFOX.getCapabilities();
options.setCapability("webSocketUrl", true);

driver = new FirefoxDriver(options);

server = new NettyAppServer();
server.start();
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
void canListenToBeforeRequestSentEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
Expand All @@ -71,6 +72,9 @@ void canListenToBeforeRequestSentEvent()
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
void canListenToResponseStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
Expand All @@ -92,6 +96,9 @@ void canListenToResponseStartedEvent()
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
void canListenToResponseCompletedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
Expand All @@ -112,6 +119,30 @@ void canListenToResponseCompletedEvent()
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
void canListenToResponseCompletedEventWithCookie()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();

driver.get(new Pages(server).blankPage);
driver.manage().addCookie(new Cookie("foo", "bar"));
network.onBeforeRequestSent(future::complete);
driver.navigate().refresh();

BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
assertThat(requestSent.getBrowsingContextId()).isEqualTo(windowHandle);
assertThat(requestSent.getRequest().getCookies().size()).isEqualTo(1);
assertThat(requestSent.getRequest().getCookies().get(0).getName()).isEqualTo("foo");
assertThat(requestSent.getRequest().getCookies().get(0).getValue().getValue())
.isEqualTo("bar");
}
}

@AfterEach
public void quitDriver() {
if (driver != null) {
Expand Down

0 comments on commit bb10753

Please sign in to comment.