Skip to content

Commit

Permalink
Only lowercase proxy type in W3C options
Browse files Browse the repository at this point in the history
The W3C spec says that the `proxyType` field of
`proxy` must be in lower-case. The original
solution of lower-casing the field blindly causes
things like the XPI-based FirefoxDriver and,
potentially, other implementations to not
recognise the proxy type.

Rather than doing things like this, we just
change the value for the W3C route, and leave it
as it was in the non-W3C codepath.

Checking the implementations of XPI FirefoxDriver,
IEDriverServer, ChromeDriver, and GeckoDriver, I
think that this change is good.
  • Loading branch information
shs96c committed May 4, 2017
1 parent cf2870f commit f8976be
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion java/client/src/org/openqa/selenium/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Map<String, Object> toJson() {
Map<String, Object> m = new HashMap<>();

if (proxyType != ProxyType.UNSPECIFIED) {
m.put("proxyType", proxyType.toString().toLowerCase());
m.put("proxyType", proxyType.toString());
}
if (ftpProxy != null) {
m.put("ftpProxy", ftpProxy);
Expand Down
14 changes: 14 additions & 0 deletions java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;

Expand Down Expand Up @@ -68,6 +69,7 @@
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class ProtocolHandshake {

Expand Down Expand Up @@ -324,6 +326,18 @@ private void streamW3CProtocolParameters(
));

// TODO(simon): transform some capabilities that changed in the spec (timeout's "pageLoad")
Stream.concat(Stream.of(alwaysMatch), StreamSupport.stream(firstMatch.spliterator(), false))
.map(el -> (JsonObject) el)
.forEach(obj -> {
if (obj.has("proxy")) {
JsonObject proxy = obj.getAsJsonObject("proxy");
if (proxy.has("proxyType")) {
proxy.add(
"proxyType",
new JsonPrimitive(proxy.get("proxyType").getAsString().toLowerCase()));
}
}
});

out.name("alwaysMatch");
gson.toJson(alwaysMatch, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.openqa.selenium.Proxy.ProxyType.AUTODETECT;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand All @@ -31,6 +32,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
Expand Down Expand Up @@ -236,6 +238,42 @@ public void shouldNotIncludeNonProtocolExtensionKeys() throws IOException {
assertFalse(keys.contains("options"));
}

@Test
public void shouldLowerCaseProxyTypeForW3CRequest() throws IOException {
DesiredCapabilities caps = new DesiredCapabilities();
Proxy proxy = new Proxy();
proxy.setProxyType(AUTODETECT);
caps.setCapability(CapabilityType.PROXY, proxy);
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", caps);
Command command = new Command(null, DriverCommand.NEW_SESSION, params);

HttpResponse response = new HttpResponse();
response.setStatus(HTTP_OK);
response.setContent(
"{\"sessionId\": \"23456789\", \"status\": 0, \"value\": {}}".getBytes(UTF_8));
RecordingHttpClient client = new RecordingHttpClient(response);

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());

Object rawCaps = handshakeRequest.get("capabilities");
assertTrue(rawCaps instanceof Map);

Map<?, ?> capabilities = (Map<?, ?>) rawCaps;

Map<String, ?> always = (Map<String, ?>) capabilities.get("alwaysMatch");
Map<String, ?> seenProxy = (Map<String, ?>) always.get("proxy");
assertEquals("autodetect", seenProxy.get("proxyType"));

Map<String, ?> jsonCaps = (Map<String, ?>) capabilities.get("desiredCapabilities");
seenProxy = (Map<String, ?>) jsonCaps.get("proxy");
assertEquals("AUTODETECT", seenProxy.get("proxyType"));
}

class RecordingHttpClient implements HttpClient {

private final HttpResponse response;
Expand Down

0 comments on commit f8976be

Please sign in to comment.