1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v 1.2 2004/04/12 11:16:25 olegk Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/04/12 11:16:25 $
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  
31  package org.apache.commons.httpclient;
32  
33  import java.io.IOException;
34  
35  import junit.framework.Test;
36  import junit.framework.TestSuite;
37  
38  import org.apache.commons.httpclient.methods.GetMethod;
39  import org.apache.commons.httpclient.params.HttpClientParams;
40  import org.apache.commons.httpclient.server.HttpService;
41  import org.apache.commons.httpclient.server.RequestLine;
42  import org.apache.commons.httpclient.server.SimpleRequest;
43  import org.apache.commons.httpclient.server.SimpleResponse;
44  
45  /***
46   * Basic authentication test cases.
47   *
48   * @author Oleg Kalnichevski
49   * 
50   * @version $Id: TestRedirects.java,v 1.2 2004/04/12 11:16:25 olegk Exp $
51   */
52  public class TestRedirects extends HttpClientTestBase {
53  
54      // ------------------------------------------------------------ Constructor
55      public TestRedirects(String testName) {
56          super(testName);
57      }
58  
59      // ------------------------------------------------------------------- Main
60      public static void main(String args[]) {
61          String[] testCaseName = { TestRedirects.class.getName() };
62          junit.textui.TestRunner.main(testCaseName);
63      }
64  
65      // ------------------------------------------------------- TestCase Methods
66  
67      public static Test suite() {
68          return new TestSuite(TestRedirects.class);
69      }
70  
71      private class RedirectService implements HttpService {
72  
73          public RedirectService() {
74              super();
75          }
76  
77          public boolean process(final SimpleRequest request, final SimpleResponse response)
78              throws IOException
79          {
80              RequestLine reqline = request.getRequestLine();
81              if (reqline.getUri().equals("/circular-location1/")) {
82                  response.setStatusLine("HTTP/1.1 302 Object moved");
83                  response.addHeader(new Header("Location", "/circular-location2/"));
84              } else if (reqline.getUri().equals("/circular-location2/")) {
85                  response.setStatusLine("HTTP/1.1 302 Object moved");
86                  response.addHeader(new Header("Location", "/circular-location1/"));
87              } else {
88                  response.setStatusLine("HTTP/1.1 404 Not Found");
89              }
90              return true;
91          }
92      }
93  
94      public void testMaxRedirectCheck() throws IOException {
95          this.server.setHttpService(new RedirectService());
96          GetMethod httpget = new GetMethod("/circular-location1/");
97          try {
98              this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
99              this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 5);
100             this.client.executeMethod(httpget);
101             fail("RedirectException exception should have been thrown");
102         }
103         catch (RedirectException e) {
104             // expected
105         } finally {
106             httpget.releaseConnection();
107         }
108     }
109 
110     public void testCircularRedirect() throws IOException {
111         this.server.setHttpService(new RedirectService());
112         GetMethod httpget = new GetMethod("/circular-location1/");
113         try {
114             this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, false);
115             this.client.executeMethod(httpget);
116             fail("RedirectException exception should have been thrown");
117         }
118         catch (RedirectException e) {
119             // expected
120         } finally {
121             httpget.releaseConnection();
122         }
123     }
124 
125     private class RedirectService2 implements HttpService {
126     
127         private String host = null;
128         private int port;
129 
130         public RedirectService2(final String host, int port) {
131             super();
132             this.host = host;
133             this.port = port;
134         }
135 
136         public boolean process(final SimpleRequest request, final SimpleResponse response)
137             throws IOException
138         {
139             RequestLine reqline = request.getRequestLine();
140             if (reqline.getUri().equals("/location1/")) {
141                 response.setStatusLine("HTTP/1.1 302 Object moved");
142                 response.addHeader(new Header("Location", "http://" + this.host + ":" + this.port + "/location2/"));
143             } else if (reqline.getUri().equals("/location2/")) {
144                 response.setStatusLine("HTTP/1.1 200 OK");
145                 response.setBodyString("Successful redirect");
146             } else {
147                 response.setStatusLine("HTTP/1.1 404 Not Found");
148             }
149             return true;
150         }
151     }
152 
153     public void testRedirectLocation() throws IOException {
154         String host = this.server.getLocalAddress();
155         int port = this.server.getLocalPort();
156         this.server.setHttpService(new RedirectService2(host, port));
157         GetMethod httpget = new GetMethod("/location1/");
158         try {
159             this.client.executeMethod(httpget);
160             assertEquals(host, httpget.getURI().getHost());
161             assertEquals(port, httpget.getURI().getPort());
162             assertEquals(new URI("http://" + host + ":" + port + "/location2/", false), httpget.getURI());
163         } finally {
164             httpget.releaseConnection();
165         }
166     }
167 
168 }