1   /*
2    * $HeadURL: http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestProxyWithRedirect.java $
3    * $Revision: 162028 $
4    * $Date: 2005-04-20 13:37:36 -0400 (Wed, 20 Apr 2005) $
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   */
28  package org.apache.commons.httpclient;
29  
30  import java.io.IOException;
31  
32  import junit.framework.Test;
33  import junit.framework.TestSuite;
34  
35  import org.apache.commons.httpclient.auth.AuthScope;
36  import org.apache.commons.httpclient.methods.GetMethod;
37  import org.apache.commons.httpclient.server.RequestLine;
38  import org.apache.commons.httpclient.server.SimpleRequest;
39  import org.apache.commons.httpclient.server.SimpleResponse;
40  
41  /***
42   * Tests for proxied connections.
43   * 
44   * @author Ortwin Glueck
45   * @author Oleg Kalnichevski
46   */
47  public class TestProxyWithRedirect extends HttpClientTestBase {
48  
49      public TestProxyWithRedirect(String testName) throws IOException {
50          super(testName);
51          setUseProxy(true);
52      }
53  
54      public static Test suite() {
55          return new TestSuite(TestProxyWithRedirect.class);
56      }
57      
58      private class BasicRedirectService extends EchoService {
59          
60          private String location = null;
61  
62          public BasicRedirectService(final String location) {
63              super();
64              this.location = location;
65          }
66  
67          public boolean process(final SimpleRequest request, final SimpleResponse response)
68              throws IOException
69          {
70              RequestLine reqline = request.getRequestLine();
71              HttpVersion ver = reqline.getHttpVersion();
72              if (reqline.getUri().equals("/redirect/")) {
73                  response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
74                  response.addHeader(new Header("Location", this.location));
75                  response.addHeader(new Header("Connection", "Close"));
76                  return true;
77              } else {
78                  return super.process(request, response);
79              }
80          }
81      }
82      
83      public void testAuthProxyWithRedirect() throws Exception {
84          UsernamePasswordCredentials creds = 
85              new UsernamePasswordCredentials("testuser", "testpass");
86          
87          this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
88          this.server.setHttpService(new BasicRedirectService("/"));
89          this.proxy.requireAuthentication(creds, "test", true);
90          
91          GetMethod get = new GetMethod("/redirect/");
92          try {
93              this.client.executeMethod(get);
94              assertEquals(HttpStatus.SC_OK, get.getStatusCode());
95          } finally {
96              get.releaseConnection();
97          }
98      }
99      
100     public void testAuthProxyWithCrossSiteRedirect() throws Exception {
101         UsernamePasswordCredentials creds = 
102             new UsernamePasswordCredentials("testuser", "testpass");
103         
104         this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
105         this.server.setHttpService(new BasicRedirectService(
106                 "http://127.0.0.1:" + this.server.getLocalPort()));
107 
108         this.proxy.requireAuthentication(creds, "test", true);
109         
110         GetMethod get = new GetMethod("/redirect/");
111         try {
112             this.client.executeMethod(get);
113             assertEquals(HttpStatus.SC_OK, get.getStatusCode());
114         } finally {
115             get.releaseConnection();
116         }
117     }
118 
119     public void testPreemptiveAuthProxyWithCrossSiteRedirect() throws Exception {
120         UsernamePasswordCredentials creds = 
121             new UsernamePasswordCredentials("testuser", "testpass");
122         
123         this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
124         this.client.getParams().setAuthenticationPreemptive(true);
125         this.server.setHttpService(new BasicRedirectService(
126                 "http://127.0.0.1:" + this.server.getLocalPort()));
127 
128         this.proxy.requireAuthentication(creds, "test", true);
129         
130         GetMethod get = new GetMethod("/redirect/");
131         try {
132             this.client.executeMethod(get);
133             assertEquals(HttpStatus.SC_OK, get.getStatusCode());
134         } finally {
135             get.releaseConnection();
136         }
137     }
138     
139 }