1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v 1.5 2004/02/22 18:08:49 olegk Exp $
3    * $Revision: 1.5 $
4    * $Date: 2004/02/22 18:08:49 $
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.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  }