1 30 31 package org.apache.commons.httpclient; 32 33 import junit.framework.Test; 34 import junit.framework.TestCase; 35 import junit.framework.TestSuite; 36 37 import org.apache.commons.httpclient.methods.GetMethod; 38 39 53 public class TestHttps extends TestCase { 54 55 private String _urlWithPort = null; 57 private String _urlWithoutPort = null; 58 private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost"); 59 private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort"); 60 private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser"); 61 private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass"); 62 63 public TestHttps(String testName) { 65 super(testName); 66 } 67 68 public static void main(String args[]) { 70 String [] testCaseName = { TestHttps.class.getName() }; 71 junit.textui.TestRunner.main(testCaseName); 72 } 73 74 public static Test suite() { 76 return new TestSuite(TestHttps.class); 77 } 78 79 public void setUp() throws Exception { 80 _urlWithPort = "https://www.verisign.com:443/"; 81 _urlWithoutPort = "https://www.verisign.com/"; 82 } 83 84 public void testHttpsGet() { 85 HttpClient client = new HttpClient(); 86 if (PROXY_HOST != null) { 87 if (PROXY_USER != null) { 88 HttpState state = client.getState(); 89 state.setProxyCredentials(null, new UsernamePasswordCredentials( 90 PROXY_USER, PROXY_PASS)); 91 } 92 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT)); 93 } 94 GetMethod method = new GetMethod(_urlWithPort); 95 96 try { 97 client.executeMethod(method); 98 } catch (Throwable t) { 99 t.printStackTrace(); 100 fail("Exception thrown during HTTPS GET: " + t.toString()); 101 } 102 103 try { 104 String data = method.getResponseBodyAsString(); 105 assertTrue("No data returned.", (data.length() > 0)); 107 } catch (Throwable t) { 108 t.printStackTrace(); 109 fail("Exception thrown while retrieving data : " + t.toString()); 110 } 111 } 112 113 public void testHttpsGetNoPort() { 114 HttpClient client = new HttpClient(); 115 if (PROXY_HOST != null) { 116 if (PROXY_USER != null) { 117 HttpState state = client.getState(); 118 state.setProxyCredentials(null, new UsernamePasswordCredentials( 119 PROXY_USER, PROXY_PASS)); 120 } 121 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT)); 122 } 123 GetMethod method = new GetMethod(_urlWithoutPort); 124 125 try { 126 client.executeMethod(method); 127 } catch (Throwable t) { 128 t.printStackTrace(); 129 fail("Exception thrown during HTTPS GET: " + t.toString()); 130 } 131 132 try { 133 String data = method.getResponseBodyAsString(); 134 assertTrue("No data returned.", (data.length() > 0)); 136 } catch (Throwable t) { 137 t.printStackTrace(); 138 fail("Exception thrown while retrieving data : " + t.toString()); 139 } 140 } 141 } 142 | Popular Tags |