Skip to content

Commit 7461f1d

Browse files
committed
[java][cdp] Ensure request can be modified while intercepting
Fixes #12930
1 parent e8c3e9d commit 7461f1d

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

java/src/org/openqa/selenium/devtools/v116/v116Network.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req
196196

197197
return Fetch.continueRequest(
198198
pausedReq.getRequestId(),
199-
Optional.empty(),
199+
Optional.of(req.getUri()),
200200
Optional.of(req.getMethod().toString()),
201201
Optional.of(Base64.getEncoder().encodeToString(bos.toByteArray())),
202202
Optional.of(headers),

java/src/org/openqa/selenium/devtools/v117/v117Network.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req
191191

192192
return Fetch.continueRequest(
193193
pausedReq.getRequestId(),
194-
Optional.empty(),
194+
Optional.of(req.getUri()),
195195
Optional.of(req.getMethod().toString()),
196196
Optional.of(Base64.getEncoder().encodeToString(bos.toByteArray())),
197197
Optional.of(headers),

java/src/org/openqa/selenium/devtools/v118/v118Network.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req
191191

192192
return Fetch.continueRequest(
193193
pausedReq.getRequestId(),
194-
Optional.empty(),
194+
Optional.of(req.getUri()),
195195
Optional.of(req.getMethod().toString()),
196196
Optional.of(Base64.getEncoder().encodeToString(bos.toByteArray())),
197197
Optional.of(headers),

java/src/org/openqa/selenium/devtools/v85/V85Network.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req
200200

201201
return Fetch.continueRequest(
202202
pausedReq.getRequestId(),
203-
Optional.empty(),
203+
Optional.of(req.getUri()),
204204
Optional.of(req.getMethod().toString()),
205205
Optional.of(Base64.getEncoder().encodeToString(bos.toByteArray())),
206206
Optional.of(headers));

java/test/org/openqa/selenium/devtools/NetworkInterceptorTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.openqa.selenium.environment.webserver.NettyAppServer;
3939
import org.openqa.selenium.remote.http.Contents;
4040
import org.openqa.selenium.remote.http.Filter;
41+
import org.openqa.selenium.remote.http.HttpMethod;
42+
import org.openqa.selenium.remote.http.HttpRequest;
4143
import org.openqa.selenium.remote.http.HttpResponse;
4244
import org.openqa.selenium.remote.http.Route;
4345
import org.openqa.selenium.testing.JupiterTestBase;
@@ -78,6 +80,17 @@ public void setup() {
7880
utf8String(
7981
"<html><head><title>Hello,"
8082
+ " World!</title></head><body/></html>"))),
83+
Route.matching(req -> req.getUri().contains("london"))
84+
.to(
85+
() ->
86+
req ->
87+
new HttpResponse()
88+
.setStatus(200)
89+
.addHeader("Content-Type", XHTML_UTF_8.toString())
90+
.setContent(
91+
utf8String(
92+
"<html><head><title>Hello,"
93+
+ " London!</title></head><body/></html>"))),
8194
Route.get("/redirect")
8295
.to(
8396
() ->
@@ -132,6 +145,26 @@ void shouldAllowTheInterceptorToChangeTheResponse() {
132145
assertThat(source).contains("delicious cheese!");
133146
}
134147

148+
@Test
149+
@NoDriverBeforeTest
150+
void shouldAllowTheInterceptorToChangeTheRequest() {
151+
interceptor =
152+
new NetworkInterceptor(
153+
driver,
154+
(Filter)
155+
next ->
156+
req -> {
157+
req = new HttpRequest(HttpMethod.GET, appServer.whereIs("/london"));
158+
return next.execute(req);
159+
});
160+
161+
driver.get(appServer.whereIs("/cheese"));
162+
163+
String source = driver.getPageSource();
164+
165+
assertThat(source).contains("London");
166+
}
167+
135168
@Test
136169
@NoDriverBeforeTest
137170
void shouldBeAbleToReturnAMagicResponseThatCausesTheOriginalRequestToProceed() {

0 commit comments

Comments
 (0)