Skip to content

Commit 83e1124

Browse files
committed
Add explicit JSON serialization for Proxy.
The reflection-based serialization done by BeanToJsonConverter inserts extraneous keys (such as "class" and "hCode"), which are rejected by W3C-compliant drivers.
1 parent d0b1b81 commit 83e1124

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

java/client/src/org/openqa/selenium/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ java_library(name = 'core',
6464
'//java/client/src/org/openqa/selenium/interactions:exceptions',
6565
'//java/client/src/org/openqa/selenium/logging:api',
6666
'//java/client/src/org/openqa/selenium/security:security',
67+
'//third_party/java/gson:gson',
6768
],
6869
visibility = [
6970
'//java/client/src/org/openqa/selenium/interactions:interactions',

java/client/src/org/openqa/selenium/Proxy.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.openqa.selenium;
1919

20+
import com.google.gson.Gson;
21+
import com.google.gson.JsonElement;
22+
import java.util.HashMap;
2023
import java.util.Map;
2124

2225
/**
@@ -92,6 +95,41 @@ public Proxy(Map<String, ?> raw) {
9295
}
9396
}
9497

98+
public JsonElement toJson() {
99+
Map<String, String> m = new HashMap<>();
100+
if (proxyType != ProxyType.UNSPECIFIED) {
101+
m.put("proxyType", proxyType.toString().toLowerCase());
102+
}
103+
if (ftpProxy != null) {
104+
m.put("ftpProxy", ftpProxy);
105+
}
106+
if (httpProxy != null) {
107+
m.put("httpProxy", httpProxy);
108+
}
109+
if (noProxy != null) {
110+
m.put("noProxy", noProxy);
111+
}
112+
if (sslProxy != null) {
113+
m.put("sslProxy", sslProxy);
114+
}
115+
if (socksProxy != null) {
116+
m.put("socksProxy", socksProxy);
117+
}
118+
if (socksUsername != null) {
119+
m.put("socksUsername", socksUsername);
120+
}
121+
if (socksPassword != null) {
122+
m.put("socksPassword", socksPassword);
123+
}
124+
if (proxyAutoconfigUrl != null) {
125+
m.put("proxyAutoconfigUrl", proxyAutoconfigUrl);
126+
}
127+
if (autodetect) {
128+
m.put("autodetect", "true");
129+
}
130+
return new Gson().toJsonTree(m);
131+
}
132+
95133
/**
96134
* Gets the {@link ProxyType}. This can signal if set to use a direct connection (without proxy),
97135
* manually set proxy settings, auto-configured proxy settings, or whether to use the default

java/client/test/org/openqa/selenium/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ java_library(name = 'tests',
8181
'//java/client/src/org/openqa/selenium/support/ui:wait',
8282
'//java/client/test/org/openqa/selenium/testing:test-base',
8383
'//java/client/test/org/openqa/selenium/testing/drivers:drivers',
84+
'//third_party/java/gson:gson',
8485
'//third_party/java/guava:guava',
8586
'//third_party/java/hamcrest:hamcrest-core',
8687
'//third_party/java/hamcrest:hamcrest-library',

java/client/test/org/openqa/selenium/ProxyTest.java

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
2626

27+
import com.google.gson.JsonElement;
28+
import com.google.gson.JsonObject;
29+
import java.util.Set;
2730
import org.junit.Test;
2831
import org.junit.runner.RunWith;
2932
import org.junit.runners.JUnit4;
@@ -204,7 +207,7 @@ public void testAutodetectProxy() {
204207

205208

206209
@Test
207-
public void testInitializationManualProxy() {
210+
public void manualProxyFromMap() {
208211
Map<String, String> proxyData = new HashMap<>();
209212
proxyData.put("proxyType", "manual");
210213
proxyData.put("httpProxy", "http.proxy:1234");
@@ -231,7 +234,32 @@ public void testInitializationManualProxy() {
231234
}
232235

233236
@Test
234-
public void testInitializationPACProxy() {
237+
public void manualProxyToJson() {
238+
Proxy proxy = new Proxy();
239+
proxy.setProxyType(ProxyType.MANUAL);
240+
proxy.setHttpProxy("http.proxy:1234");
241+
proxy.setFtpProxy("ftp.proxy");
242+
proxy.setSslProxy("ssl.proxy");
243+
proxy.setNoProxy("localhost,127.0.0.*");
244+
proxy.setSocksProxy("socks.proxy:65555");
245+
proxy.setSocksUsername("test1");
246+
proxy.setSocksPassword("test2");
247+
248+
JsonObject json = proxy.toJson().getAsJsonObject();
249+
250+
assertEquals("manual", json.getAsJsonPrimitive("proxyType").getAsString());
251+
assertEquals("ftp.proxy", json.getAsJsonPrimitive("ftpProxy").getAsString());
252+
assertEquals("http.proxy:1234", json.getAsJsonPrimitive("httpProxy").getAsString());
253+
assertEquals("ssl.proxy", json.getAsJsonPrimitive("sslProxy").getAsString());
254+
assertEquals("socks.proxy:65555", json.getAsJsonPrimitive("socksProxy").getAsString());
255+
assertEquals("test1", json.getAsJsonPrimitive("socksUsername").getAsString());
256+
assertEquals("test2", json.getAsJsonPrimitive("socksPassword").getAsString());
257+
assertEquals("localhost,127.0.0.*", json.getAsJsonPrimitive("noProxy").getAsString());
258+
assertEquals(8, json.entrySet().size());
259+
}
260+
261+
@Test
262+
public void pacProxyFromMap() {
235263
Map<String, String> proxyData = new HashMap<>();
236264
proxyData.put("proxyType", "PAC");
237265
proxyData.put("proxyAutoconfigUrl", "http://aaa/bbb.pac");
@@ -252,7 +280,20 @@ public void testInitializationPACProxy() {
252280
}
253281

254282
@Test
255-
public void testInitializationAutodetectProxy() {
283+
public void pacProxyToJson() {
284+
Proxy proxy = new Proxy();
285+
proxy.setProxyType(ProxyType.PAC);
286+
proxy.setProxyAutoconfigUrl("http://aaa/bbb.pac");
287+
288+
JsonObject json = proxy.toJson().getAsJsonObject();
289+
290+
assertEquals("pac", json.getAsJsonPrimitive("proxyType").getAsString());
291+
assertEquals("http://aaa/bbb.pac", json.getAsJsonPrimitive("proxyAutoconfigUrl").getAsString());
292+
assertEquals(2, json.entrySet().size());
293+
}
294+
295+
@Test
296+
public void autodetectProxyFromMap() {
256297
Map<String, Object> proxyData = new HashMap<>();
257298
proxyData.put("proxyType", "AUTODETECT");
258299
proxyData.put("autodetect", true);
@@ -273,7 +314,20 @@ public void testInitializationAutodetectProxy() {
273314
}
274315

275316
@Test
276-
public void testInitializationSystemProxy() {
317+
public void autodetectProxyToJson() {
318+
Proxy proxy = new Proxy();
319+
proxy.setProxyType(ProxyType.AUTODETECT);
320+
proxy.setAutodetect(true);
321+
322+
JsonObject json = proxy.toJson().getAsJsonObject();
323+
324+
assertEquals("autodetect", json.getAsJsonPrimitive("proxyType").getAsString());
325+
assertTrue(json.getAsJsonPrimitive("autodetect").getAsBoolean());
326+
assertEquals(2, json.entrySet().size());
327+
}
328+
329+
@Test
330+
public void systemProxyFromMap() {
277331
Map<String, String> proxyData = new HashMap<>();
278332
proxyData.put("proxyType", "system");
279333

@@ -293,7 +347,18 @@ public void testInitializationSystemProxy() {
293347
}
294348

295349
@Test
296-
public void testInitializationDirectProxy() {
350+
public void systemProxyToJson() {
351+
Proxy proxy = new Proxy();
352+
proxy.setProxyType(ProxyType.SYSTEM);
353+
354+
JsonObject json = proxy.toJson().getAsJsonObject();
355+
356+
assertEquals("system", json.getAsJsonPrimitive("proxyType").getAsString());
357+
assertEquals(1, json.entrySet().size());
358+
}
359+
360+
@Test
361+
public void directProxyFromMap() {
297362
Map<String, String> proxyData = new HashMap<>();
298363
proxyData.put("proxyType", "DIRECT");
299364

@@ -312,6 +377,17 @@ public void testInitializationDirectProxy() {
312377
assertNull(proxy.getProxyAutoconfigUrl());
313378
}
314379

380+
@Test
381+
public void directProxyToJson() {
382+
Proxy proxy = new Proxy();
383+
proxy.setProxyType(ProxyType.DIRECT);
384+
385+
JsonObject json = proxy.toJson().getAsJsonObject();
386+
387+
assertEquals("direct", json.getAsJsonPrimitive("proxyType").getAsString());
388+
assertEquals(1, json.entrySet().size());
389+
}
390+
315391
@Test
316392
public void constructingWithNullKeysWorksAsExpected() {
317393
Map<String, String> rawProxy = new HashMap<>();

0 commit comments

Comments
 (0)