1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappRedirect.java,v 1.18 2003/03/05 04:02:57 mbecke Exp $
3 * $Revision: 1.18 $
4 * $Date: 2003/03/05 04:02:57 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient;
65
66 import java.io.ByteArrayInputStream;
67
68 import junit.framework.Test;
69 import junit.framework.TestSuite;
70 import org.apache.commons.httpclient.methods.GetMethod;
71 import org.apache.commons.httpclient.methods.PostMethod;
72 import org.apache.commons.httpclient.methods.PutMethod;
73 import org.apache.commons.httpclient.util.URIUtil;
74 import org.apache.commons.logging.Log;
75 import org.apache.commons.logging.LogFactory;
76
77 /***
78 * This suite of tests depends upon the httpclienttest webapp,
79 * which is available in the httpclient/src/test-webapp
80 * directory in the CVS tree.
81 * <p>
82 * The webapp should be deployed in the context "httpclienttest"
83 * on a servlet engine running on port 8080 on the localhost
84 * (IP 127.0.0.1).
85 * <p>
86 * You can change the assumed port by setting the
87 * "httpclient.test.localPort" property.
88 * You can change the assumed host by setting the
89 * "httpclient.test.localHost" property.
90 * You can change the assumed context by setting the
91 * "httpclient.test.webappContext" property.
92 *
93 * @author Rodney Waldhoff
94 * @version $Id: TestWebappRedirect.java,v 1.18 2003/03/05 04:02:57 mbecke Exp $
95 */
96 public class TestWebappRedirect extends TestWebappBase {
97
98 private static final Log log = LogFactory.getLog(TestWebappRedirect.class);
99
100 private final String redirectUrl = "/" + getWebappContext() + "/redirect";
101
102 private final String paramsUrl = "/" + getWebappContext() + "/params";
103
104 private final String bodyUrl = "/" + getWebappContext() + "/body";
105
106 public TestWebappRedirect(String testName) {
107 super(testName);
108 }
109
110 public static Test suite() {
111 TestSuite suite = new TestSuite(TestWebappRedirect.class);
112 return suite;
113 }
114
115 public static void main(String args[]) {
116 String[] testCaseName = { TestWebappRedirect.class.getName() };
117 junit.textui.TestRunner.main(testCaseName);
118 }
119
120 public void absoluteRedirectHelper(int code) throws Exception {
121 HttpClient client = createHttpClient();
122 GetMethod method = new GetMethod(redirectUrl);
123 method.setQueryString("to=" + paramsUrl + "&code=" + code);
124 try {
125 client.executeMethod(method);
126 } catch (Throwable t) {
127 t.printStackTrace();
128 fail("Unable to execute method : " + t.toString());
129 }
130 assertEquals(200,method.getStatusCode());
131 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
132 }
133
134
135 // ------------------------------------------------------------------ Tests
136
137 public void testAbsoluteRedirectCode301() throws Exception {
138 absoluteRedirectHelper(301);
139 }
140
141 public void testAbsoluteRedirectCode302() throws Exception {
142 absoluteRedirectHelper(302);
143 }
144
145 public void testAbsoluteRedirectCode303() throws Exception {
146 absoluteRedirectHelper(303);
147 }
148
149 public void testAbsoluteRedirectCode307() throws Exception {
150 absoluteRedirectHelper(307);
151 }
152
153 public void testRelativeRedirect() throws Exception {
154 HttpClient client = createHttpClient();
155 GetMethod method = new GetMethod(redirectUrl);
156 method.setQueryString("to=params");
157 try {
158 client.executeMethod(method);
159 } catch (Throwable t) {
160 t.printStackTrace();
161 fail("Unable to execute method : " + t.toString());
162 }
163 assertEquals(200,method.getStatusCode());
164 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
165 }
166
167
168 public void testRedirectWithQueryString() throws Exception {
169 HttpClient client = createHttpClient();
170 GetMethod method = new GetMethod(redirectUrl);
171 method.setQueryString(new NameValuePair[] {
172 new NameValuePair("to", paramsUrl + "?foo=bar&bar=foo")
173 }
174 );
175 try {
176 client.executeMethod(method);
177 } catch (Throwable t) {
178 t.printStackTrace();
179 fail("Unable to execute method : " + t.toString());
180 }
181 assertEquals(200,method.getStatusCode());
182 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
183 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"foo=bar&bar=foo\"</p>") >= 0);
184 }
185
186 public void testRecursiveRedirect() throws Exception {
187 HttpClient client = createHttpClient();
188 GetMethod method = new GetMethod(redirectUrl);
189
190 String qs = paramsUrl + "?foo=bar&bar=foo";
191 for(int i=0;i<2;i++) {
192 qs = redirectUrl + "?to=" + URIUtil.encodeWithinQuery(qs);
193 }
194 method.setQueryString("to=" + URIUtil.encodeWithinQuery(qs));
195 try {
196 client.executeMethod(method);
197 } catch (Throwable t) {
198 t.printStackTrace();
199 fail("Unable to execute method : " + t.toString());
200 }
201 assertEquals(200,method.getStatusCode());
202 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
203 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"foo=bar&bar=foo\"</p>") >= 0);
204 }
205
206 public void testDetectRedirectLoop() throws Exception {
207 HttpClient client = createHttpClient();
208 GetMethod method = new GetMethod(redirectUrl);
209 method.setQueryString("loop=true");
210 try {
211 client.executeMethod(method);
212 fail("Expected HTTPException");
213 } catch (HttpException t) {
214 // expected
215 }
216 assertEquals(302,method.getStatusCode());
217 assertTrue(null != method.getResponseHeader("location"));
218 assertTrue(null != (method.getResponseHeader("location")).getValue());
219 assertEquals(client.getHostConfiguration().getHostURL() + "/" + getWebappContext() + "/redirect?loop=true",(method.getResponseHeader("location")).getValue());
220 log.info("Previous redirect loop warining is okay");
221 }
222
223 public void testPostRedirect() throws Exception {
224 String bodyStr = "Hello World";
225 HttpClient client = createHttpClient();
226 PostMethod method = new PostMethod(redirectUrl);
227 method.setQueryString("to=" + URIUtil.encodeWithinQuery(
228 client.getHostConfiguration().getHostURL() + "/"
229 + getWebappContext() + "/params?foo=bar&bar=foo"));
230 byte[] body = HttpConstants.getContentBytes(bodyStr);
231 method.setRequestBody(new ByteArrayInputStream(body));
232 method.setRequestContentLength(body.length); //unbuffered request
233
234 try {
235 client.executeMethod(method);
236 } catch (Throwable t) {
237 t.printStackTrace();
238 fail("Unable to execute method : " + t.toString());
239 }
240 //unbuffered request can not be redirected
241 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
242
243 method = new PostMethod(redirectUrl);
244 method.setQueryString("to=" + URIUtil.encodeWithinQuery(paramsUrl + "?foo=bar&bar=foo"));
245 method.setRequestBody(new ByteArrayInputStream(body));
246 method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO); //buffered request
247
248 try {
249 client.executeMethod(method);
250 } catch (Throwable t) {
251 t.printStackTrace();
252 fail("Unable to execute method : " + t.toString());
253 }
254 //buffered request is okay to redirect
255 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
256 }
257
258 public void testPutRedirect() throws Exception {
259 HttpClient client = createHttpClient();
260 PutMethod method = new PutMethod(redirectUrl);
261 method.setQueryString("to=" + URIUtil.encodeWithinQuery(bodyUrl + "?foo=bar&bar=foo"));
262 method.setRequestBody("This is data to be sent in the body of an HTTP PUT.");
263 try {
264 client.executeMethod(method);
265 } catch (Throwable t) {
266 t.printStackTrace();
267 fail("Unable to execute method : " + t.toString());
268 }
269 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
270 }
271 }
272
This page was automatically generated by Maven