1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestProxyWithRedirect.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  package org.apache.commons.httpclient;
30  
31  import java.io.IOException;
32  
33  import junit.framework.Test;
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.httpclient.auth.AuthScope;
37  import org.apache.commons.httpclient.methods.GetMethod;
38  import org.apache.commons.httpclient.server.RequestLine;
39  import org.apache.commons.httpclient.server.SimpleRequest;
40  import org.apache.commons.httpclient.server.SimpleResponse;
41  
42  /***
43   * Tests for proxied connections.
44   * 
45   * @author Ortwin Glueck
46   * @author Oleg Kalnichevski
47   */
48  public class TestProxyWithRedirect extends HttpClientTestBase {
49  
50      public TestProxyWithRedirect(String testName) throws IOException {
51          super(testName);
52          setUseProxy(true);
53      }
54  
55      public static Test suite() {
56          return new TestSuite(TestProxyWithRedirect.class);
57      }
58      
59      private class BasicRedirectService extends EchoService {
60          
61          private String location = null;
62  
63          public BasicRedirectService(final String location) {
64              super();
65              this.location = location;
66          }
67  
68          public boolean process(final SimpleRequest request, final SimpleResponse response)
69              throws IOException
70          {
71              RequestLine reqline = request.getRequestLine();
72              HttpVersion ver = reqline.getHttpVersion();
73              if (reqline.getUri().equals("/redirect/")) {
74                  response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
75                  response.addHeader(new Header("Location", this.location));
76                  response.addHeader(new Header("Connection", "Close"));
77                  return true;
78              } else {
79                  return super.process(request, response);
80              }
81          }
82      }
83      
84      public void testAuthProxyWithRedirect() throws Exception {
85          UsernamePasswordCredentials creds = 
86              new UsernamePasswordCredentials("testuser", "testpass");
87          
88          this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
89          this.server.setHttpService(new BasicRedirectService("/"));
90          this.proxy.requireAuthentication(creds, "test", true);
91          
92          GetMethod get = new GetMethod("/redirect/");
93          try {
94              this.client.executeMethod(get);
95              assertEquals(HttpStatus.SC_OK, get.getStatusCode());
96          } finally {
97              get.releaseConnection();
98          }
99      }
100     
101     public void testAuthProxyWithCrossSiteRedirect() throws Exception {
102         UsernamePasswordCredentials creds = 
103             new UsernamePasswordCredentials("testuser", "testpass");
104         
105         this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
106         this.server.setHttpService(new BasicRedirectService(
107                 "http://127.0.0.1:" + this.server.getLocalPort()));
108 
109         this.proxy.requireAuthentication(creds, "test", true);
110         
111         GetMethod get = new GetMethod("/redirect/");
112         try {
113             this.client.executeMethod(get);
114             assertEquals(HttpStatus.SC_OK, get.getStatusCode());
115         } finally {
116             get.releaseConnection();
117         }
118     }
119 
120     public void testPreemptiveAuthProxyWithCrossSiteRedirect() throws Exception {
121         UsernamePasswordCredentials creds = 
122             new UsernamePasswordCredentials("testuser", "testpass");
123         
124         this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
125         this.client.getParams().setAuthenticationPreemptive(true);
126         this.server.setHttpService(new BasicRedirectService(
127                 "http://127.0.0.1:" + this.server.getLocalPort()));
128 
129         this.proxy.requireAuthentication(creds, "test", true);
130         
131         GetMethod get = new GetMethod("/redirect/");
132         try {
133             this.client.executeMethod(get);
134             assertEquals(HttpStatus.SC_OK, get.getStatusCode());
135         } finally {
136             get.releaseConnection();
137         }
138     }
139     
140 }