Skip to content

Commit 569e64b

Browse files
authored
[bidi][java] Add network intercept commands
1 parent 17d0491 commit 569e64b

File tree

6 files changed

+421
-0
lines changed

6 files changed

+421
-0
lines changed

java/src/org/openqa/selenium/bidi/Network.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import java.util.Collections;
2121
import java.util.HashSet;
22+
import java.util.Map;
2223
import java.util.Set;
2324
import java.util.function.Consumer;
2425
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.bidi.network.AddInterceptParameters;
2527
import org.openqa.selenium.bidi.network.BeforeRequestSent;
2628
import org.openqa.selenium.bidi.network.ResponseDetails;
2729
import org.openqa.selenium.internal.Require;
@@ -64,6 +66,21 @@ public Network(Set<String> browsingContextIds, WebDriver driver) {
6466
this.browsingContextIds = browsingContextIds;
6567
}
6668

69+
public String addIntercept(AddInterceptParameters parameters) {
70+
return this.bidi.send(
71+
new Command<>(
72+
"network.addIntercept",
73+
parameters.toMap(),
74+
jsonInput -> {
75+
Map<String, Object> result = jsonInput.read(Map.class);
76+
return (String) result.get("intercept");
77+
}));
78+
}
79+
80+
public void removeIntercept(String interceptId) {
81+
this.bidi.send(new Command<>("network.removeIntercept", Map.of("intercept", interceptId)));
82+
}
83+
6784
public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
6885
if (browsingContextIds.isEmpty()) {
6986
this.bidi.addListener(beforeRequestSentEvent, consumer);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
18+
package org.openqa.selenium.bidi.network;
19+
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
public class AddInterceptParameters {
26+
27+
private final List<String> phases = new ArrayList<>();
28+
29+
private final List<Map<String, String>> urlPatterns = new ArrayList<>();
30+
31+
public AddInterceptParameters(InterceptPhase phase) {
32+
this.phases.add(phase.toString());
33+
}
34+
35+
public AddInterceptParameters(List<InterceptPhase> phases) {
36+
phases.forEach(phase -> this.phases.add(phase.toString()));
37+
}
38+
39+
public AddInterceptParameters urlPattern(UrlPattern pattern) {
40+
this.urlPatterns.add(pattern.toMap());
41+
return this;
42+
}
43+
44+
public AddInterceptParameters urlPatterns(List<UrlPattern> patterns) {
45+
patterns.forEach(pattern -> this.urlPatterns.add(pattern.toMap()));
46+
return this;
47+
}
48+
49+
public AddInterceptParameters urlStringPattern(String pattern) {
50+
this.urlPatterns.add(Map.of("type", "string", "pattern", pattern));
51+
return this;
52+
}
53+
54+
public AddInterceptParameters urlStringPatterns(List<String> patterns) {
55+
patterns.forEach(pattern -> this.urlPatterns.add(Map.of("type", "string", "pattern", pattern)));
56+
return this;
57+
}
58+
59+
public Map<String, Object> toMap() {
60+
Map<String, Object> map = new HashMap<>();
61+
map.put("phases", phases);
62+
if (!urlPatterns.isEmpty()) {
63+
map.put("urlPatterns", urlPatterns);
64+
}
65+
return map;
66+
}
67+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
public enum InterceptPhase {
20+
BEFORE_REQUEST_SENT("beforeRequestSent"),
21+
RESPONSE_STARTED("responseStarted"),
22+
AUTH_REQUIRED("authRequired");
23+
24+
private final String value;
25+
26+
InterceptPhase(String value) {
27+
this.value = value;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return value;
33+
}
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
18+
package org.openqa.selenium.bidi.network;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class UrlPattern {
24+
private final Map<String, String> map = new HashMap<>();
25+
26+
public UrlPattern protocol(String protocol) {
27+
map.put("protocol", protocol);
28+
return this;
29+
}
30+
31+
public UrlPattern hostname(String hostname) {
32+
map.put("hostname", hostname);
33+
return this;
34+
}
35+
36+
public UrlPattern port(String port) {
37+
map.put("port", port);
38+
return this;
39+
}
40+
41+
public UrlPattern pathname(String pathname) {
42+
map.put("pathname", pathname);
43+
return this;
44+
}
45+
46+
public UrlPattern search(String search) {
47+
map.put("search", search);
48+
return this;
49+
}
50+
51+
public Map<String, String> toMap() {
52+
map.put("type", "pattern");
53+
return map;
54+
}
55+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
18+
package org.openqa.selenium.bidi.network;
19+
20+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21+
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.FIREFOX;
24+
import static org.openqa.selenium.testing.drivers.Browser.IE;
25+
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
26+
27+
import java.util.List;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
import org.openqa.selenium.bidi.Network;
32+
import org.openqa.selenium.environment.webserver.AppServer;
33+
import org.openqa.selenium.environment.webserver.NettyAppServer;
34+
import org.openqa.selenium.testing.JupiterTestBase;
35+
import org.openqa.selenium.testing.NotYetImplemented;
36+
37+
class AddInterceptParametersTest extends JupiterTestBase {
38+
39+
private AppServer server;
40+
41+
@BeforeEach
42+
public void setUp() {
43+
server = new NettyAppServer();
44+
server.start();
45+
}
46+
47+
@Test
48+
@NotYetImplemented(SAFARI)
49+
@NotYetImplemented(IE)
50+
@NotYetImplemented(EDGE)
51+
@NotYetImplemented(FIREFOX)
52+
void canAddInterceptPhase() {
53+
try (Network network = new Network(driver)) {
54+
String intercept =
55+
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
56+
assertThat(intercept).isNotNull();
57+
}
58+
}
59+
60+
@Test
61+
@NotYetImplemented(SAFARI)
62+
@NotYetImplemented(IE)
63+
@NotYetImplemented(EDGE)
64+
@NotYetImplemented(FIREFOX)
65+
void canAddInterceptPhases() {
66+
try (Network network = new Network(driver)) {
67+
String intercept =
68+
network.addIntercept(
69+
new AddInterceptParameters(
70+
List.of(InterceptPhase.BEFORE_REQUEST_SENT, InterceptPhase.RESPONSE_STARTED)));
71+
assertThat(intercept).isNotNull();
72+
}
73+
}
74+
75+
@Test
76+
@NotYetImplemented(SAFARI)
77+
@NotYetImplemented(IE)
78+
@NotYetImplemented(EDGE)
79+
@NotYetImplemented(FIREFOX)
80+
void canAddStringUrlPattern() {
81+
try (Network network = new Network(driver)) {
82+
String intercept =
83+
network.addIntercept(
84+
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)
85+
.urlStringPattern("http://localhost:4444/basicAuth"));
86+
assertThat(intercept).isNotNull();
87+
}
88+
}
89+
90+
@Test
91+
@NotYetImplemented(SAFARI)
92+
@NotYetImplemented(IE)
93+
@NotYetImplemented(EDGE)
94+
@NotYetImplemented(FIREFOX)
95+
void canAddStringUrlPatterns() {
96+
try (Network network = new Network(driver)) {
97+
String intercept =
98+
network.addIntercept(
99+
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)
100+
.urlStringPatterns(
101+
List.of(
102+
"http://localhost:4444/basicAuth",
103+
"http://localhost:4445/logEntryAdded")));
104+
assertThat(intercept).isNotNull();
105+
}
106+
}
107+
108+
@Test
109+
@NotYetImplemented(SAFARI)
110+
@NotYetImplemented(IE)
111+
@NotYetImplemented(EDGE)
112+
@NotYetImplemented(FIREFOX)
113+
void canAddUrlPattern() {
114+
try (Network network = new Network(driver)) {
115+
UrlPattern pattern =
116+
new UrlPattern()
117+
.hostname("localhost")
118+
.pathname("/logEntryAdded")
119+
.port("4444")
120+
.protocol("http")
121+
.search("");
122+
123+
String intercept =
124+
network.addIntercept(
125+
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT).urlPattern(pattern));
126+
assertThat(intercept).isNotNull();
127+
}
128+
}
129+
130+
@Test
131+
@NotYetImplemented(SAFARI)
132+
@NotYetImplemented(IE)
133+
@NotYetImplemented(EDGE)
134+
@NotYetImplemented(FIREFOX)
135+
void canAddUrlPatterns() {
136+
try (Network network = new Network(driver)) {
137+
UrlPattern pattern1 =
138+
new UrlPattern()
139+
.hostname("localhost")
140+
.pathname("/logEntryAdded")
141+
.port("4444")
142+
.protocol("http")
143+
.search("");
144+
145+
UrlPattern pattern2 =
146+
new UrlPattern()
147+
.hostname("localhost")
148+
.pathname("/basicAuth")
149+
.port("4445")
150+
.protocol("https")
151+
.search("auth");
152+
153+
String intercept =
154+
network.addIntercept(
155+
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)
156+
.urlPatterns(List.of(pattern1, pattern2)));
157+
assertThat(intercept).isNotNull();
158+
}
159+
}
160+
161+
@AfterEach
162+
public void quitDriver() {
163+
if (driver != null) {
164+
driver.quit();
165+
}
166+
safelyCall(server::stop);
167+
}
168+
}

0 commit comments

Comments
 (0)