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 package org.apache.commons.httpclient;
31
32 import org.apache.commons.httpclient.auth.AuthScope;
33 import org.apache.commons.httpclient.methods.GetMethod;
34 import org.apache.commons.httpclient.protocol.Protocol;
35 import org.apache.commons.httpclient.server.SimpleProxy;
36
37 import junit.framework.Test;
38 import junit.framework.TestCase;
39 import junit.framework.TestSuite;
40
41 /***
42 * Tests for proxied connections.
43 *
44 * @author Ortwin Glueck
45 */
46 public class TestProxy extends TestCase {
47
48 private static String TARGET_HOST = null;
49 private static int TARGET_PORT = -1;
50
51 public TestProxy(String testName) {
52 super(testName);
53 }
54
55 public static Test suite() {
56 return new TestSuite(TestProxy.class);
57 }
58
59 protected void setUp() throws Exception {
60 super.setUp();
61 TARGET_HOST = System.getProperty("httpclient.test.localHost", "localhost");
62 TARGET_PORT = Integer.parseInt(System.getProperty("httpclient.test.localPort", "8080"));
63 }
64
65 public void testSimpleGet() throws Exception {
66 SimpleProxy proxy = new SimpleProxy();
67
68 HttpClient client = new HttpClient();
69 HostConfiguration hc = new HostConfiguration();
70 hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http"));
71 hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
72 client.setHostConfiguration(hc);
73
74 GetMethod get = new GetMethod("/");
75 client.executeMethod(get);
76 assertEquals(200, get.getStatusCode());
77 get.releaseConnection();
78 }
79
80 public void testAuthGet() throws Exception {
81 Credentials creds = new UsernamePasswordCredentials("user", "password");
82 SimpleProxy proxy = new SimpleProxy();
83 proxy.requireCredentials(creds);
84
85 HttpClient client = new HttpClient();
86 HostConfiguration hc = new HostConfiguration();
87 hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http"));
88 hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
89 client.setHostConfiguration(hc);
90 client.getState().setProxyCredentials(AuthScope.ANY, creds);
91
92 GetMethod get = new GetMethod("/");
93 client.executeMethod(get);
94 assertEquals(200, get.getStatusCode());
95 get.releaseConnection();
96 }
97
98 }