diff --git a/java/src/org/openqa/selenium/bidi/Network.java b/java/src/org/openqa/selenium/bidi/Network.java index 4cba35cc180fc..ddc53af097b83 100644 --- a/java/src/org/openqa/selenium/bidi/Network.java +++ b/java/src/org/openqa/selenium/bidi/Network.java @@ -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; @@ -64,6 +66,21 @@ public Network(Set browsingContextIds, WebDriver driver) { this.browsingContextIds = browsingContextIds; } + public String addIntercept(AddInterceptParameters parameters) { + return this.bidi.send( + new Command<>( + "network.addIntercept", + parameters.toMap(), + jsonInput -> { + Map 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 consumer) { if (browsingContextIds.isEmpty()) { this.bidi.addListener(beforeRequestSentEvent, consumer); diff --git a/java/src/org/openqa/selenium/bidi/network/AddInterceptParameters.java b/java/src/org/openqa/selenium/bidi/network/AddInterceptParameters.java new file mode 100644 index 0000000000000..1c0611364e98c --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/network/AddInterceptParameters.java @@ -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 phases = new ArrayList<>(); + + private final List> urlPatterns = new ArrayList<>(); + + public AddInterceptParameters(InterceptPhase phase) { + this.phases.add(phase.toString()); + } + + public AddInterceptParameters(List 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 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 patterns) { + patterns.forEach(pattern -> this.urlPatterns.add(Map.of("type", "string", "pattern", pattern))); + return this; + } + + public Map toMap() { + Map map = new HashMap<>(); + map.put("phases", phases); + if (!urlPatterns.isEmpty()) { + map.put("urlPatterns", urlPatterns); + } + return map; + } +} diff --git a/java/src/org/openqa/selenium/bidi/network/InterceptPhase.java b/java/src/org/openqa/selenium/bidi/network/InterceptPhase.java new file mode 100644 index 0000000000000..bec0b451360ae --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/network/InterceptPhase.java @@ -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; + } +} diff --git a/java/src/org/openqa/selenium/bidi/network/UrlPattern.java b/java/src/org/openqa/selenium/bidi/network/UrlPattern.java new file mode 100644 index 0000000000000..eb021db05b1ef --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/network/UrlPattern.java @@ -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 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 toMap() { + map.put("type", "pattern"); + return map; + } +} diff --git a/java/test/org/openqa/selenium/bidi/network/AddInterceptParametersTest.java b/java/test/org/openqa/selenium/bidi/network/AddInterceptParametersTest.java new file mode 100644 index 0000000000000..9174699d84d90 --- /dev/null +++ b/java/test/org/openqa/selenium/bidi/network/AddInterceptParametersTest.java @@ -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); + } +} diff --git a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java new file mode 100644 index 0000000000000..d37ad33d3dfbc --- /dev/null +++ b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java @@ -0,0 +1,80 @@ +// 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 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 NetworkCommandsTest extends JupiterTestBase { + private AppServer server; + + @BeforeEach + public void setUp() { + server = new NettyAppServer(); + server.start(); + } + + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) + @NotYetImplemented(FIREFOX) + void canAddIntercept() { + 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 canRemoveIntercept() { + try (Network network = new Network(driver)) { + String intercept = + network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)); + assertThat(intercept).isNotNull(); + + network.removeIntercept(intercept); + } + } + + @AfterEach + public void quitDriver() { + if (driver != null) { + driver.quit(); + } + safelyCall(server::stop); + } +}