1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsRedirectNoHost.java,v 1.11 2004/04/24 23:51:57 olegk Exp $
3    * $Revision: 1.11 $
4    * $Date: 2004/04/24 23:51:57 $
5    * ====================================================================
6    *
7    *  Copyright 2002-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 junit.framework.Test;
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.httpclient.methods.PostMethod;
37  import org.apache.commons.httpclient.params.HttpClientParams;
38  
39  /***
40   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
41   * @version $Revision: 1.11 $
42   */
43  public class TestMethodsRedirectNoHost extends TestNoHostBase {
44  
45      
46      // ------------------------------------------------------------ Constructor
47  
48      public TestMethodsRedirectNoHost(String testName) {
49          super(testName);
50      }
51  
52      // ------------------------------------------------------- TestCase Methods
53  
54      public static Test suite() {
55          return new TestSuite(TestMethodsRedirectNoHost.class);
56      }
57  
58      private void addRedirectResponse(String location) {
59          String headers = "HTTP/1.1 302 Redirect\r\n"
60                         +"Date: Wed, 28 Mar 2002 05:05:04 GMT\r\n"
61                         +"Location: " + location + "\r\n"
62                         +"Connection: close\r\n";
63          conn.addResponse(headers, "");
64      }
65  
66      private void addOkResponse() {
67          String headers = "HTTP/1.1 200 OK\r\n"
68                         +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
69                         +"Connection: close\r\n";
70          conn.addResponse(headers, "");
71      }
72  
73  
74      // ----------------------------------------------------------------- Tests
75  
76      public void testRedirect() throws Exception {
77          addRedirectResponse("http://localhost/newfile");
78          addOkResponse();
79          conn.open();
80  
81          HttpMethod method = new SimpleHttpMethod("http://localhost/oldfile");
82          method.setFollowRedirects(true);
83          client.executeMethod(method);
84          Header locationHeader = method.getResponseHeader("Location");
85          assertEquals(200, method.getStatusCode());
86          assertEquals("/newfile", method.getPath());
87          
88      }
89  
90  
91      public void testRedirectIgnoreCase() throws Exception {
92          addRedirectResponse("HtTP://localhost/newfile");
93          addOkResponse();
94          conn.open();
95  
96          HttpMethod method = new SimpleHttpMethod("/oldfile");
97          method.setFollowRedirects(true);
98          client.executeMethod(method);
99          Header locationHeader = method.getResponseHeader("Location");
100         assertEquals(200, method.getStatusCode());
101         assertEquals("/newfile", method.getPath());
102         
103     }
104 
105 
106     public void testPostRedirect() throws Exception {
107         addRedirectResponse("http://localhost/newfile");
108         addOkResponse();
109         conn.open();
110 
111         PostMethod method = new PostMethod("/oldfile");
112         method.setRequestBody(new NameValuePair[] { new NameValuePair("name", "value") } );
113         client.executeMethod(method);
114         Header locationHeader = method.getResponseHeader("Location");
115         assertEquals(302, method.getStatusCode());
116         assertEquals("/oldfile", method.getPath());
117         
118     }
119 
120 
121     public void testNoRedirect() throws Exception {
122 
123         addRedirectResponse("http://localhost/newfile");
124         addOkResponse();
125         conn.open();
126 
127         HttpMethod method = new SimpleHttpMethod("/oldfile");
128         method.setFollowRedirects(false);
129         method.execute(new HttpState(), conn);
130         Header locationHeader = method.getResponseHeader("Location");
131         assertEquals(302, method.getStatusCode());
132         assertEquals("/oldfile", method.getPath());
133         
134     }
135  
136 
137     public void testRedirectBadLocation() throws Exception {
138         addRedirectResponse("newfile");
139         addOkResponse();
140         conn.open();
141 
142         HttpMethod method = new SimpleHttpMethod("/oldfile");
143         method.setFollowRedirects(true);
144         client.getParams().setBooleanParameter(
145             HttpClientParams.REJECT_RELATIVE_REDIRECT, false);
146         client.executeMethod(method);
147         Header locationHeader = method.getResponseHeader("Location");
148         assertEquals(200, method.getStatusCode());
149         assertEquals("/newfile", method.getPath());
150     }
151 
152    
153     public void testRedirectBadLocationStrict() throws Exception {
154         addRedirectResponse("newfile");
155         addOkResponse();
156         conn.open();
157 
158         HttpMethod method = new SimpleHttpMethod("/oldfile");
159         method.setFollowRedirects(true);
160         client.getParams().setBooleanParameter(
161             HttpClientParams.REJECT_RELATIVE_REDIRECT, true);
162         method.execute(new HttpState(), conn);
163         Header locationHeader = method.getResponseHeader("Location");
164         assertEquals(302, method.getStatusCode());
165         assertEquals("/oldfile", method.getPath());
166     }   
167 
168     public void testRedirectBogusLocationStrict() throws Exception {
169         addRedirectResponse("xxx://bogus");
170         addOkResponse();
171         conn.open();
172 
173         HttpMethod method = new SimpleHttpMethod("/oldfile");
174         method.setFollowRedirects(true);
175         client.getParams().setBooleanParameter(
176             HttpClientParams.REJECT_RELATIVE_REDIRECT, false);
177         method.execute(new HttpState(), conn);
178         Header locationHeader = method.getResponseHeader("Location");
179         assertEquals(302, method.getStatusCode());
180         assertEquals("/oldfile", method.getPath());
181     }
182 
183     public void testRedirectDifferentHost() throws Exception {
184         
185         addRedirectResponse("http://newhost/newfile");
186         addOkResponse();
187 
188         HttpMethod method = new SimpleHttpMethod("/oldfile");
189         method.setFollowRedirects(true);
190         client.executeMethod(method);
191         assertEquals(200, method.getStatusCode());
192         assertEquals("/newfile", method.getPath());
193         assertEquals("newhost", method.getRequestHeader("host").getValue());
194     }
195 
196     public void testRedirectDifferentPort() throws Exception {
197         conn = new SimpleHttpConnection("oldhost", 80);
198         addRedirectResponse("http://oldhost:8080/newfile");
199         addOkResponse();
200         conn.open();
201 
202         HttpMethod method = new SimpleHttpMethod("/oldfile");
203         method.setFollowRedirects(true);
204         method.execute(new HttpState(), conn);
205         Header locationHeader = method.getResponseHeader("Location");
206         assertEquals(302, method.getStatusCode());
207         assertEquals("/oldfile", method.getPath());
208     }
209 
210 
211     public void testRedirectDifferentProtocol() throws Exception {
212         conn = new SimpleHttpConnection("oldhost", 80);
213         addRedirectResponse("https://oldhost:80/newfile");
214         addOkResponse();
215         conn.open();
216 
217         HttpMethod method = new SimpleHttpMethod("/oldfile");
218         method.setFollowRedirects(true);
219         method.execute(new HttpState(), conn);
220         Header locationHeader = method.getResponseHeader("Location");
221         assertEquals(302, method.getStatusCode());
222         assertEquals("/oldfile", method.getPath());
223     }
224 
225 
226     public void testRedirectWithCookie() throws Exception {
227         addRedirectResponse("http://localhost/newfile");
228         addOkResponse();
229         conn.open();
230 
231         client.getState().addCookie(new Cookie("localhost", "name", "value", "/", -1, false)); 
232 
233         HttpMethod method = new SimpleHttpMethod("/oldfile");
234         method.setFollowRedirects(true);
235         client.executeMethod(method);
236         Header locationHeader = method.getResponseHeader("Location");
237         assertEquals(200, method.getStatusCode());
238 
239         Header[] headers = method.getRequestHeaders();
240         int cookiecount = 0;
241         for (int i = 0; i < headers.length; i++) {
242             if ("cookie".equalsIgnoreCase(headers[i].getName())) {
243                 ++cookiecount;
244             }
245         }
246         assertEquals("There can only be one (cookie)", 1, cookiecount);            
247     }
248 
249 }