KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsRedirectNoHost.java,v 1.6.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.6.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
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.TestCase;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.httpclient.methods.*;
38
39 /**
40  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
41  * @version $Revision: 1.6.2.1 $
42  */

43 public class TestMethodsRedirectNoHost extends TestCase {
44
45     
46     SimpleHttpConnection conn;
47  
48
49     // ------------------------------------------------------------ Constructor
50

51     public TestMethodsRedirectNoHost(String JavaDoc testName) {
52         super(testName);
53     }
54
55     // ------------------------------------------------------- TestCase Methods
56

57     public static Test suite() {
58         return new TestSuite(TestMethodsRedirectNoHost.class);
59     }
60
61     public void setUp() throws Exception JavaDoc{
62         conn = new SimpleHttpConnection();
63     }
64
65
66     private void addRedirectResponse(String JavaDoc location) {
67         String JavaDoc headers = "HTTP/1.1 302 Redirect\r\n"
68                        +"Date: Wed, 28 Mar 2002 05:05:04 GMT\r\n"
69                        +"Location: " + location + "\r\n"
70                        +"Connection: close\r\n";
71         conn.addResponse(headers, "");
72     }
73
74     private void addOkResponse() {
75         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
76                        +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
77                        +"Connection: close\r\n";
78         conn.addResponse(headers, "");
79     }
80
81
82     // ----------------------------------------------------------------- Tests
83

84     public void testRedirect() throws Exception JavaDoc {
85         addRedirectResponse("http://localhost/newfile");
86         addOkResponse();
87         conn.open();
88
89         HttpMethod method = new SimpleHttpMethod("/oldfile");
90         method.setFollowRedirects(true);
91         method.execute(new HttpState(), conn);
92         Header locationHeader = method.getResponseHeader("Location");
93         assertEquals(200, method.getStatusCode());
94         assertEquals("/newfile", method.getPath());
95         
96     }
97
98
99     public void testRedirectIgnoreCase() throws Exception JavaDoc {
100         addRedirectResponse("HtTP://localhost/newfile");
101         addOkResponse();
102         conn.open();
103
104         HttpMethod method = new SimpleHttpMethod("/oldfile");
105         method.setFollowRedirects(true);
106         method.execute(new HttpState(), conn);
107         Header locationHeader = method.getResponseHeader("Location");
108         assertEquals(200, method.getStatusCode());
109         assertEquals("/newfile", method.getPath());
110         
111     }
112
113
114     public void testPostRedirect() throws Exception JavaDoc {
115         addRedirectResponse("http://localhost/newfile");
116         addOkResponse();
117         conn.open();
118
119         PostMethod method = new PostMethod("/oldfile");
120         method.setRequestBody(new NameValuePair[] { new NameValuePair("name", "value") } );
121         method.execute(new HttpState(), conn);
122         Header locationHeader = method.getResponseHeader("Location");
123         assertEquals(302, method.getStatusCode());
124         assertEquals("/oldfile", method.getPath());
125         
126     }
127
128
129     public void testNoRedirect() throws Exception JavaDoc {
130
131         addRedirectResponse("http://localhost/newfile");
132         addOkResponse();
133         conn.open();
134
135         HttpMethod method = new SimpleHttpMethod("/oldfile");
136         method.setFollowRedirects(false);
137         method.execute(new HttpState(), conn);
138         Header locationHeader = method.getResponseHeader("Location");
139         assertEquals(302, method.getStatusCode());
140         assertEquals("/oldfile", method.getPath());
141         
142     }
143  
144
145     public void testRedirectBadLocation() throws Exception JavaDoc {
146         addRedirectResponse("newfile");
147         addOkResponse();
148         conn.open();
149
150         HttpMethod method = new SimpleHttpMethod("/oldfile");
151         method.setFollowRedirects(true);
152         method.setStrictMode(false);
153         method.execute(new HttpState(), conn);
154         Header locationHeader = method.getResponseHeader("Location");
155         assertEquals(200, method.getStatusCode());
156         assertEquals("/newfile", method.getPath());
157     }
158
159    
160     public void testRedirectBadLocationStrict() throws Exception JavaDoc {
161         addRedirectResponse("newfile");
162         addOkResponse();
163         conn.open();
164
165         HttpMethod method = new SimpleHttpMethod("/oldfile");
166         method.setFollowRedirects(true);
167         method.setStrictMode(true);
168         method.execute(new HttpState(), conn);
169         Header locationHeader = method.getResponseHeader("Location");
170         assertEquals(302, method.getStatusCode());
171         assertEquals("/oldfile", method.getPath());
172     }
173
174     public void testRedirectBogusLocationStrict() throws Exception JavaDoc {
175         addRedirectResponse("xxx://bogus");
176         addOkResponse();
177         conn.open();
178
179         HttpMethod method = new SimpleHttpMethod("/oldfile");
180         method.setFollowRedirects(true);
181         method.setStrictMode(true);
182         method.execute(new HttpState(), conn);
183         Header locationHeader = method.getResponseHeader("Location");
184         assertEquals(302, method.getStatusCode());
185         assertEquals("/oldfile", method.getPath());
186     }
187
188     public void testRedirectDifferentHost() throws Exception JavaDoc {
189         conn = new SimpleHttpConnection("oldhost", 80);
190         addRedirectResponse("http://newhost/newfile");
191         addOkResponse();
192         conn.open();
193
194         HttpMethod method = new SimpleHttpMethod("/oldfile");
195         method.setFollowRedirects(true);
196         method.execute(new HttpState(), conn);
197         Header locationHeader = method.getResponseHeader("Location");
198         assertEquals(302, method.getStatusCode());
199         assertEquals("/oldfile", method.getPath());
200     }
201
202     public void testRedirectDifferentPort() throws Exception JavaDoc {
203         conn = new SimpleHttpConnection("oldhost", 80);
204         addRedirectResponse("http://oldhost:8080/newfile");
205         addOkResponse();
206         conn.open();
207
208         HttpMethod method = new SimpleHttpMethod("/oldfile");
209         method.setFollowRedirects(true);
210         method.execute(new HttpState(), conn);
211         Header locationHeader = method.getResponseHeader("Location");
212         assertEquals(302, method.getStatusCode());
213         assertEquals("/oldfile", method.getPath());
214     }
215
216
217     public void testRedirectDifferentProtocol() throws Exception JavaDoc {
218         conn = new SimpleHttpConnection("oldhost", 80);
219         addRedirectResponse("https://oldhost:80/newfile");
220         addOkResponse();
221         conn.open();
222
223         HttpMethod method = new SimpleHttpMethod("/oldfile");
224         method.setFollowRedirects(true);
225         method.execute(new HttpState(), conn);
226         Header locationHeader = method.getResponseHeader("Location");
227         assertEquals(302, method.getStatusCode());
228         assertEquals("/oldfile", method.getPath());
229     }
230
231
232     public void testRedirectWithCookie() throws Exception JavaDoc {
233         addRedirectResponse("http://localhost/newfile");
234         addOkResponse();
235         conn.open();
236
237         HttpState state = new HttpState();
238         state.addCookie(
239             new Cookie("localhost", "name", "value", "/", -1, false));
240
241         HttpMethod method = new SimpleHttpMethod("/oldfile");
242         method.setFollowRedirects(true);
243         method.execute(state, conn);
244         Header locationHeader = method.getResponseHeader("Location");
245         assertEquals(200, method.getStatusCode());
246
247         Header[] headers = method.getRequestHeaders();
248         int cookiecount = 0;
249         for (int i = 0; i < headers.length; i++) {
250             if ("cookie".equalsIgnoreCase(headers[i].getName())) {
251                 ++cookiecount;
252             }
253         }
254         assertTrue("There can only be one (cookie)", cookiecount == 1);
255     }
256
257 }
258
Popular Tags