Path: blob/trunk/java/test/org/openqa/selenium/ProxyTest.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.assertj.core.api.Assertions.assertThatExceptionOfType;21import static org.openqa.selenium.Proxy.ProxyType.AUTODETECT;22import static org.openqa.selenium.Proxy.ProxyType.DIRECT;23import static org.openqa.selenium.Proxy.ProxyType.MANUAL;24import static org.openqa.selenium.Proxy.ProxyType.PAC;25import static org.openqa.selenium.Proxy.ProxyType.SYSTEM;26import static org.openqa.selenium.Proxy.ProxyType.UNSPECIFIED;27import static org.openqa.selenium.remote.CapabilityType.PROXY;2829import java.util.Arrays;30import java.util.HashMap;31import java.util.Map;32import org.junit.jupiter.api.Disabled;33import org.junit.jupiter.api.Tag;34import org.junit.jupiter.api.Test;35import org.openqa.selenium.Proxy.ProxyType;36import org.openqa.selenium.json.Json;3738@Tag("UnitTests")39class ProxyTest {4041@Test42void testNotInitializedProxy() {43Proxy proxy = new Proxy();4445assertThat(proxy.getProxyType()).isEqualTo(UNSPECIFIED);4647assertThat(proxy.getFtpProxy()).isNull();48assertThat(proxy.getHttpProxy()).isNull();49assertThat(proxy.getSslProxy()).isNull();50assertThat(proxy.getSocksProxy()).isNull();51assertThat(proxy.getSocksVersion()).isNull();52assertThat(proxy.getSocksUsername()).isNull();53assertThat(proxy.getSocksPassword()).isNull();54assertThat(proxy.getNoProxy()).isNull();55assertThat(proxy.getProxyAutoconfigUrl()).isNull();56assertThat(proxy.isAutodetect()).isFalse();57}5859@Test60void testCanNotChangeAlreadyInitializedProxyType() {61final Proxy proxy = new Proxy();62proxy.setProxyType(DIRECT);6364assertThatExceptionOfType(IllegalStateException.class)65.isThrownBy(() -> proxy.setAutodetect(true));6667assertThatExceptionOfType(IllegalStateException.class)68.isThrownBy(() -> proxy.setSocksPassword(""));6970assertThatExceptionOfType(IllegalStateException.class)71.isThrownBy(() -> proxy.setSocksUsername(""));7273assertThatExceptionOfType(IllegalStateException.class)74.isThrownBy(() -> proxy.setSocksProxy(""));7576assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> proxy.setFtpProxy(""));7778assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> proxy.setHttpProxy(""));7980assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> proxy.setNoProxy(""));8182assertThatExceptionOfType(IllegalStateException.class)83.isThrownBy(() -> proxy.setProxyAutoconfigUrl(""));8485assertThatExceptionOfType(IllegalStateException.class)86.isThrownBy(() -> proxy.setProxyType(SYSTEM));8788assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> proxy.setSslProxy(""));8990final Proxy proxy2 = new Proxy();91proxy2.setProxyType(AUTODETECT);9293assertThatExceptionOfType(IllegalStateException.class)94.isThrownBy(() -> proxy2.setProxyType(SYSTEM));9596assertThatExceptionOfType(IllegalStateException.class)97.isThrownBy(() -> proxy.setSocksVersion(5));98}99100@Test101void testManualProxy() {102Proxy proxy = new Proxy();103104proxy105.setHttpProxy("http.proxy:1234")106.setFtpProxy("ftp.proxy")107.setSslProxy("ssl.proxy")108.setNoProxy("localhost,127.0.0.*")109.setSocksProxy("socks.proxy:65555")110.setSocksVersion(5)111.setSocksUsername("test1")112.setSocksPassword("test2");113114assertThat(proxy.getProxyType()).isEqualTo(MANUAL);115assertThat(proxy.getFtpProxy()).isEqualTo("ftp.proxy");116assertThat(proxy.getHttpProxy()).isEqualTo("http.proxy:1234");117assertThat(proxy.getSslProxy()).isEqualTo("ssl.proxy");118assertThat(proxy.getSocksProxy()).isEqualTo("socks.proxy:65555");119assertThat(proxy.getSocksVersion()).isEqualTo(5);120assertThat(proxy.getSocksUsername()).isEqualTo("test1");121assertThat(proxy.getSocksPassword()).isEqualTo("test2");122assertThat(proxy.getNoProxy()).isEqualTo("localhost,127.0.0.*");123124assertThat(proxy.getProxyAutoconfigUrl()).isNull();125assertThat(proxy.isAutodetect()).isFalse();126}127128@Test129void testPACProxy() {130Proxy proxy = new Proxy();131proxy.setProxyAutoconfigUrl("http://aaa/bbb.pac");132133assertThat(proxy.getProxyType()).isEqualTo(PAC);134assertThat(proxy.getProxyAutoconfigUrl()).isEqualTo("http://aaa/bbb.pac");135136assertThat(proxy.getFtpProxy()).isNull();137assertThat(proxy.getHttpProxy()).isNull();138assertThat(proxy.getSslProxy()).isNull();139assertThat(proxy.getSocksProxy()).isNull();140assertThat(proxy.getSocksVersion()).isNull();141assertThat(proxy.getSocksUsername()).isNull();142assertThat(proxy.getSocksPassword()).isNull();143assertThat(proxy.getNoProxy()).isNull();144assertThat(proxy.isAutodetect()).isFalse();145}146147@Test148void testAutodetectProxy() {149Proxy proxy = new Proxy();150proxy.setAutodetect(true);151152assertThat(proxy.getProxyType().name()).isEqualTo(AUTODETECT.name());153assertThat(proxy.isAutodetect()).isTrue();154155assertThat(proxy.getFtpProxy()).isNull();156assertThat(proxy.getHttpProxy()).isNull();157assertThat(proxy.getSslProxy()).isNull();158assertThat(proxy.getSocksProxy()).isNull();159assertThat(proxy.getSocksVersion()).isNull();160assertThat(proxy.getSocksUsername()).isNull();161assertThat(proxy.getSocksPassword()).isNull();162assertThat(proxy.getNoProxy()).isNull();163assertThat(proxy.getProxyAutoconfigUrl()).isNull();164}165166@Test167void manualProxyFromMap() {168Map<String, Object> proxyData = new HashMap<>();169proxyData.put("proxyType", "manual");170proxyData.put("httpProxy", "http.proxy:1234");171proxyData.put("ftpProxy", "ftp.proxy");172proxyData.put("sslProxy", "ssl.proxy");173proxyData.put("noProxy", "localhost,127.0.0.*");174proxyData.put("socksProxy", "socks.proxy:65555");175proxyData.put("socksVersion", 5);176proxyData.put("socksUsername", "test1");177proxyData.put("socksPassword", "test2");178179Proxy proxy = new Proxy(proxyData);180181assertThat(proxy.getProxyType()).isEqualTo(MANUAL);182assertThat(proxy.getFtpProxy()).isEqualTo("ftp.proxy");183assertThat(proxy.getHttpProxy()).isEqualTo("http.proxy:1234");184assertThat(proxy.getSslProxy()).isEqualTo("ssl.proxy");185assertThat(proxy.getSocksProxy()).isEqualTo("socks.proxy:65555");186assertThat(proxy.getSocksVersion()).isEqualTo(5);187assertThat(proxy.getSocksUsername()).isEqualTo("test1");188assertThat(proxy.getSocksPassword()).isEqualTo("test2");189assertThat(proxy.getNoProxy()).isEqualTo("localhost,127.0.0.*");190191assertThat(proxy.getProxyAutoconfigUrl()).isNull();192assertThat(proxy.isAutodetect()).isFalse();193}194195@Test196void longSocksVersionFromMap() {197Map<String, Object> proxyData = new HashMap<>();198long l = 5;199proxyData.put("proxyType", "manual");200proxyData.put("httpProxy", "http.proxy:1234");201proxyData.put("ftpProxy", "ftp.proxy");202proxyData.put("sslProxy", "ssl.proxy");203proxyData.put("noProxy", "localhost,127.0.0.*");204proxyData.put("socksProxy", "socks.proxy:65555");205proxyData.put("socksVersion", l);206proxyData.put("socksUsername", "test1");207proxyData.put("socksPassword", "test2");208209Proxy proxy = new Proxy(proxyData);210211assertThat(proxy.getSocksVersion()).isEqualTo(5);212}213214@Test215void manualProxyToJson() {216Proxy proxy = new Proxy();217proxy.setProxyType(ProxyType.MANUAL);218proxy.setHttpProxy("http.proxy:1234");219proxy.setFtpProxy("ftp.proxy");220proxy.setSslProxy("ssl.proxy");221proxy.setNoProxy("localhost,127.0.0.*");222proxy.setSocksProxy("socks.proxy:65555");223proxy.setSocksVersion(5);224proxy.setSocksUsername("test1");225proxy.setSocksPassword("test2");226227Map<String, Object> json = proxy.toJson();228229assertThat(json.get("proxyType")).isEqualTo("manual");230assertThat(json.get("ftpProxy")).isEqualTo("ftp.proxy");231assertThat(json.get("httpProxy")).isEqualTo("http.proxy:1234");232assertThat(json.get("sslProxy")).isEqualTo("ssl.proxy");233assertThat(json.get("socksProxy")).isEqualTo("socks.proxy:65555");234assertThat(json.get("socksVersion")).isEqualTo(5);235assertThat(json.get("socksUsername")).isEqualTo("test1");236assertThat(json.get("socksPassword")).isEqualTo("test2");237assertThat(json.get("noProxy")).isEqualTo(Arrays.asList("localhost", "127.0.0.*"));238assertThat(json.entrySet()).hasSize(9);239}240241@Test242void pacProxyFromMap() {243Map<String, String> proxyData = new HashMap<>();244proxyData.put("proxyType", "PAC");245proxyData.put("proxyAutoconfigUrl", "http://aaa/bbb.pac");246247Proxy proxy = new Proxy(proxyData);248249assertThat(proxy.getProxyType()).isEqualTo(PAC);250assertThat(proxy.getProxyAutoconfigUrl()).isEqualTo("http://aaa/bbb.pac");251252assertThat(proxy.getFtpProxy()).isNull();253assertThat(proxy.getHttpProxy()).isNull();254assertThat(proxy.getSslProxy()).isNull();255assertThat(proxy.getSocksProxy()).isNull();256assertThat(proxy.getSocksVersion()).isNull();257assertThat(proxy.getSocksUsername()).isNull();258assertThat(proxy.getSocksPassword()).isNull();259assertThat(proxy.getNoProxy()).isNull();260assertThat(proxy.isAutodetect()).isFalse();261}262263@Test264void pacProxyToJson() {265Proxy proxy = new Proxy();266proxy.setProxyType(ProxyType.PAC);267proxy.setProxyAutoconfigUrl("http://aaa/bbb.pac");268269Map<String, Object> json = proxy.toJson();270271assertThat(json.get("proxyType")).isEqualTo("pac");272assertThat(json.get("proxyAutoconfigUrl")).isEqualTo("http://aaa/bbb.pac");273assertThat(json.entrySet()).hasSize(2);274}275276@Test277void autodetectProxyFromMap() {278Map<String, Object> proxyData = new HashMap<>();279proxyData.put("proxyType", "AUTODETECT");280proxyData.put("autodetect", true);281282Proxy proxy = new Proxy(proxyData);283284assertThat(proxy.getProxyType()).isEqualTo(AUTODETECT);285assertThat(proxy.isAutodetect()).isTrue();286287assertThat(proxy.getFtpProxy()).isNull();288assertThat(proxy.getHttpProxy()).isNull();289assertThat(proxy.getSslProxy()).isNull();290assertThat(proxy.getSocksProxy()).isNull();291assertThat(proxy.getSocksVersion()).isNull();292assertThat(proxy.getSocksUsername()).isNull();293assertThat(proxy.getSocksPassword()).isNull();294assertThat(proxy.getNoProxy()).isNull();295assertThat(proxy.getProxyAutoconfigUrl()).isNull();296}297298@Test299void autodetectProxyToJson() {300Proxy proxy = new Proxy();301proxy.setProxyType(ProxyType.AUTODETECT);302proxy.setAutodetect(true);303304Map<String, ?> json = proxy.toJson();305306assertThat(json.get("proxyType")).isEqualTo("autodetect");307assertThat((Boolean) json.get("autodetect")).isTrue();308assertThat(json.entrySet()).hasSize(2);309}310311@Test312void systemProxyFromMap() {313Map<String, String> proxyData = new HashMap<>();314proxyData.put("proxyType", "system");315316Proxy proxy = new Proxy(proxyData);317318assertThat(proxy.getProxyType()).isEqualTo(SYSTEM);319320assertThat(proxy.getFtpProxy()).isNull();321assertThat(proxy.getHttpProxy()).isNull();322assertThat(proxy.getSslProxy()).isNull();323assertThat(proxy.getSocksProxy()).isNull();324assertThat(proxy.getSocksVersion()).isNull();325assertThat(proxy.getSocksUsername()).isNull();326assertThat(proxy.getSocksPassword()).isNull();327assertThat(proxy.getNoProxy()).isNull();328assertThat(proxy.isAutodetect()).isFalse();329assertThat(proxy.getProxyAutoconfigUrl()).isNull();330}331332@Test333void systemProxyToJson() {334Proxy proxy = new Proxy();335proxy.setProxyType(ProxyType.SYSTEM);336337Map<String, Object> json = proxy.toJson();338339assertThat(json.get("proxyType")).isEqualTo("system");340assertThat(json.entrySet()).hasSize(1);341}342343@Test344void directProxyFromMap() {345Map<String, String> proxyData = new HashMap<>();346proxyData.put("proxyType", "DIRECT");347348Proxy proxy = new Proxy(proxyData);349350assertThat(proxy.getProxyType()).isEqualTo(DIRECT);351352assertThat(proxy.getFtpProxy()).isNull();353assertThat(proxy.getHttpProxy()).isNull();354assertThat(proxy.getSslProxy()).isNull();355assertThat(proxy.getSocksProxy()).isNull();356assertThat(proxy.getSocksVersion()).isNull();357assertThat(proxy.getSocksUsername()).isNull();358assertThat(proxy.getSocksPassword()).isNull();359assertThat(proxy.getNoProxy()).isNull();360assertThat(proxy.isAutodetect()).isFalse();361assertThat(proxy.getProxyAutoconfigUrl()).isNull();362}363364@Test365void directProxyToJson() {366Proxy proxy = new Proxy();367proxy.setProxyType(ProxyType.DIRECT);368369Map<String, Object> json = proxy.toJson();370371assertThat(json.get("proxyType")).isEqualTo("direct");372assertThat(json.entrySet()).hasSize(1);373}374375@Test376void constructingWithNullKeysWorksAsExpected() {377Map<String, String> rawProxy = new HashMap<>();378rawProxy.put("ftpProxy", null);379rawProxy.put("httpProxy", "http://www.example.com");380rawProxy.put("autodetect", null);381Capabilities caps = new ImmutableCapabilities(PROXY, rawProxy);382383Proxy proxy = Proxy.extractFrom(caps);384385assertThat(proxy.getFtpProxy()).isNull();386assertThat(proxy.isAutodetect()).isFalse();387assertThat(proxy.getHttpProxy()).isEqualTo("http://www.example.com");388}389390@Test391@Disabled392public void serializesAndDeserializesWithoutError() {393Proxy proxy = new Proxy();394proxy.setProxyAutoconfigUrl("http://www.example.com/config.pac");395396Capabilities caps = new ImmutableCapabilities(PROXY, proxy);397398String rawJson = new Json().toJson(caps);399Capabilities converted = new Json().toType(rawJson, Capabilities.class);400401Object returnedProxy = converted.getCapability(PROXY);402assertThat(returnedProxy).isInstanceOf(Proxy.class);403}404}405406407