Skip to content

Commit

Permalink
[bidi][java] Add network intercept commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored Jan 25, 2024
1 parent 17d0491 commit 569e64b
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/src/org/openqa/selenium/bidi/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.internal.Require;
Expand Down Expand Up @@ -64,6 +66,21 @@ public Network(Set<String> browsingContextIds, WebDriver driver) {
this.browsingContextIds = browsingContextIds;
}

public String addIntercept(AddInterceptParameters parameters) {
return this.bidi.send(
new Command<>(
"network.addIntercept",
parameters.toMap(),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
return (String) result.get("intercept");
}));
}

public void removeIntercept(String interceptId) {
this.bidi.send(new Command<>("network.removeIntercept", Map.of("intercept", interceptId)));
}

public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(beforeRequestSentEvent, consumer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddInterceptParameters {

private final List<String> phases = new ArrayList<>();

private final List<Map<String, String>> urlPatterns = new ArrayList<>();

public AddInterceptParameters(InterceptPhase phase) {
this.phases.add(phase.toString());
}

public AddInterceptParameters(List<InterceptPhase> phases) {
phases.forEach(phase -> this.phases.add(phase.toString()));
}

public AddInterceptParameters urlPattern(UrlPattern pattern) {
this.urlPatterns.add(pattern.toMap());
return this;
}

public AddInterceptParameters urlPatterns(List<UrlPattern> patterns) {
patterns.forEach(pattern -> this.urlPatterns.add(pattern.toMap()));
return this;
}

public AddInterceptParameters urlStringPattern(String pattern) {
this.urlPatterns.add(Map.of("type", "string", "pattern", pattern));
return this;
}

public AddInterceptParameters urlStringPatterns(List<String> patterns) {
patterns.forEach(pattern -> this.urlPatterns.add(Map.of("type", "string", "pattern", pattern)));
return this;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("phases", phases);
if (!urlPatterns.isEmpty()) {
map.put("urlPatterns", urlPatterns);
}
return map;
}
}
34 changes: 34 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/InterceptPhase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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;

public enum InterceptPhase {
BEFORE_REQUEST_SENT("beforeRequestSent"),
RESPONSE_STARTED("responseStarted"),
AUTH_REQUIRED("authRequired");

private final String value;

InterceptPhase(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}
55 changes: 55 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/UrlPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 UrlPattern {
private final Map<String, String> map = new HashMap<>();

public UrlPattern protocol(String protocol) {
map.put("protocol", protocol);
return this;
}

public UrlPattern hostname(String hostname) {
map.put("hostname", hostname);
return this;
}

public UrlPattern port(String port) {
map.put("port", port);
return this;
}

public UrlPattern pathname(String pathname) {
map.put("pathname", pathname);
return this;
}

public UrlPattern search(String search) {
map.put("search", search);
return this;
}

public Map<String, String> toMap() {
map.put("type", "pattern");
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// 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 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.FIREFOX;
import static org.openqa.selenium.testing.drivers.Browser.IE;
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;

import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.bidi.Network;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NotYetImplemented;

class AddInterceptParametersTest extends JupiterTestBase {

private AppServer server;

@BeforeEach
public void setUp() {
server = new NettyAppServer();
server.start();
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canAddInterceptPhase() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
assertThat(intercept).isNotNull();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canAddInterceptPhases() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(
new AddInterceptParameters(
List.of(InterceptPhase.BEFORE_REQUEST_SENT, InterceptPhase.RESPONSE_STARTED)));
assertThat(intercept).isNotNull();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canAddStringUrlPattern() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)
.urlStringPattern("http://localhost:4444/basicAuth"));
assertThat(intercept).isNotNull();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canAddStringUrlPatterns() {
try (Network network = new Network(driver)) {
String intercept =
network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)
.urlStringPatterns(
List.of(
"http://localhost:4444/basicAuth",
"http://localhost:4445/logEntryAdded")));
assertThat(intercept).isNotNull();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canAddUrlPattern() {
try (Network network = new Network(driver)) {
UrlPattern pattern =
new UrlPattern()
.hostname("localhost")
.pathname("/logEntryAdded")
.port("4444")
.protocol("http")
.search("");

String intercept =
network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT).urlPattern(pattern));
assertThat(intercept).isNotNull();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canAddUrlPatterns() {
try (Network network = new Network(driver)) {
UrlPattern pattern1 =
new UrlPattern()
.hostname("localhost")
.pathname("/logEntryAdded")
.port("4444")
.protocol("http")
.search("");

UrlPattern pattern2 =
new UrlPattern()
.hostname("localhost")
.pathname("/basicAuth")
.port("4445")
.protocol("https")
.search("auth");

String intercept =
network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)
.urlPatterns(List.of(pattern1, pattern2)));
assertThat(intercept).isNotNull();
}
}

@AfterEach
public void quitDriver() {
if (driver != null) {
driver.quit();
}
safelyCall(server::stop);
}
}
Loading

0 comments on commit 569e64b

Please sign in to comment.