Skip to content

Commit

Permalink
[grid]: Add failing test for JWP support
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 19, 2019
1 parent abffeac commit b1747ac
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
1 change: 1 addition & 0 deletions java/server/test/org/openqa/selenium/grid/router/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_test(
"//java/server/src/org/openqa/selenium/grid/web:web",
"//java/server/src/org/openqa/selenium/injector:injector",
"//java/server/test/org/openqa/selenium/grid/web:utils",
"//third_party/java/assertj:assertj",
"//third_party/java/guava:guava",
"//third_party/java/junit:junit",
],
Expand Down
114 changes: 104 additions & 10 deletions java/server/test/org/openqa/selenium/grid/router/EndToEndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@

package org.openqa.selenium.grid.router;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.time.Duration.ofSeconds;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.POST;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -53,6 +61,7 @@
import org.openqa.selenium.grid.web.RoutableHttpClientFactory;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.grid.web.Values;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
Expand All @@ -71,25 +80,48 @@
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;

@RunWith(Parameterized.class)
public class EndToEndTest {

private static final Capabilities CAPS = new ImmutableCapabilities("browserName", "cheese");
private Json json = new Json();

@Parameterized.Parameters(name = "End to End {0}")
public static Collection<Object[]> buildTracers()
throws MalformedURLException, URISyntaxException {
return ImmutableSet.of(createInMemory(), createRemotes());
public static Collection<Supplier<Object[]>> buildGrids() {
return ImmutableSet.of(
() -> {
try {
return createInMemory();
} catch (Exception e) {
throw new RuntimeException(e);
}
},
() -> {
try {
return createRemotes();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
});
}

@Parameter(value = 0)
public Server<?> server;
@Parameter
public Supplier<Object[]> values;

@Parameter(value = 1)
public HttpClient.Factory clientFactory;
private Server<?> server;

public static Object[] createInMemory() throws URISyntaxException, MalformedURLException {
private HttpClient.Factory clientFactory;

@Before
public void setFields() {
Object[] raw = values.get();
this.server = (Server<?>) raw[0];
this.clientFactory = (HttpClient.Factory) raw[1];
}

private static Object[] createInMemory() throws URISyntaxException, MalformedURLException {
EventBus bus = ZeroMqEventBus.create(
new ZContext(),
"inproc://end-to-end-pub",
Expand Down Expand Up @@ -125,7 +157,7 @@ public static Object[] createInMemory() throws URISyntaxException, MalformedURLE
return new Object[] { server, clientFactory };
}

public static Object[] createRemotes() throws URISyntaxException {
private static Object[] createRemotes() throws URISyntaxException {
EventBus bus = ZeroMqEventBus.create(
new ZContext(),
"tcp://localhost:" + PortProber.findFreePort(),
Expand Down Expand Up @@ -210,7 +242,7 @@ public void execute(HttpRequest req, HttpResponse resp) {
}
}

return caps -> new SpoofSession(caps);
return SpoofSession::new;
}

@Test
Expand Down Expand Up @@ -249,4 +281,66 @@ public void exerciseDriver() {
driver.get("http://www.google.com");
driver.quit();
}

@Test
public void shouldAllowPassthroughForW3CMode() throws IOException {
HttpRequest request = new HttpRequest(POST, "/session");
request.setContent(json.toJson(
ImmutableMap.of(
"capabilities", ImmutableMap.of(
"alwaysMatch", ImmutableMap.of("browserName", "cheese")))).getBytes(UTF_8));

HttpClient client = clientFactory.createClient(server.getUrl());
HttpResponse response = client.execute(request);

assertEquals(200, response.getStatus());

Map<String, Object> topLevel = json.toType(response.getContentString(), MAP_TYPE);

// There should not be a numeric status field
assertFalse(request.getContentString(), topLevel.containsKey("status"));

// And the value should have all the good stuff in it: the session id and the capabilities
Map<?, ?> value = (Map<?, ?>) topLevel.get("value");
assertThat(value.get("sessionId")).isInstanceOf(String.class);

Map<?, ?> caps = (Map<?, ?>) value.get("capabilities");
assertEquals("cheese", caps.get("browserName"));
}

@Test
@Ignore("The new grid doesn't handle passthrough yet")
public void shouldAllowPassthroughForJWPMode() throws IOException {
HttpRequest request = new HttpRequest(POST, "/session");
request.setContent(json.toJson(
ImmutableMap.of(
"desiredCapabilities", ImmutableMap.of(
"browserName", "cheese"))).getBytes(UTF_8));

HttpClient client = clientFactory.createClient(server.getUrl());
HttpResponse response = client.execute(request);

assertEquals(200, response.getStatus());

Map<String, Object> topLevel = json.toType(response.getContentString(), MAP_TYPE);

// There should be a numeric status field
assertEquals(request.getContentString(), topLevel.get("status"));
// The session id
assertTrue(request.getContentString(), topLevel.containsKey("sessionId"));

// And the value should be the capabilities.
Map<?, ?> value = (Map<?, ?>) topLevel.get("value");
assertEquals(request.getContentString(), "cheese", value.get("browserName"));
}

@Test
public void shouldDoProtocolTranslationFromW3CLocalEndToJWPRemoteEnd() {

}

@Test
public void shouldDoProtocolTranslationFromJWPLocalEndToW3CRemoteEnd() {

}
}

0 comments on commit b1747ac

Please sign in to comment.