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