-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CDP] Provide an in-process proxy for stubbing responses
This makes use of the CDP to provide a lightweight mechanism for intercepting requests from the browser to the server, allowing them to be overridden if necessary. In order to do this, the user needs to be provide a `Route` from our own HTTP abstractions. If the route matches, then it is called to figure out what to return to the browser. This allows tests to stub out backends if they see this as being necessary.
- Loading branch information
Showing
23 changed files
with
730 additions
and
17 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
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
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
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
76 changes: 76 additions & 0 deletions
76
java/client/src/org/openqa/selenium/devtools/fetch/Fetch.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,76 @@ | ||
package org.openqa.selenium.devtools.fetch; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.openqa.selenium.devtools.Command; | ||
import org.openqa.selenium.devtools.Event; | ||
import org.openqa.selenium.devtools.fetch.model.HeaderEntry; | ||
import org.openqa.selenium.devtools.fetch.model.RequestId; | ||
import org.openqa.selenium.devtools.fetch.model.RequestPattern; | ||
import org.openqa.selenium.devtools.fetch.model.RequestPaused; | ||
import org.openqa.selenium.devtools.network.model.ErrorReason; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class Fetch { | ||
|
||
public static Command<Void> disable() { | ||
return new Command<>("Fetch.disable", ImmutableMap.of()); | ||
} | ||
|
||
public static Command<Void> enable( | ||
Optional<List<RequestPattern>> requestPatterns, | ||
Optional<Boolean> handleAuthRequests) { | ||
|
||
ImmutableMap.Builder<String, Object> args = ImmutableMap.builder(); | ||
requestPatterns.ifPresent(patterns -> args.put("patterns", patterns)); | ||
handleAuthRequests.ifPresent(authRequests -> args.put("handleAuthRequests", authRequests)); | ||
|
||
return new Command<>("Fetch.enable", args.build()); | ||
} | ||
|
||
public static Command<Void> failRequest(RequestId requestId, ErrorReason errorReason) { | ||
return new Command<Void>( | ||
"Fetch.failRequest", | ||
ImmutableMap.of("requestId", requestId, "errorReason", errorReason)) | ||
.doesNotSendResponse(); | ||
} | ||
|
||
public static Command<Void> fulfillRequest( | ||
RequestId requestId, | ||
int responseCode, | ||
List<HeaderEntry> responseHeaders, | ||
Optional<String> body, | ||
Optional<String> responsePhrase) { | ||
|
||
ImmutableMap.Builder<String, Object> args = ImmutableMap.builder(); | ||
args.put("requestId", requestId); | ||
args.put("responseCode", responseCode); | ||
args.put("responseHeaders", responseHeaders); | ||
body.ifPresent(text -> args.put("body", text)); | ||
responsePhrase.ifPresent(phrase -> args.put("responsePhrase", phrase)); | ||
|
||
return new Command<Void>("Fetch.fulfillRequest", args.build()).doesNotSendResponse(); | ||
} | ||
|
||
public static Command<Void> continueRequest( | ||
RequestId requestId, | ||
Optional<String> url, | ||
Optional<String> method, | ||
Optional<String> postData, | ||
Optional<List<HeaderEntry>> headers) { | ||
|
||
ImmutableMap.Builder<String, Object> args = ImmutableMap.builder(); | ||
args.put("requestId", requestId); | ||
url.ifPresent(u -> args.put("url", u)); | ||
method.ifPresent(m -> args.put("method", m)); | ||
postData.ifPresent(data -> args.put("postData", data)); | ||
headers.ifPresent(h -> args.put("headers", headers)); | ||
|
||
return new Command<Void>("Fetch.continueRequest", args.build()).doesNotSendResponse(); | ||
} | ||
|
||
public static Event<RequestPaused> requestPaused() { | ||
return new Event<>("Fetch.requestPaused", input -> input.read(RequestPaused.class)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
java/client/src/org/openqa/selenium/devtools/fetch/model/HeaderEntry.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,55 @@ | ||
package org.openqa.selenium.devtools.fetch.model; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.openqa.selenium.json.JsonInput; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class HeaderEntry { | ||
|
||
private final String name; | ||
private final String value; | ||
|
||
public HeaderEntry(String name, String value) { | ||
this.name = Objects.requireNonNull(name); | ||
this.value = Objects.requireNonNull(value); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
private static HeaderEntry fromJson(JsonInput input) { | ||
String name = null; | ||
String value = null; | ||
|
||
input.beginObject(); | ||
while (input.hasNext()) { | ||
switch (input.nextName()) { | ||
case "name": | ||
name = input.nextString(); | ||
break; | ||
|
||
case "value": | ||
value = input.nextString(); | ||
break; | ||
|
||
default: | ||
input.skipValue(); | ||
break; | ||
} | ||
} | ||
input.endObject(); | ||
|
||
return new HeaderEntry(name, value); | ||
} | ||
|
||
private Map<String, String> toJson() { | ||
return ImmutableMap.of("name", name, "value", value); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
java/client/src/org/openqa/selenium/devtools/fetch/model/RequestId.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,20 @@ | ||
package org.openqa.selenium.devtools.fetch.model; | ||
|
||
import java.util.Objects; | ||
|
||
public class RequestId { | ||
|
||
private final String id; | ||
|
||
private RequestId(String id) { | ||
this.id = Objects.requireNonNull(id); | ||
} | ||
|
||
private static RequestId fromJson(String id) { | ||
return new RequestId(id); | ||
} | ||
|
||
private String toJson() { | ||
return id; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
java/client/src/org/openqa/selenium/devtools/fetch/model/RequestPattern.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,76 @@ | ||
package org.openqa.selenium.devtools.fetch.model; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.openqa.selenium.devtools.network.model.ResourceType; | ||
import org.openqa.selenium.json.JsonInput; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class RequestPattern { | ||
|
||
private final Optional<String> urlPattern; | ||
private final Optional<ResourceType> resourceType; | ||
private final Optional<RequestStage> requestStage; | ||
|
||
public RequestPattern( | ||
Optional<String> urlPattern, | ||
Optional<ResourceType> resourceType, | ||
Optional<RequestStage> requestStage) { | ||
|
||
this.urlPattern = Objects.requireNonNull(urlPattern); | ||
this.resourceType = Objects.requireNonNull(resourceType); | ||
this.requestStage = Objects.requireNonNull(requestStage); | ||
} | ||
|
||
public Optional<String> getUrlPattern() { | ||
return urlPattern; | ||
} | ||
|
||
public Optional<ResourceType> getResourceType() { | ||
return resourceType; | ||
} | ||
|
||
public Optional<RequestStage> getRequestStage() { | ||
return requestStage; | ||
} | ||
|
||
private Map<String, Object> toJson() { | ||
ImmutableMap.Builder<String, Object> blob = ImmutableMap.builder(); | ||
urlPattern.ifPresent(pattern -> blob.put("urlPattern", pattern)); | ||
resourceType.ifPresent(type -> blob.put("resourceType", type)); | ||
requestStage.ifPresent(stage -> blob.put("requestStage", stage)); | ||
return blob.build(); | ||
} | ||
|
||
private static RequestPattern fromJson(JsonInput input) { | ||
Optional<String> urlPattern = Optional.empty(); | ||
Optional<ResourceType> resourceType = Optional.empty(); | ||
Optional<RequestStage> requestStage = Optional.empty(); | ||
|
||
input.beginObject(); | ||
while (input.hasNext()) { | ||
switch (input.nextName()) { | ||
case "urlPattern": | ||
urlPattern = Optional.of(input.nextString()); | ||
break; | ||
|
||
case "resourceType": | ||
resourceType = Optional.of(input.read(ResourceType.class)); | ||
break; | ||
|
||
case "requestStage": | ||
requestStage = Optional.of(input.read(RequestStage.class)); | ||
break; | ||
|
||
default: | ||
input.skipValue(); | ||
break; | ||
} | ||
} | ||
input.endObject(); | ||
|
||
return new RequestPattern(urlPattern, resourceType, requestStage); | ||
} | ||
} |
Oops, something went wrong.