1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v 1.6 2004/06/13 12:13:08 olegk Exp $
3    * $Revision: 1.6 $
4    * $Date: 2004/06/13 12:13:08 $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
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  }