Skip to content

Commit

Permalink
[cdp] Fetch.requestPaused doesn't handle redirects well
Browse files Browse the repository at this point in the history
If we can't retrieve the body of the request, don't abandon hope, just
abandon getting the body of the response.
  • Loading branch information
shs96c committed Sep 13, 2021
1 parent a4f8c86 commit 968a87a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 15 deletions.
5 changes: 4 additions & 1 deletion java/src/org/openqa/selenium/devtools/idealized/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ protected HttpResponse createHttpResponse(
Boolean bodyIsBase64Encoded,
List<Map.Entry<String, String>> headers) {
Supplier<InputStream> content;
if (bodyIsBase64Encoded != null && bodyIsBase64Encoded) {

if (body == null) {
content = Contents.empty();
} else if (bodyIsBase64Encoded != null && bodyIsBase64Encoded) {
byte[] decoded = Base64.getDecoder().decode(body);
content = () -> new ByteArrayInputStream(decoded);
} else {
Expand Down
29 changes: 25 additions & 4 deletions java/src/org/openqa/selenium/devtools/v85/V85Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.DevToolsException;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.Network;
import org.openqa.selenium.devtools.v85.fetch.Fetch;
Expand All @@ -45,9 +46,14 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

import static java.net.HttpURLConnection.HTTP_OK;

public class V85Network extends Network<AuthRequired, RequestPaused> {

private static final Logger LOG = Logger.getLogger(V85Network.class.getName());

public V85Network(DevTools devTools) {
super(devTools);
}
Expand Down Expand Up @@ -117,16 +123,32 @@ public Event<RequestPaused> requestPausedEvent() {
@Override
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq) {
if (pausedReq.getResponseStatusCode().isPresent() || pausedReq.getResponseErrorReason().isPresent()) {
Fetch.GetResponseBodyResponse base64Body = devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
String body;
boolean bodyIsBase64Encoded;

try {
Fetch.GetResponseBodyResponse base64Body = devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
body = base64Body.getBody();
bodyIsBase64Encoded = base64Body.getBase64Encoded() != null && base64Body.getBase64Encoded();
} catch (DevToolsException e) {
// Redirects don't seem to have bodies
int code = pausedReq.getResponseStatusCode().orElse(HTTP_OK);
if (code < 300 && code > 399) {
LOG.warning("Unable to get body for request id " + pausedReq.getRequestId());
}

body = null;
bodyIsBase64Encoded = false;
}

List<Map.Entry<String, String>> headers = new LinkedList<>();
pausedReq.getResponseHeaders().ifPresent(resHeaders ->
resHeaders.forEach(header -> headers.add(new AbstractMap.SimpleEntry<>(header.getName(), header.getValue()))));

HttpResponse res = createHttpResponse(
pausedReq.getResponseStatusCode(),
base64Body.getBody(),
base64Body.getBase64Encoded(),
body,
bodyIsBase64Encoded,
headers);

return Either.right(res);
Expand All @@ -142,7 +164,6 @@ public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedRe

return Either.left(req);
}

@Override
protected String getRequestId(RequestPaused pausedReq) {
return pausedReq.getRequestId().toString();
Expand Down
28 changes: 25 additions & 3 deletions java/src/org/openqa/selenium/devtools/v92/V92Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.DevToolsException;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.Network;
import org.openqa.selenium.devtools.v92.fetch.Fetch;
Expand All @@ -45,9 +46,14 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

import static java.net.HttpURLConnection.HTTP_OK;

public class V92Network extends Network<AuthRequired, RequestPaused> {

private static final Logger LOG = Logger.getLogger(V92Network.class.getName());

public V92Network(DevTools devTools) {
super(devTools);
}
Expand Down Expand Up @@ -117,16 +123,32 @@ public Event<RequestPaused> requestPausedEvent() {
@Override
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq) {
if (pausedReq.getResponseStatusCode().isPresent() || pausedReq.getResponseErrorReason().isPresent()) {
Fetch.GetResponseBodyResponse base64Body = devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
String body;
boolean bodyIsBase64Encoded;

try {
Fetch.GetResponseBodyResponse base64Body = devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
body = base64Body.getBody();
bodyIsBase64Encoded = base64Body.getBase64Encoded() != null && base64Body.getBase64Encoded();
} catch (DevToolsException e) {
// Redirects don't seem to have bodies
int code = pausedReq.getResponseStatusCode().orElse(HTTP_OK);
if (code < 300 && code > 399) {
LOG.warning("Unable to get body for request id " + pausedReq.getRequestId());
}

body = null;
bodyIsBase64Encoded = false;
}

List<Map.Entry<String, String>> headers = new LinkedList<>();
pausedReq.getResponseHeaders().ifPresent(resHeaders ->
resHeaders.forEach(header -> headers.add(new AbstractMap.SimpleEntry<>(header.getName(), header.getValue()))));

HttpResponse res = createHttpResponse(
pausedReq.getResponseStatusCode(),
base64Body.getBody(),
base64Body.getBase64Encoded(),
body,
bodyIsBase64Encoded,
headers);

return Either.right(res);
Expand Down
28 changes: 25 additions & 3 deletions java/src/org/openqa/selenium/devtools/v93/V93Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.DevToolsException;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.Network;
import org.openqa.selenium.devtools.v93.fetch.Fetch;
Expand All @@ -45,9 +46,14 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

import static java.net.HttpURLConnection.HTTP_OK;

public class V93Network extends Network<AuthRequired, RequestPaused> {

private static final Logger LOG = Logger.getLogger(V93Network.class.getName());

public V93Network(DevTools devTools) {
super(devTools);
}
Expand Down Expand Up @@ -117,16 +123,32 @@ public Event<RequestPaused> requestPausedEvent() {
@Override
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq) {
if (pausedReq.getResponseStatusCode().isPresent() || pausedReq.getResponseErrorReason().isPresent()) {
Fetch.GetResponseBodyResponse base64Body = devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
String body;
boolean bodyIsBase64Encoded;

try {
Fetch.GetResponseBodyResponse base64Body = devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
body = base64Body.getBody();
bodyIsBase64Encoded = base64Body.getBase64Encoded() != null && base64Body.getBase64Encoded();
} catch (DevToolsException e) {
// Redirects don't seem to have bodies
int code = pausedReq.getResponseStatusCode().orElse(HTTP_OK);
if (code < 300 && code > 399) {
LOG.warning("Unable to get body for request id " + pausedReq.getRequestId());
}

body = null;
bodyIsBase64Encoded = false;
}

List<Map.Entry<String, String>> headers = new LinkedList<>();
pausedReq.getResponseHeaders().ifPresent(resHeaders ->
resHeaders.forEach(header -> headers.add(new AbstractMap.SimpleEntry<>(header.getName(), header.getValue()))));

HttpResponse res = createHttpResponse(
pausedReq.getResponseStatusCode(),
base64Body.getBody(),
base64Body.getBase64Encoded(),
body,
bodyIsBase64Encoded,
headers);

return Either.right(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@

import java.util.concurrent.atomic.AtomicBoolean;

import static com.google.common.net.MediaType.XHTML_UTF_8;
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.openqa.selenium.remote.http.Contents.utf8String;
import static org.openqa.selenium.testing.Safely.safelyCall;
Expand All @@ -59,10 +62,19 @@ public void setup() {
assumeThat(driver).isInstanceOf(HasDevTools.class);
assumeThat(isFirefoxVersionOlderThan(87, driver)).isFalse();

appServer = new NettyAppServer(req -> new HttpResponse()
.setStatus(200)
.addHeader("Content-Type", MediaType.XHTML_UTF_8.toString())
.setContent(utf8String("<html><head><title>Hello, World!</title></head><body/></html>")));
Route route = Route.combine(
Route.matching(req -> true)
.to(() -> req -> new HttpResponse()
.setStatus(200)
.addHeader("Content-Type", XHTML_UTF_8.toString())
.setContent(utf8String("<html><head><title>Hello, World!</title></head><body/></html>"))),
Route.get("/redirect")
.to(() -> req -> new HttpResponse()
.setStatus(HTTP_MOVED_TEMP)
.setHeader("Location", "/cheese")
.setContent(Contents.utf8String("Delicious"))));

appServer = new NettyAppServer(route);
appServer.start();
}

Expand Down Expand Up @@ -159,4 +171,16 @@ public void shouldBeAbleToInterceptAResponse() {
String body = driver.findElement(By.tagName("body")).getText();
assertThat(body).contains("Sausages");
}

@Test
public void shouldHandleRedirects() {
try (NetworkInterceptor networkInterceptor = new NetworkInterceptor(
driver,
(Filter) next -> next::execute)) {
driver.get(appServer.whereIs("/redirect"));

String body = driver.findElement(By.tagName("body")).getText();
assertThat(body).contains("Hello, World!");
}
}
}

0 comments on commit 968a87a

Please sign in to comment.