1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.auth.AuthScope;
38 import org.apache.commons.httpclient.methods.GetMethod;
39
40 /***
41 * Simple tests for HTTPS support in HttpClient.
42 *
43 * To run this test you'll need:
44 * + a JSSE implementation installed (see README.txt)
45 * + the java.protocol.handler.pkgs system property set
46 * for your provider. e.g.:
47 * -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol
48 * (see build.xml)
49 *
50 * @author Rodney Waldhoff
51 * @author Ortwin Glück
52 * @version $Id: TestHttps.java 155418 2005-02-26 13:01:52Z dirkv $
53 */
54 public class TestHttps extends TestCase {
55
56
57 private String _urlWithPort = null;
58 private String _urlWithoutPort = null;
59 private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
60 private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
61 private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser");
62 private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
63
64
65 public TestHttps(String testName) {
66 super(testName);
67 }
68
69
70 public static void main(String args[]) {
71 String[] testCaseName = { TestHttps.class.getName() };
72 junit.textui.TestRunner.main(testCaseName);
73 }
74
75
76 public static Test suite() {
77 return new TestSuite(TestHttps.class);
78 }
79
80 public void setUp() throws Exception {
81 _urlWithPort = "https://www.verisign.com:443/";
82 _urlWithoutPort = "https://www.verisign.com/";
83 }
84
85 public void testHttpsGet() {
86 HttpClient client = new HttpClient();
87 if (PROXY_HOST != null) {
88 if (PROXY_USER != null) {
89 HttpState state = client.getState();
90 state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
91 PROXY_USER, PROXY_PASS));
92 }
93 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
94 }
95 GetMethod method = new GetMethod(_urlWithPort);
96
97 try {
98 client.executeMethod(method);
99 } catch (Throwable t) {
100 t.printStackTrace();
101 fail("Exception thrown during HTTPS GET: " + t.toString());
102 }
103
104 try {
105 String data = method.getResponseBodyAsString();
106
107 assertTrue("No data returned.", (data.length() > 0));
108 } catch (Throwable t) {
109 t.printStackTrace();
110 fail("Exception thrown while retrieving data : " + t.toString());
111 }
112 }
113
114 public void testHttpsGetNoPort() {
115 HttpClient client = new HttpClient();
116 if (PROXY_HOST != null) {
117 if (PROXY_USER != null) {
118 HttpState state = client.getState();
119 state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
120 PROXY_USER, PROXY_PASS));
121 }
122 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
123 }
124 GetMethod method = new GetMethod(_urlWithoutPort);
125
126 try {
127 client.executeMethod(method);
128 } catch (Throwable t) {
129 t.printStackTrace();
130 fail("Exception thrown during HTTPS GET: " + t.toString());
131 }
132
133 try {
134 String data = method.getResponseBodyAsString();
135
136 assertTrue("No data returned.", (data.length() > 0));
137 } catch (Throwable t) {
138 t.printStackTrace();
139 fail("Exception thrown while retrieving data : " + t.toString());
140 }
141 }
142 }