Skip to content

Commit 41e6384

Browse files
authored
[bidi][java] Add continueRequest and continueResponse command (#13692)
1 parent 5a7272e commit 41e6384

File tree

8 files changed

+314
-0
lines changed

8 files changed

+314
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.openqa.selenium.bidi.HasBiDi;
3131
import org.openqa.selenium.bidi.network.AddInterceptParameters;
3232
import org.openqa.selenium.bidi.network.BeforeRequestSent;
33+
import org.openqa.selenium.bidi.network.ContinueRequestParameters;
34+
import org.openqa.selenium.bidi.network.ContinueResponseParameters;
3335
import org.openqa.selenium.bidi.network.FetchError;
3436
import org.openqa.selenium.bidi.network.ResponseDetails;
3537
import org.openqa.selenium.internal.Require;
@@ -122,6 +124,14 @@ public void failRequest(String requestId) {
122124
this.bidi.send(new Command<>("network.failRequest", Map.of("request", requestId)));
123125
}
124126

127+
public void continueRequest(ContinueRequestParameters parameters) {
128+
this.bidi.send(new Command<>("network.continueRequest", parameters.toMap()));
129+
}
130+
131+
public void continueResponse(ContinueResponseParameters parameters) {
132+
this.bidi.send(new Command<>("network.continueResponse", parameters.toMap()));
133+
}
134+
125135
public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
126136
if (browsingContextIds.isEmpty()) {
127137
this.bidi.addListener(beforeRequestSentEvent, consumer);

java/src/org/openqa/selenium/bidi/network/BytesValue.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.bidi.network;
1919

20+
import java.util.HashMap;
21+
import java.util.Map;
2022
import org.openqa.selenium.json.JsonInput;
2123

2224
public class BytesValue {
@@ -77,4 +79,12 @@ public Type getType() {
7779
public String getValue() {
7880
return value;
7981
}
82+
83+
public Map<String, String> toMap() {
84+
Map<String, String> map = new HashMap<>();
85+
map.put("type", type.toString());
86+
map.put("value", value);
87+
88+
return map;
89+
}
8090
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.List;
22+
import java.util.Map;
23+
import java.util.stream.Collectors;
24+
25+
public class ContinueRequestParameters {
26+
private final Map<String, Object> map = new HashMap<>();
27+
28+
public ContinueRequestParameters(String request) {
29+
map.put("request", request);
30+
}
31+
32+
public ContinueRequestParameters body(BytesValue value) {
33+
map.put("body", value.toMap());
34+
return this;
35+
}
36+
37+
public ContinueRequestParameters cookies(List<Header> cookieHeaders) {
38+
List<Map<String, Object>> cookies =
39+
cookieHeaders.stream().map(Header::toMap).collect(Collectors.toList());
40+
map.put("cookies", cookies);
41+
return this;
42+
}
43+
44+
public ContinueRequestParameters headers(List<Header> headers) {
45+
List<Map<String, Object>> headerList =
46+
headers.stream().map(Header::toMap).collect(Collectors.toList());
47+
map.put("headers", headerList);
48+
return this;
49+
}
50+
51+
public ContinueRequestParameters method(String method) {
52+
map.put("method", method);
53+
return this;
54+
}
55+
56+
public ContinueRequestParameters url(String url) {
57+
map.put("url", url);
58+
return this;
59+
}
60+
61+
public Map<String, Object> toMap() {
62+
return map;
63+
}
64+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.List;
22+
import java.util.Map;
23+
import java.util.stream.Collectors;
24+
import org.openqa.selenium.UsernameAndPassword;
25+
26+
public class ContinueResponseParameters {
27+
private final Map<String, Object> map = new HashMap<>();
28+
29+
public ContinueResponseParameters(String request) {
30+
map.put("request", request);
31+
}
32+
33+
public ContinueResponseParameters cookies(List<SetCookieHeader> cookieHeaders) {
34+
List<Map<String, Object>> cookies =
35+
cookieHeaders.stream().map(SetCookieHeader::toMap).collect(Collectors.toList());
36+
map.put("cookies", cookies);
37+
return this;
38+
}
39+
40+
public ContinueResponseParameters credentials(UsernameAndPassword credentials) {
41+
map.put(
42+
"credentials", Map.of("type", "password", credentials.password(), credentials.username()));
43+
return this;
44+
}
45+
46+
public ContinueResponseParameters headers(List<Header> headers) {
47+
List<Map<String, Object>> headerList =
48+
headers.stream().map(Header::toMap).collect(Collectors.toList());
49+
map.put("headers", headerList);
50+
return this;
51+
}
52+
53+
public ContinueResponseParameters reasonPhrase(String reasonPhrase) {
54+
map.put("reasonPhrase", reasonPhrase);
55+
return this;
56+
}
57+
58+
public ContinueResponseParameters statusCode(int statusCode) {
59+
map.put("statusCode", statusCode);
60+
return this;
61+
}
62+
63+
public Map<String, Object> toMap() {
64+
return map;
65+
}
66+
}

java/src/org/openqa/selenium/bidi/network/Cookie.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package org.openqa.selenium.bidi.network;
1818

19+
import java.util.HashMap;
20+
import java.util.Map;
1921
import java.util.Optional;
2022
import org.openqa.selenium.json.JsonInput;
2123

@@ -166,4 +168,20 @@ public SameSite getSameSite() {
166168
public Optional<Long> getExpiry() {
167169
return expiry;
168170
}
171+
172+
public Map<String, Object> toMap() {
173+
Map<String, Object> map = new HashMap<>();
174+
map.put("name", getName());
175+
map.put("value", getValue().toMap());
176+
map.put("domain", getDomain());
177+
map.put("path", getPath());
178+
map.put("size", getSize());
179+
map.put("secure", isSecure());
180+
map.put("httpOnly", isHttpOnly());
181+
map.put("sameSite", getSameSite().toString());
182+
183+
getExpiry().ifPresent(expiryValue -> map.put("expiry", expiryValue));
184+
185+
return map;
186+
}
169187
}

java/src/org/openqa/selenium/bidi/network/Header.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.bidi.network;
1919

20+
import java.util.HashMap;
21+
import java.util.Map;
2022
import org.openqa.selenium.json.JsonInput;
2123

2224
public class Header {
@@ -58,4 +60,12 @@ public String getName() {
5860
public BytesValue getValue() {
5961
return value;
6062
}
63+
64+
public Map<String, Object> toMap() {
65+
Map<String, Object> map = new HashMap<>();
66+
map.put("name", this.name);
67+
map.put("value", this.value.toMap());
68+
69+
return map;
70+
}
6171
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public class SetCookieHeader {
23+
24+
private final Map<String, Object> map = new HashMap<>();
25+
26+
public SetCookieHeader(String name, BytesValue value) {
27+
map.put("name", name);
28+
map.put("value", value);
29+
}
30+
31+
public SetCookieHeader domain(String domain) {
32+
map.put("domain", domain);
33+
return this;
34+
}
35+
36+
public SetCookieHeader path(String path) {
37+
map.put("path", path);
38+
return this;
39+
}
40+
41+
public SetCookieHeader maxAge(long maxAge) {
42+
map.put("maxAge", maxAge);
43+
return this;
44+
}
45+
46+
public SetCookieHeader httpOnly(boolean httpOnly) {
47+
map.put("httpOnly", httpOnly);
48+
return this;
49+
}
50+
51+
public SetCookieHeader secure(boolean secure) {
52+
map.put("secure", secure);
53+
return this;
54+
}
55+
56+
public SetCookieHeader sameSite(Cookie.SameSite sameSite) {
57+
map.put("sameSite", sameSite.toString());
58+
return this;
59+
}
60+
61+
public SetCookieHeader expiry(long expiry) {
62+
map.put("expiry", expiry);
63+
return this;
64+
}
65+
66+
public Map<String, Object> toMap() {
67+
return map;
68+
}
69+
}

java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import java.time.Duration;
2929
import java.time.temporal.ChronoUnit;
30+
import java.util.concurrent.CountDownLatch;
31+
import java.util.concurrent.TimeUnit;
3032
import org.junit.jupiter.api.AfterEach;
3133
import org.junit.jupiter.api.BeforeEach;
3234
import org.junit.jupiter.api.Test;
@@ -65,6 +67,71 @@ void canAddIntercept() {
6567
}
6668
}
6769

70+
@Test
71+
@NotYetImplemented(SAFARI)
72+
@NotYetImplemented(IE)
73+
@NotYetImplemented(EDGE)
74+
@NotYetImplemented(FIREFOX)
75+
void canContinueRequest() throws InterruptedException {
76+
try (Network network = new Network(driver)) {
77+
String intercept =
78+
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
79+
80+
CountDownLatch latch = new CountDownLatch(1);
81+
82+
// String alternatePage = server.whereIs("printPage.html");
83+
// TODO: Test sending request to alternate page once it is supported by browsers
84+
network.onBeforeRequestSent(
85+
beforeRequestSent -> {
86+
network.continueRequest(
87+
new ContinueRequestParameters(beforeRequestSent.getRequest().getRequestId()));
88+
89+
// network.continueRequest(
90+
// new
91+
// ContinueRequestParameters(beforeRequestSent.getRequest().getRequestId()).method("get").url(alternatePage));
92+
93+
latch.countDown();
94+
});
95+
96+
assertThat(intercept).isNotNull();
97+
98+
driver.get(server.whereIs("/bidi/logEntryAdded.html"));
99+
100+
boolean countdown = latch.await(5, TimeUnit.SECONDS);
101+
assertThat(countdown).isTrue();
102+
}
103+
}
104+
105+
@Test
106+
@NotYetImplemented(SAFARI)
107+
@NotYetImplemented(IE)
108+
@NotYetImplemented(EDGE)
109+
@NotYetImplemented(FIREFOX)
110+
void canContinueResponse() throws InterruptedException {
111+
try (Network network = new Network(driver)) {
112+
String intercept =
113+
network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED));
114+
115+
CountDownLatch latch = new CountDownLatch(1);
116+
117+
// TODO: Test sending response with a different status code once it is supported by the
118+
// browsers
119+
network.onResponseStarted(
120+
responseDetails -> {
121+
network.continueResponse(
122+
new ContinueResponseParameters(responseDetails.getRequest().getRequestId()));
123+
latch.countDown();
124+
});
125+
126+
assertThat(intercept).isNotNull();
127+
128+
driver.get(server.whereIs("/bidi/logEntryAdded.html"));
129+
130+
boolean countdown = latch.await(5, TimeUnit.SECONDS);
131+
assertThat(countdown).isTrue();
132+
}
133+
}
134+
68135
@Test
69136
@NotYetImplemented(SAFARI)
70137
@NotYetImplemented(IE)

0 commit comments

Comments
 (0)