KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappRedirect.java,v 1.18.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.18.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * 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  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import java.io.ByteArrayInputStream JavaDoc;
35
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38 import org.apache.commons.httpclient.methods.GetMethod;
39 import org.apache.commons.httpclient.methods.PostMethod;
40 import org.apache.commons.httpclient.methods.PutMethod;
41 import org.apache.commons.httpclient.util.URIUtil;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46  * This suite of tests depends upon the httpclienttest webapp,
47  * which is available in the httpclient/src/test-webapp
48  * directory in the CVS tree.
49  * <p>
50  * The webapp should be deployed in the context "httpclienttest"
51  * on a servlet engine running on port 8080 on the localhost
52  * (IP 127.0.0.1).
53  * <p>
54  * You can change the assumed port by setting the
55  * "httpclient.test.localPort" property.
56  * You can change the assumed host by setting the
57  * "httpclient.test.localHost" property.
58  * You can change the assumed context by setting the
59  * "httpclient.test.webappContext" property.
60  *
61  * @author Rodney Waldhoff
62  * @version $Id: TestWebappRedirect.java,v 1.18.2.1 2004/02/22 18:21:16 olegk Exp $
63  */

64 public class TestWebappRedirect extends TestWebappBase {
65     
66     private static final Log log = LogFactory.getLog(TestWebappRedirect.class);
67
68     private final String JavaDoc redirectUrl = "/" + getWebappContext() + "/redirect";
69
70     private final String JavaDoc paramsUrl = "/" + getWebappContext() + "/params";
71
72     private final String JavaDoc bodyUrl = "/" + getWebappContext() + "/body";
73
74     public TestWebappRedirect(String JavaDoc testName) {
75         super(testName);
76     }
77
78     public static Test suite() {
79         TestSuite suite = new TestSuite(TestWebappRedirect.class);
80         return suite;
81     }
82
83     public static void main(String JavaDoc args[]) {
84         String JavaDoc[] testCaseName = { TestWebappRedirect.class.getName() };
85         junit.textui.TestRunner.main(testCaseName);
86     }
87
88     public void absoluteRedirectHelper(int code) throws Exception JavaDoc {
89         HttpClient client = createHttpClient();
90         GetMethod method = new GetMethod(redirectUrl);
91         method.setQueryString("to=" + paramsUrl + "&code=" + code);
92         try {
93             client.executeMethod(method);
94         } catch (Throwable JavaDoc t) {
95             t.printStackTrace();
96             fail("Unable to execute method : " + t.toString());
97         }
98         assertEquals(200,method.getStatusCode());
99         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
100     }
101
102
103     // ------------------------------------------------------------------ Tests
104

105     public void testAbsoluteRedirectCode301() throws Exception JavaDoc {
106         absoluteRedirectHelper(301);
107     }
108
109     public void testAbsoluteRedirectCode302() throws Exception JavaDoc {
110         absoluteRedirectHelper(302);
111     }
112
113     public void testAbsoluteRedirectCode303() throws Exception JavaDoc {
114         absoluteRedirectHelper(303);
115     }
116
117     public void testAbsoluteRedirectCode307() throws Exception JavaDoc {
118         absoluteRedirectHelper(307);
119     }
120
121     public void testRelativeRedirect() throws Exception JavaDoc {
122         HttpClient client = createHttpClient();
123         GetMethod method = new GetMethod(redirectUrl);
124         method.setQueryString("to=params");
125         try {
126             client.executeMethod(method);
127         } catch (Throwable JavaDoc t) {
128             t.printStackTrace();
129             fail("Unable to execute method : " + t.toString());
130         }
131         assertEquals(200,method.getStatusCode());
132         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
133     }
134
135
136     public void testRedirectWithQueryString() throws Exception JavaDoc {
137         HttpClient client = createHttpClient();
138         GetMethod method = new GetMethod(redirectUrl);
139         method.setQueryString(new NameValuePair[] {
140             new NameValuePair("to", paramsUrl + "?foo=bar&bar=foo")
141             }
142         );
143         try {
144             client.executeMethod(method);
145         } catch (Throwable JavaDoc t) {
146             t.printStackTrace();
147             fail("Unable to execute method : " + t.toString());
148         }
149         assertEquals(200,method.getStatusCode());
150         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
151         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"foo=bar&bar=foo\"</p>") >= 0);
152     }
153
154     public void testRecursiveRedirect() throws Exception JavaDoc {
155         HttpClient client = createHttpClient();
156         GetMethod method = new GetMethod(redirectUrl);
157
158         String JavaDoc qs = paramsUrl + "?foo=bar&bar=foo";
159         for(int i=0;i<2;i++) {
160             qs = redirectUrl + "?to=" + URIUtil.encodeWithinQuery(qs);
161         }
162         method.setQueryString("to=" + URIUtil.encodeWithinQuery(qs));
163         try {
164             client.executeMethod(method);
165         } catch (Throwable JavaDoc t) {
166             t.printStackTrace();
167             fail("Unable to execute method : " + t.toString());
168         }
169         assertEquals(200,method.getStatusCode());
170         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
171         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"foo=bar&bar=foo\"</p>") >= 0);
172     }
173
174     public void testDetectRedirectLoop() throws Exception JavaDoc {
175         HttpClient client = createHttpClient();
176         GetMethod method = new GetMethod(redirectUrl);
177         method.setQueryString("loop=true");
178         try {
179             client.executeMethod(method);
180             fail("Expected HTTPException");
181         } catch (HttpException t) {
182             // expected
183
}
184         assertEquals(302,method.getStatusCode());
185         assertTrue(null != method.getResponseHeader("location"));
186         assertTrue(null != (method.getResponseHeader("location")).getValue());
187         assertEquals(client.getHostConfiguration().getHostURL() + "/" + getWebappContext() + "/redirect?loop=true",(method.getResponseHeader("location")).getValue());
188         log.info("Previous redirect loop warining is okay");
189     }
190
191     public void testPostRedirect() throws Exception JavaDoc {
192         String JavaDoc bodyStr = "Hello World";
193         HttpClient client = createHttpClient();
194         PostMethod method = new PostMethod(redirectUrl);
195         method.setQueryString("to=" + URIUtil.encodeWithinQuery(
196                     client.getHostConfiguration().getHostURL() + "/"
197                     + getWebappContext() + "/params?foo=bar&bar=foo"));
198         byte[] body = HttpConstants.getContentBytes(bodyStr);
199         method.setRequestBody(new ByteArrayInputStream JavaDoc(body));
200         method.setRequestContentLength(body.length); //unbuffered request
201

202         try {
203             client.executeMethod(method);
204         } catch (Throwable JavaDoc t) {
205             t.printStackTrace();
206             fail("Unable to execute method : " + t.toString());
207         }
208         //unbuffered request can not be redirected
209
assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
210
211         method = new PostMethod(redirectUrl);
212         method.setQueryString("to=" + URIUtil.encodeWithinQuery(paramsUrl + "?foo=bar&bar=foo"));
213         method.setRequestBody(new ByteArrayInputStream JavaDoc(body));
214         method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO); //buffered request
215

216         try {
217             client.executeMethod(method);
218         } catch (Throwable JavaDoc t) {
219             t.printStackTrace();
220             fail("Unable to execute method : " + t.toString());
221         }
222         //buffered request is okay to redirect
223
assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
224     }
225
226     public void testPutRedirect() throws Exception JavaDoc {
227         HttpClient client = createHttpClient();
228         PutMethod method = new PutMethod(redirectUrl);
229         method.setQueryString("to=" + URIUtil.encodeWithinQuery(bodyUrl + "?foo=bar&bar=foo"));
230         method.setRequestBody("This is data to be sent in the body of an HTTP PUT.");
231         try {
232             client.executeMethod(method);
233         } catch (Throwable JavaDoc t) {
234             t.printStackTrace();
235             fail("Unable to execute method : " + t.toString());
236         }
237         assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
238     }
239 }
240
241
Popular Tags