-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bidi][java] Add network intercept commands
- Loading branch information
Showing
6 changed files
with
421 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
java/src/org/openqa/selenium/bidi/network/AddInterceptParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
java/src/org/openqa/selenium/bidi/network/InterceptPhase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
java/test/org/openqa/selenium/bidi/network/AddInterceptParametersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.