Skip to content

Commit

Permalink
[grid]: Protocol conversion is generally useful.
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 19, 2019
1 parent a19a013 commit 02df06e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 84 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.grid.session.ActiveSession;
import org.openqa.selenium.grid.session.SessionFactory;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.ProtocolConverter;
import org.openqa.selenium.grid.web.ReverseProxyHandler;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.Command;
Expand Down Expand Up @@ -67,15 +70,15 @@ public abstract class RemoteSession implements ActiveSession {
private final SessionId id;
private final Dialect downstream;
private final Dialect upstream;
private final SessionCodec codec;
private final CommandHandler codec;
private final Map<String, Object> capabilities;
private final TemporaryFilesystem filesystem;
private final WebDriver driver;

protected RemoteSession(
Dialect downstream,
Dialect upstream,
SessionCodec codec,
CommandHandler codec,
SessionId id,
Map<String, Object> capabilities) {
this.downstream = downstream;
Expand Down Expand Up @@ -126,7 +129,7 @@ public WebDriver getWrappedDriver() {

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
codec.handle(req, resp);
codec.execute(req, resp);
}

public abstract static class Factory<X> implements SessionFactory {
Expand All @@ -146,11 +149,11 @@ protected Optional<ActiveSession> performHandshake(

ProtocolHandshake.Result result = new ProtocolHandshake().createSession(client, command);

SessionCodec codec;
CommandHandler codec;
Dialect upstream = result.getDialect();
Dialect downstream;
if (downstreamDialects.contains(result.getDialect())) {
codec = new Passthrough(client);
codec = new ReverseProxyHandler(client);
downstream = upstream;
} else {
downstream = downstreamDialects.isEmpty() ? OSS : downstreamDialects.iterator().next();
Expand Down Expand Up @@ -184,7 +187,7 @@ protected abstract ActiveSession newActiveSession(
X additionalData,
Dialect downstream,
Dialect upstream,
SessionCodec codec,
CommandHandler codec,
SessionId id,
Map<String, Object> capabilities);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.session.ActiveSession;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ServicedSession(
DriverService service,
Dialect downstream,
Dialect upstream,
SessionCodec codec,
CommandHandler codec,
SessionId id,
Map<String, Object> capabilities) {
super(downstream, upstream, codec, id, capabilities);
Expand Down Expand Up @@ -175,7 +176,7 @@ protected ServicedSession newActiveSession(
DriverService service,
Dialect downstream,
Dialect upstream,
SessionCodec codec,
CommandHandler codec,
SessionId id,
Map<String, Object> capabilities) {
return new ServicedSession(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.session.remote;
package org.openqa.selenium.grid.web;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;

import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.CommandCodec;
import org.openqa.selenium.remote.Response;
Expand All @@ -33,7 +34,7 @@
import java.net.URL;
import java.util.Map;

class ProtocolConverter implements SessionCodec {
public class ProtocolConverter implements CommandHandler {

private final static ImmutableSet<String> IGNORED_REQ_HEADERS = ImmutableSet.<String>builder()
.add("connection")
Expand Down Expand Up @@ -70,7 +71,7 @@ public ProtocolConverter(
}

@Override
public void handle(HttpRequest req, HttpResponse resp) throws IOException {
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Command command = downstream.decode(req);
// Massage the webelements
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.session.remote;
package org.openqa.selenium.grid.web;


import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
Expand All @@ -33,6 +33,8 @@

import org.junit.Test;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.ProtocolConverter;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.DriverCommand;
Expand All @@ -58,7 +60,7 @@ public class ProtocolConverterTest {
public void shouldRoundTripASimpleCommand() throws IOException {
SessionId sessionId = new SessionId("1234567");

SessionCodec handler = new ProtocolConverter(
CommandHandler handler = new ProtocolConverter(
new URL("http://example.com/wd/hub"),
new W3CHttpCommandCodec(),
new W3CHttpResponseCodec(),
Expand Down Expand Up @@ -90,7 +92,7 @@ protected HttpResponse makeRequest(HttpRequest request) {
HttpRequest w3cRequest = new W3CHttpCommandCodec().encode(command);

HttpResponse resp = new HttpResponse();
handler.handle(w3cRequest, resp);
handler.execute(w3cRequest, resp);

assertEquals(MediaType.JSON_UTF_8, MediaType.parse(resp.getHeader("Content-type")));
assertEquals(HttpURLConnection.HTTP_OK, resp.getStatus());
Expand All @@ -107,7 +109,7 @@ public void shouldAliasAComplexCommand() throws IOException {

// Downstream is JSON, upstream is W3C. This way we can force "isDisplayed" to become JS
// execution.
SessionCodec handler = new ProtocolConverter(
CommandHandler handler = new ProtocolConverter(
new URL("http://example.com/wd/hub"),
new JsonHttpCommandCodec(),
new JsonHttpResponseCodec(),
Expand Down Expand Up @@ -147,7 +149,7 @@ protected HttpResponse makeRequest(HttpRequest request) {
HttpRequest w3cRequest = new JsonHttpCommandCodec().encode(command);

HttpResponse resp = new HttpResponse();
handler.handle(w3cRequest, resp);
handler.execute(w3cRequest, resp);

assertEquals(MediaType.JSON_UTF_8, MediaType.parse(resp.getHeader("Content-type")));
assertEquals(HttpURLConnection.HTTP_OK, resp.getStatus());
Expand All @@ -163,7 +165,7 @@ public void shouldConvertAnException() throws IOException {
// Json upstream, w3c downstream
SessionId sessionId = new SessionId("1234567");

SessionCodec handler = new ProtocolConverter(
CommandHandler handler = new ProtocolConverter(
new URL("http://example.com/wd/hub"),
new W3CHttpCommandCodec(),
new W3CHttpResponseCodec(),
Expand Down Expand Up @@ -196,7 +198,7 @@ protected HttpResponse makeRequest(HttpRequest request) {
HttpRequest w3cRequest = new W3CHttpCommandCodec().encode(command);

HttpResponse resp = new HttpResponse();
handler.handle(w3cRequest, resp);
handler.execute(w3cRequest, resp);

assertEquals(MediaType.JSON_UTF_8, MediaType.parse(resp.getHeader("Content-type")));
assertEquals(HTTP_INTERNAL_ERROR, resp.getStatus());
Expand Down

0 comments on commit 02df06e

Please sign in to comment.