Skip to content

Commit

Permalink
[bidi][java] Add continueRequest and continueResponse command
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Mar 14, 2024
1 parent 83083c6 commit 8583136
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 0 deletions.
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ContinueRequestParameters;
import org.openqa.selenium.bidi.network.ContinueResponseParameters;
import org.openqa.selenium.bidi.network.FetchError;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.internal.Require;
Expand Down Expand Up @@ -122,6 +124,14 @@ public void failRequest(String requestId) {
this.bidi.send(new Command<>("network.failRequest", Map.of("request", requestId)));
}

public void continueRequest(ContinueRequestParameters parameters) {
this.bidi.send(new Command<>("network.continueRequest", parameters.toMap()));
}

public void continueResponse(ContinueResponseParameters parameters) {
this.bidi.send(new Command<>("network.continueResponse", parameters.toMap()));
}

public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(beforeRequestSentEvent, consumer);
Expand Down
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/BytesValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.bidi.network;

import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.json.JsonInput;

public class BytesValue {
Expand Down Expand Up @@ -77,4 +79,12 @@ public Type getType() {
public String getValue() {
return value;
}

public Map<String, String> toMap() {
Map<String, String> map = new HashMap<>();
map.put("type", type.toString());
map.put("value", value);

return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ContinueRequestParameters {
private final Map<String, Object> map = new HashMap<>();

public ContinueRequestParameters(String request) {
map.put("request", request);
}

public ContinueRequestParameters body(BytesValue value) {
map.put("body", value.toMap());
return this;
}

public ContinueRequestParameters cookies(List<Header> cookieHeaders) {
List<Map<String, Object>> cookies =
cookieHeaders.stream().map(Header::toMap).collect(Collectors.toList());
map.put("cookies", cookies);
return this;
}

public ContinueRequestParameters headers(List<Header> headers) {
List<Map<String, Object>> headerList =
headers.stream().map(Header::toMap).collect(Collectors.toList());
map.put("headers", headerList);
return this;
}

public ContinueRequestParameters method(String method) {
map.put("method", method);
return this;
}

public ContinueRequestParameters url(String url) {
map.put("url", url);
return this;
}

public Map<String, Object> toMap() {
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.openqa.selenium.UsernameAndPassword;

public class ContinueResponseParameters {
private final Map<String, Object> map = new HashMap<>();

public ContinueResponseParameters(String request) {
map.put("request", request);
}

public ContinueResponseParameters cookies(List<SetCookieHeader> cookieHeaders) {
List<Map<String, Object>> cookies =
cookieHeaders.stream().map(SetCookieHeader::toMap).collect(Collectors.toList());
map.put("cookies", cookies);
return this;
}

public ContinueResponseParameters credentials(UsernameAndPassword credentials) {
map.put(
"credentials", Map.of("type", "password", credentials.password(), credentials.username()));
return this;
}

public ContinueResponseParameters headers(List<Header> headers) {
List<Map<String, Object>> headerList =
headers.stream().map(Header::toMap).collect(Collectors.toList());
map.put("headers", headerList);
return this;
}

public ContinueResponseParameters reasonPhrase(String reasonPhrase) {
map.put("reasonPhrase", reasonPhrase);
return this;
}

public ContinueResponseParameters statusCode(int statusCode) {
map.put("statusCode", statusCode);
return this;
}

public Map<String, Object> toMap() {
return map;
}
}
18 changes: 18 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// under the License.
package org.openqa.selenium.bidi.network;

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

Expand Down Expand Up @@ -166,4 +168,20 @@ public SameSite getSameSite() {
public Optional<Long> getExpiry() {
return expiry;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("name", getName());
map.put("value", getValue().toMap());
map.put("domain", getDomain());
map.put("path", getPath());
map.put("size", getSize());
map.put("secure", isSecure());
map.put("httpOnly", isHttpOnly());
map.put("sameSite", getSameSite().toString());

getExpiry().ifPresent(expiryValue -> map.put("expiry", expiryValue));

return map;
}
}
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.bidi.network;

import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.json.JsonInput;

public class Header {
Expand Down Expand Up @@ -58,4 +60,12 @@ public String getName() {
public BytesValue getValue() {
return value;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("name", this.name);
map.put("value", this.value.toMap());

return map;
}
}
69 changes: 69 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/SetCookieHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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.HashMap;
import java.util.Map;

public class SetCookieHeader {

private final Map<String, Object> map = new HashMap<>();

public SetCookieHeader(String name, BytesValue value) {
map.put("name", name);
map.put("value", value);
}

public SetCookieHeader domain(String domain) {
map.put("domain", domain);
return this;
}

public SetCookieHeader path(String path) {
map.put("path", path);
return this;
}

public SetCookieHeader maxAge(long maxAge) {
map.put("maxAge", maxAge);
return this;
}

public SetCookieHeader httpOnly(boolean httpOnly) {
map.put("httpOnly", httpOnly);
return this;
}

public SetCookieHeader secure(boolean secure) {
map.put("secure", secure);
return this;
}

public SetCookieHeader sameSite(Cookie.SameSite sameSite) {
map.put("sameSite", sameSite.toString());
return this;
}

public SetCookieHeader expiry(long expiry) {
map.put("expiry", expiry);
return this;
}

public Map<String, Object> toMap() {
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -65,6 +67,71 @@ void canAddIntercept() {
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canContinueRequest() throws InterruptedException {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));

CountDownLatch latch = new CountDownLatch(1);

// String alternatePage = server.whereIs("printPage.html");
// TODO: Test sending request to alternate page once it is supported by browsers
network.onBeforeRequestSent(
beforeRequestSent -> {
network.continueRequest(
new ContinueRequestParameters(beforeRequestSent.getRequest().getRequestId()));

// network.continueRequest(
// new
// ContinueRequestParameters(beforeRequestSent.getRequest().getRequestId()).method("get").url(alternatePage));

latch.countDown();
});

assertThat(intercept).isNotNull();

driver.get(server.whereIs("/bidi/logEntryAdded.html"));

boolean countdown = latch.await(5, TimeUnit.SECONDS);
assertThat(countdown).isTrue();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canContinueResponse() throws InterruptedException {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED));

CountDownLatch latch = new CountDownLatch(1);

// TODO: Test sending response with a different status code once it is supported by the
// browsers
network.onResponseStarted(
responseDetails -> {
network.continueResponse(
new ContinueResponseParameters(responseDetails.getRequest().getRequestId()));
latch.countDown();
});

assertThat(intercept).isNotNull();

driver.get(server.whereIs("/bidi/logEntryAdded.html"));

boolean countdown = latch.await(5, TimeUnit.SECONDS);
assertThat(countdown).isTrue();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
Expand Down

0 comments on commit 8583136

Please sign in to comment.