KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestProxy


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v 1.1.2.2 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.1.2.2 $
4  * $Date: 2004/02/22 18:21:16 $
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  * @author Ortwin Glueck
43  */

44 public class TestProxy extends TestCase {
45
46     public TestProxy(String JavaDoc 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 JavaDoc {
55         /*
56         System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
57         System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
58         System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
59         System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
60         */

61     }
62     
63     public void testSimpleGet() throws Exception JavaDoc {
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 JavaDoc {
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 }
96
Popular Tags