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 package org.apache.commons.httpclient;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.apache.commons.httpclient.methods.PostMethod;
37 import org.apache.commons.httpclient.params.HttpClientParams;
38
39 /***
40 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
41 * @version $Revision: 1.11 $
42 */
43 public class TestMethodsRedirectNoHost extends TestNoHostBase {
44
45
46
47
48 public TestMethodsRedirectNoHost(String testName) {
49 super(testName);
50 }
51
52
53
54 public static Test suite() {
55 return new TestSuite(TestMethodsRedirectNoHost.class);
56 }
57
58 private void addRedirectResponse(String location) {
59 String headers = "HTTP/1.1 302 Redirect\r\n"
60 +"Date: Wed, 28 Mar 2002 05:05:04 GMT\r\n"
61 +"Location: " + location + "\r\n"
62 +"Connection: close\r\n";
63 conn.addResponse(headers, "");
64 }
65
66 private void addOkResponse() {
67 String headers = "HTTP/1.1 200 OK\r\n"
68 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
69 +"Connection: close\r\n";
70 conn.addResponse(headers, "");
71 }
72
73
74
75
76 public void testRedirect() throws Exception {
77 addRedirectResponse("http://localhost/newfile");
78 addOkResponse();
79 conn.open();
80
81 HttpMethod method = new SimpleHttpMethod("http://localhost/oldfile");
82 method.setFollowRedirects(true);
83 client.executeMethod(method);
84 Header locationHeader = method.getResponseHeader("Location");
85 assertEquals(200, method.getStatusCode());
86 assertEquals("/newfile", method.getPath());
87
88 }
89
90
91 public void testRedirectIgnoreCase() throws Exception {
92 addRedirectResponse("HtTP://localhost/newfile");
93 addOkResponse();
94 conn.open();
95
96 HttpMethod method = new SimpleHttpMethod("/oldfile");
97 method.setFollowRedirects(true);
98 client.executeMethod(method);
99 Header locationHeader = method.getResponseHeader("Location");
100 assertEquals(200, method.getStatusCode());
101 assertEquals("/newfile", method.getPath());
102
103 }
104
105
106 public void testPostRedirect() throws Exception {
107 addRedirectResponse("http://localhost/newfile");
108 addOkResponse();
109 conn.open();
110
111 PostMethod method = new PostMethod("/oldfile");
112 method.setRequestBody(new NameValuePair[] { new NameValuePair("name", "value") } );
113 client.executeMethod(method);
114 Header locationHeader = method.getResponseHeader("Location");
115 assertEquals(302, method.getStatusCode());
116 assertEquals("/oldfile", method.getPath());
117
118 }
119
120
121 public void testNoRedirect() throws Exception {
122
123 addRedirectResponse("http://localhost/newfile");
124 addOkResponse();
125 conn.open();
126
127 HttpMethod method = new SimpleHttpMethod("/oldfile");
128 method.setFollowRedirects(false);
129 method.execute(new HttpState(), conn);
130 Header locationHeader = method.getResponseHeader("Location");
131 assertEquals(302, method.getStatusCode());
132 assertEquals("/oldfile", method.getPath());
133
134 }
135
136
137 public void testRedirectBadLocation() throws Exception {
138 addRedirectResponse("newfile");
139 addOkResponse();
140 conn.open();
141
142 HttpMethod method = new SimpleHttpMethod("/oldfile");
143 method.setFollowRedirects(true);
144 client.getParams().setBooleanParameter(
145 HttpClientParams.REJECT_RELATIVE_REDIRECT, false);
146 client.executeMethod(method);
147 Header locationHeader = method.getResponseHeader("Location");
148 assertEquals(200, method.getStatusCode());
149 assertEquals("/newfile", method.getPath());
150 }
151
152
153 public void testRedirectBadLocationStrict() throws Exception {
154 addRedirectResponse("newfile");
155 addOkResponse();
156 conn.open();
157
158 HttpMethod method = new SimpleHttpMethod("/oldfile");
159 method.setFollowRedirects(true);
160 client.getParams().setBooleanParameter(
161 HttpClientParams.REJECT_RELATIVE_REDIRECT, true);
162 method.execute(new HttpState(), conn);
163 Header locationHeader = method.getResponseHeader("Location");
164 assertEquals(302, method.getStatusCode());
165 assertEquals("/oldfile", method.getPath());
166 }
167
168 public void testRedirectBogusLocationStrict() throws Exception {
169 addRedirectResponse("xxx://bogus");
170 addOkResponse();
171 conn.open();
172
173 HttpMethod method = new SimpleHttpMethod("/oldfile");
174 method.setFollowRedirects(true);
175 client.getParams().setBooleanParameter(
176 HttpClientParams.REJECT_RELATIVE_REDIRECT, false);
177 method.execute(new HttpState(), conn);
178 Header locationHeader = method.getResponseHeader("Location");
179 assertEquals(302, method.getStatusCode());
180 assertEquals("/oldfile", method.getPath());
181 }
182
183 public void testRedirectDifferentHost() throws Exception {
184
185 addRedirectResponse("http://newhost/newfile");
186 addOkResponse();
187
188 HttpMethod method = new SimpleHttpMethod("/oldfile");
189 method.setFollowRedirects(true);
190 client.executeMethod(method);
191 assertEquals(200, method.getStatusCode());
192 assertEquals("/newfile", method.getPath());
193 assertEquals("newhost", method.getRequestHeader("host").getValue());
194 }
195
196 public void testRedirectDifferentPort() throws Exception {
197 conn = new SimpleHttpConnection("oldhost", 80);
198 addRedirectResponse("http://oldhost:8080/newfile");
199 addOkResponse();
200 conn.open();
201
202 HttpMethod method = new SimpleHttpMethod("/oldfile");
203 method.setFollowRedirects(true);
204 method.execute(new HttpState(), conn);
205 Header locationHeader = method.getResponseHeader("Location");
206 assertEquals(302, method.getStatusCode());
207 assertEquals("/oldfile", method.getPath());
208 }
209
210
211 public void testRedirectDifferentProtocol() throws Exception {
212 conn = new SimpleHttpConnection("oldhost", 80);
213 addRedirectResponse("https://oldhost:80/newfile");
214 addOkResponse();
215 conn.open();
216
217 HttpMethod method = new SimpleHttpMethod("/oldfile");
218 method.setFollowRedirects(true);
219 method.execute(new HttpState(), conn);
220 Header locationHeader = method.getResponseHeader("Location");
221 assertEquals(302, method.getStatusCode());
222 assertEquals("/oldfile", method.getPath());
223 }
224
225
226 public void testRedirectWithCookie() throws Exception {
227 addRedirectResponse("http://localhost/newfile");
228 addOkResponse();
229 conn.open();
230
231 client.getState().addCookie(new Cookie("localhost", "name", "value", "/", -1, false));
232
233 HttpMethod method = new SimpleHttpMethod("/oldfile");
234 method.setFollowRedirects(true);
235 client.executeMethod(method);
236 Header locationHeader = method.getResponseHeader("Location");
237 assertEquals(200, method.getStatusCode());
238
239 Header[] headers = method.getRequestHeaders();
240 int cookiecount = 0;
241 for (int i = 0; i < headers.length; i++) {
242 if ("cookie".equalsIgnoreCase(headers[i].getName())) {
243 ++cookiecount;
244 }
245 }
246 assertEquals("There can only be one (cookie)", 1, cookiecount);
247 }
248
249 }