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