1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient;
33
34 import java.io.ByteArrayInputStream;
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 redirectUrl = "/" + getWebappContext() + "/redirect";
69
70 private final String paramsUrl = "/" + getWebappContext() + "/params";
71
72 private final String bodyUrl = "/" + getWebappContext() + "/body";
73
74 public TestWebappRedirect(String 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 args[]) {
84 String[] testCaseName = { TestWebappRedirect.class.getName() };
85 junit.textui.TestRunner.main(testCaseName);
86 }
87
88 public void absoluteRedirectHelper(int code) throws Exception {
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 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
104
105 public void testAbsoluteRedirectCode301() throws Exception {
106 absoluteRedirectHelper(301);
107 }
108
109 public void testAbsoluteRedirectCode302() throws Exception {
110 absoluteRedirectHelper(302);
111 }
112
113 public void testAbsoluteRedirectCode303() throws Exception {
114 absoluteRedirectHelper(303);
115 }
116
117 public void testAbsoluteRedirectCode307() throws Exception {
118 absoluteRedirectHelper(307);
119 }
120
121 public void testRelativeRedirect() throws Exception {
122 HttpClient client = createHttpClient();
123 GetMethod method = new GetMethod(redirectUrl);
124 method.setQueryString("to=params");
125 try {
126 client.executeMethod(method);
127 } catch (Throwable 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 {
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 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 {
155 HttpClient client = createHttpClient();
156 GetMethod method = new GetMethod(redirectUrl);
157
158 String 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 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 {
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
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 {
192 String 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(body));
200 method.setRequestContentLength(body.length);
201
202 try {
203 client.executeMethod(method);
204 } catch (Throwable t) {
205 t.printStackTrace();
206 fail("Unable to execute method : " + t.toString());
207 }
208
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(body));
214 method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO);
215
216 try {
217 client.executeMethod(method);
218 } catch (Throwable t) {
219 t.printStackTrace();
220 fail("Unable to execute method : " + t.toString());
221 }
222
223 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
224 }
225
226 public void testPutRedirect() throws Exception {
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 t) {
234 t.printStackTrace();
235 fail("Unable to execute method : " + t.toString());
236 }
237 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
238 }
239 }
240