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
39 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
40 import org.apache.commons.httpclient.methods.GetMethod;
41 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
42 import org.apache.commons.httpclient.methods.PostMethod;
43 import org.apache.commons.httpclient.methods.PutMethod;
44 import org.apache.commons.httpclient.methods.StringRequestEntity;
45 import org.apache.commons.httpclient.util.EncodingUtil;
46 import org.apache.commons.httpclient.util.URIUtil;
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50 /***
51 * This suite of tests depends upon the httpclienttest webapp,
52 * which is available in the httpclient/src/test-webapp
53 * directory in the CVS tree.
54 * <p>
55 * The webapp should be deployed in the context "httpclienttest"
56 * on a servlet engine running on port 8080 on the localhost
57 * (IP 127.0.0.1).
58 * <p>
59 * You can change the assumed port by setting the
60 * "httpclient.test.localPort" property.
61 * You can change the assumed host by setting the
62 * "httpclient.test.localHost" property.
63 * You can change the assumed context by setting the
64 * "httpclient.test.webappContext" property.
65 *
66 * @author Rodney Waldhoff
67 * @version $Id: TestWebappRedirect.java,v 1.23 2004/05/12 20:43:54 olegk Exp $
68 */
69 public class TestWebappRedirect extends TestWebappBase {
70
71 private static final Log log = LogFactory.getLog(TestWebappRedirect.class);
72
73 private final String redirectUrl = "/" + getWebappContext() + "/redirect";
74
75 private final String paramsUrl = "/" + getWebappContext() + "/params";
76
77 private final String bodyUrl = "/" + getWebappContext() + "/body";
78
79 public TestWebappRedirect(String testName) {
80 super(testName);
81 }
82
83 public static Test suite() {
84 TestSuite suite = new TestSuite(TestWebappRedirect.class);
85 return suite;
86 }
87
88 public static void main(String args[]) {
89 String[] testCaseName = { TestWebappRedirect.class.getName() };
90 junit.textui.TestRunner.main(testCaseName);
91 }
92
93 public void absoluteRedirectHelper(int code) throws Exception {
94 HttpClient client = createHttpClient();
95 GetMethod method = new GetMethod(redirectUrl);
96 method.setQueryString("to=" + paramsUrl + "&code=" + code);
97 client.executeMethod(method);
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 client.executeMethod(method);
126 assertEquals(200,method.getStatusCode());
127 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
128 }
129
130
131 public void testRedirectWithQueryString() throws Exception {
132 HttpClient client = createHttpClient();
133 GetMethod method = new GetMethod(redirectUrl);
134 method.setQueryString(new NameValuePair[] {
135 new NameValuePair("to", paramsUrl + "?foo=bar&bar=foo")
136 }
137 );
138 client.executeMethod(method);
139 assertEquals(200,method.getStatusCode());
140 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
141 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"foo=bar&bar=foo\"</p>") >= 0);
142 }
143
144 public void testRecursiveRedirect() throws Exception {
145 HttpClient client = createHttpClient();
146 GetMethod method = new GetMethod(redirectUrl);
147
148 String qs = paramsUrl + "?foo=bar&bar=foo";
149 for(int i=0;i<2;i++) {
150 qs = redirectUrl + "?to=" + URIUtil.encodeWithinQuery(qs);
151 }
152 method.setQueryString("to=" + URIUtil.encodeWithinQuery(qs));
153 client.executeMethod(method);
154 assertEquals(200,method.getStatusCode());
155 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
156 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"foo=bar&bar=foo\"</p>") >= 0);
157 }
158
159 public void testDetectRedirectLoop() throws Exception {
160 HttpClient client = createHttpClient();
161 GetMethod method = new GetMethod(redirectUrl);
162 method.setQueryString("loop=true");
163 try {
164 client.executeMethod(method);
165 fail("Expected HTTPException");
166 } catch (ProtocolException t) {
167
168 }
169 assertEquals(302,method.getStatusCode());
170 assertTrue(null != method.getResponseHeader("location"));
171 assertTrue(null != (method.getResponseHeader("location")).getValue());
172 assertEquals(client.getHostConfiguration().getHostURL() + "/" + getWebappContext() + "/redirect?loop=true",(method.getResponseHeader("location")).getValue());
173 log.info("Previous redirect loop warining is okay");
174 }
175
176 public void testPostRedirect() throws Exception {
177 String bodyStr = "Hello World";
178 HttpClient client = createHttpClient();
179 PostMethod method = new PostMethod(redirectUrl);
180 method.setQueryString("to=" + URIUtil.encodeWithinQuery(
181 client.getHostConfiguration().getHostURL() + "/"
182 + getWebappContext() + "/params?foo=bar&bar=foo"));
183 byte[] body = EncodingUtil.getBytes(bodyStr, "ISO-8859-1");
184 method.setRequestEntity(new ByteArrayRequestEntity(body));
185
186 try {
187 client.executeMethod(method);
188 } catch (Throwable t) {
189 t.printStackTrace();
190 fail("Unable to execute method : " + t.toString());
191 }
192
193 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
194
195 method = new PostMethod(redirectUrl);
196 method.setQueryString("to=" + URIUtil.encodeWithinQuery(paramsUrl + "?foo=bar&bar=foo"));
197 method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(body)));
198
199 try {
200 client.executeMethod(method);
201 } catch (Throwable t) {
202 t.printStackTrace();
203 fail("Unable to execute method : " + t.toString());
204 }
205
206 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
207 }
208
209 public void testPutRedirect() throws Exception {
210 HttpClient client = createHttpClient();
211 PutMethod method = new PutMethod(redirectUrl);
212 method.setQueryString("to=" + URIUtil.encodeWithinQuery(bodyUrl + "?foo=bar&bar=foo"));
213 method.setRequestEntity(new StringRequestEntity("This is data to be sent in the body of an HTTP PUT."));
214 client.executeMethod(method);
215 assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode());
216 }
217 }
218