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 package org.apache.http.impl.client;
28
29 import java.net.URI;
30 import java.util.List;
31
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpEntityEnclosingRequest;
34 import org.apache.http.HttpHost;
35 import org.apache.http.HttpResponse;
36 import org.apache.http.HttpStatus;
37 import org.apache.http.HttpVersion;
38 import org.apache.http.ProtocolException;
39 import org.apache.http.client.config.RequestConfig;
40 import org.apache.http.client.methods.HttpDelete;
41 import org.apache.http.client.methods.HttpGet;
42 import org.apache.http.client.methods.HttpHead;
43 import org.apache.http.client.methods.HttpPost;
44 import org.apache.http.client.methods.HttpPut;
45 import org.apache.http.client.methods.HttpTrace;
46 import org.apache.http.client.methods.HttpUriRequest;
47 import org.apache.http.client.protocol.HttpClientContext;
48 import org.apache.http.entity.BasicHttpEntity;
49 import org.apache.http.message.BasicHttpResponse;
50 import org.apache.http.protocol.BasicHttpContext;
51 import org.apache.http.protocol.HttpContext;
52 import org.apache.http.protocol.HttpCoreContext;
53 import org.junit.Assert;
54 import org.junit.Test;
55
56 public class TestDefaultRedirectStrategy {
57
58 @Test
59 public void testIsRedirectable() {
60 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
61 Assert.assertTrue(redirectStrategy.isRedirectable(HttpGet.METHOD_NAME));
62 Assert.assertTrue(redirectStrategy.isRedirectable(HttpHead.METHOD_NAME));
63 Assert.assertFalse(redirectStrategy.isRedirectable(HttpPut.METHOD_NAME));
64 Assert.assertFalse(redirectStrategy.isRedirectable(HttpPost.METHOD_NAME));
65 Assert.assertFalse(redirectStrategy.isRedirectable(HttpDelete.METHOD_NAME));
66 }
67
68 @Test
69 public void testIsRedirectedMovedTemporary() throws Exception {
70 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
71 final HttpClientContext context = HttpClientContext.create();
72 final HttpGet httpget = new HttpGet("http://localhost/");
73 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
74 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
75 response.addHeader("Location", "http://localhost/stuff");
76 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
77 final HttpPost httppost = new HttpPost("http://localhost/");
78 Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
79 }
80
81 @Test
82 public void testIsRedirectedMovedTemporaryNoLocation() throws Exception {
83 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
84 final HttpClientContext context = HttpClientContext.create();
85 final HttpGet httpget = new HttpGet("http://localhost/");
86 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
87 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
88 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
89 }
90
91 @Test
92 public void testIsRedirectedMovedPermanently() throws Exception {
93 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
94 final HttpClientContext context = HttpClientContext.create();
95 final HttpGet httpget = new HttpGet("http://localhost/");
96 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
97 HttpStatus.SC_MOVED_PERMANENTLY, "Redirect");
98 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
99 final HttpPost httppost = new HttpPost("http://localhost/");
100 Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
101 }
102
103 @Test
104 public void testIsRedirectedTemporaryRedirect() throws Exception {
105 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
106 final HttpClientContext context = HttpClientContext.create();
107 final HttpGet httpget = new HttpGet("http://localhost/");
108 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
109 HttpStatus.SC_TEMPORARY_REDIRECT, "Redirect");
110 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
111 final HttpPost httppost = new HttpPost("http://localhost/");
112 Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
113 }
114
115 @Test
116 public void testIsRedirectedPermanentRedirect() throws Exception {
117 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
118 final HttpClientContext context = HttpClientContext.create();
119 final HttpGet httpget = new HttpGet("http://localhost/");
120 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
121 DefaultRedirectStrategy.SC_PERMANENT_REDIRECT, "Redirect");
122 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
123 final HttpPost httppost = new HttpPost("http://localhost/");
124 Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
125 }
126
127 @Test
128 public void testIsRedirectedSeeOther() throws Exception {
129 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
130 final HttpClientContext context = HttpClientContext.create();
131 final HttpGet httpget = new HttpGet("http://localhost/");
132 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
133 HttpStatus.SC_SEE_OTHER, "Redirect");
134 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
135 final HttpPost httppost = new HttpPost("http://localhost/");
136 Assert.assertTrue(redirectStrategy.isRedirected(httppost, response, context));
137 }
138
139 @Test
140 public void testIsRedirectedUnknownStatus() throws Exception {
141 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
142 final HttpClientContext context = HttpClientContext.create();
143 final HttpGet httpget = new HttpGet("http://localhost/");
144 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 333, "Redirect");
145 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
146 final HttpPost httppost = new HttpPost("http://localhost/");
147 Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
148 }
149
150 @Test
151 public void testIsRedirectedInvalidInput() throws Exception {
152 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
153 final HttpClientContext context = HttpClientContext.create();
154 final HttpGet httpget = new HttpGet("http://localhost/");
155 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
156 HttpStatus.SC_SEE_OTHER, "Redirect");
157 try {
158 redirectStrategy.isRedirected(null, response, context);
159 Assert.fail("IllegalArgumentException expected");
160 } catch (final IllegalArgumentException expected) {
161 }
162 try {
163 redirectStrategy.isRedirected(httpget, null, context);
164 Assert.fail("IllegalArgumentException expected");
165 } catch (final IllegalArgumentException expected) {
166 }
167 }
168
169 @Test
170 public void testGetLocationUri() throws Exception {
171 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
172 final HttpClientContext context = HttpClientContext.create();
173 final HttpGet httpget = new HttpGet("http://localhost/");
174 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
175 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
176 response.addHeader("Location", "http://localhost/stuff");
177 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
178 Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
179 }
180
181 @Test(expected=ProtocolException.class)
182 public void testGetLocationUriMissingHeader() throws Exception {
183 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
184 final HttpClientContext context = HttpClientContext.create();
185 final HttpGet httpget = new HttpGet("http://localhost/");
186 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
187 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
188 redirectStrategy.getLocationURI(httpget, response, context);
189 }
190
191 @Test(expected=ProtocolException.class)
192 public void testGetLocationUriInvalidLocation() throws Exception {
193 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
194 final HttpClientContext context = HttpClientContext.create();
195 final HttpGet httpget = new HttpGet("http://localhost/");
196 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
197 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
198 response.addHeader("Location", "http://localhost/not valid");
199 redirectStrategy.getLocationURI(httpget, response, context);
200 }
201
202 @Test
203 public void testGetLocationUriRelative() throws Exception {
204 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
205 final HttpClientContext context = HttpClientContext.create();
206 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
207 final HttpGet httpget = new HttpGet("http://localhost/");
208 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
209 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
210 response.addHeader("Location", "/stuff");
211 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
212 Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
213 }
214
215 @Test(expected=IllegalStateException.class)
216 public void testGetLocationUriRelativeMissingTargetHost() throws Exception {
217 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
218 final HttpClientContext context = HttpClientContext.create();
219 final HttpGet httpget = new HttpGet("http://localhost/");
220 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
221 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
222 response.addHeader("Location", "/stuff");
223 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
224 Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
225 }
226
227 @Test
228 public void testGetLocationUriRelativeWithFragment() throws Exception {
229 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
230 final HttpClientContext context = HttpClientContext.create();
231 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
232 final HttpGet httpget = new HttpGet("http://localhost/");
233 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
234 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
235 response.addHeader("Location", "/stuff#fragment");
236 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
237 Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
238 }
239
240 @Test
241 public void testGetLocationUriAbsoluteWithFragment() throws Exception {
242 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
243 final HttpClientContext context = HttpClientContext.create();
244 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
245 final HttpGet httpget = new HttpGet("http://localhost/");
246 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
247 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
248 response.addHeader("Location", "http://localhost/stuff#fragment");
249 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
250 Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
251 }
252
253 @Test
254 public void testGetLocationUriNormalized() throws Exception {
255 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
256 final HttpClientContext context = HttpClientContext.create();
257 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
258 final HttpGet httpget = new HttpGet("http://localhost/");
259 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
260 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
261 response.addHeader("Location", "http://localhost/././stuff/../morestuff");
262 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
263 Assert.assertEquals(URI.create("http://localhost/morestuff"), uri);
264 }
265
266 @Test(expected=ProtocolException.class)
267 public void testGetLocationUriRelativeLocationNotAllowed() throws Exception {
268 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
269 final HttpClientContext context = HttpClientContext.create();
270 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
271 final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(false).build();
272 context.setRequestConfig(config);
273
274 final HttpGet httpget = new HttpGet("http://localhost/");
275 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
276 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
277 response.addHeader("Location", "/stuff");
278 redirectStrategy.getLocationURI(httpget, response, context);
279 }
280
281 @Test
282 public void testGetLocationUriAllowCircularRedirects() throws Exception {
283 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
284 final HttpClientContext context = HttpClientContext.create();
285 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
286 final RequestConfig config = RequestConfig.custom().setCircularRedirectsAllowed(true).build();
287 context.setRequestConfig(config);
288 final URI uri1 = URI.create("http://localhost/stuff1");
289 final URI uri2 = URI.create("http://localhost/stuff2");
290 final URI uri3 = URI.create("http://localhost/stuff3");
291 final HttpGet httpget1 = new HttpGet("http://localhost/");
292 final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
293 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
294 response1.addHeader("Location", uri1.toASCIIString());
295 final HttpGet httpget2 = new HttpGet(uri1.toASCIIString());
296 final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
297 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
298 response2.addHeader("Location", uri2.toASCIIString());
299 final HttpGet httpget3 = new HttpGet(uri2.toASCIIString());
300 final HttpResponse response3 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
301 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
302 response3.addHeader("Location", uri3.toASCIIString());
303 Assert.assertEquals(uri1, redirectStrategy.getLocationURI(httpget1, response1, context));
304 Assert.assertEquals(uri2, redirectStrategy.getLocationURI(httpget2, response2, context));
305 Assert.assertEquals(uri3, redirectStrategy.getLocationURI(httpget3, response3, context));
306
307 final List<URI> uris = context.getRedirectLocations();
308 Assert.assertNotNull(uris);
309 Assert.assertTrue(uris.contains(uri1));
310 Assert.assertTrue(uris.contains(uri2));
311 Assert.assertTrue(uris.contains(uri3));
312 Assert.assertEquals(3, uris.size());
313 Assert.assertEquals(uri1, uris.get(0));
314 Assert.assertEquals(uri2, uris.get(1));
315 Assert.assertEquals(uri3, uris.get(2));
316 }
317
318 @Test(expected=ProtocolException.class)
319 public void testGetLocationUriDisallowCircularRedirects() throws Exception {
320 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
321 final HttpClientContext context = HttpClientContext.create();
322 context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
323 final HttpGet httpget = new HttpGet("http://localhost/stuff");
324 final RequestConfig config = RequestConfig.custom().setCircularRedirectsAllowed(false).build();
325 context.setRequestConfig(config);
326 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
327 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
328 response.addHeader("Location", "http://localhost/stuff");
329 final URI uri = URI.create("http://localhost/stuff");
330 Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
331 redirectStrategy.getLocationURI(httpget, response, context);
332 }
333
334 @Test
335 public void testGetLocationUriInvalidInput() throws Exception {
336 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
337 final HttpClientContext context = HttpClientContext.create();
338 final HttpGet httpget = new HttpGet("http://localhost/");
339 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
340 HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
341 response.addHeader("Location", "http://localhost/stuff");
342 try {
343 redirectStrategy.getLocationURI(null, response, context);
344 Assert.fail("IllegalArgumentException expected");
345 } catch (final IllegalArgumentException expected) {
346 }
347 try {
348 redirectStrategy.getLocationURI(httpget, null, context);
349 Assert.fail("IllegalArgumentException expected");
350 } catch (final IllegalArgumentException expected) {
351 }
352 try {
353 redirectStrategy.getLocationURI(httpget, response, null);
354 Assert.fail("IllegalArgumentException expected");
355 } catch (final IllegalArgumentException expected) {
356 }
357 }
358
359 @Test
360 public void testGetRedirectRequest() throws Exception {
361 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
362 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
363 HttpStatus.SC_SEE_OTHER, "Redirect");
364 response.addHeader("Location", "http://localhost/stuff");
365 final HttpContext context1 = new BasicHttpContext();
366 final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
367 new HttpGet("http://localhost/"), response, context1);
368 Assert.assertEquals("GET", redirect1.getMethod());
369 final HttpContext context2 = new BasicHttpContext();
370 final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
371 new HttpPost("http://localhost/"), response, context2);
372 Assert.assertEquals("GET", redirect2.getMethod());
373 final HttpContext context3 = new BasicHttpContext();
374 final HttpUriRequest redirect3 = redirectStrategy.getRedirect(
375 new HttpHead("http://localhost/"), response, context3);
376 Assert.assertEquals("HEAD", redirect3.getMethod());
377 }
378
379 @Test
380 public void testGetRedirectRequestForTemporaryRedirect() throws Exception {
381 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
382 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
383 HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect");
384 response.addHeader("Location", "http://localhost/stuff");
385 final HttpContext context1 = new BasicHttpContext();
386 final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
387 new HttpTrace("http://localhost/"), response, context1);
388 Assert.assertEquals("TRACE", redirect1.getMethod());
389 final HttpContext context2 = new BasicHttpContext();
390 final HttpPost httppost = new HttpPost("http://localhost/");
391 final HttpEntity entity = new BasicHttpEntity();
392 httppost.setEntity(entity);
393 final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
394 httppost, response, context2);
395 Assert.assertEquals("POST", redirect2.getMethod());
396 Assert.assertTrue(redirect2 instanceof HttpEntityEnclosingRequest);
397 Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
398 }
399
400 @Test
401 public void testGetRedirectRequestForPermanentRedirect() throws Exception {
402 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
403 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
404 DefaultRedirectStrategy.SC_PERMANENT_REDIRECT, "Permanent Redirect");
405 response.addHeader("Location", "http://localhost/stuff");
406 final HttpContext context1 = new BasicHttpContext();
407 final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
408 new HttpTrace("http://localhost/"), response, context1);
409 Assert.assertEquals("TRACE", redirect1.getMethod());
410 final HttpContext context2 = new BasicHttpContext();
411 final HttpPost httppost = new HttpPost("http://localhost/");
412 final HttpEntity entity = new BasicHttpEntity();
413 httppost.setEntity(entity);
414 final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
415 httppost, response, context2);
416 Assert.assertEquals("POST", redirect2.getMethod());
417 Assert.assertTrue(redirect2 instanceof HttpEntityEnclosingRequest);
418 Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
419 }
420
421 @Test(expected=ProtocolException.class)
422 public void testCreateLocationURIInvalid() throws Exception {
423 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
424 redirectStrategy.createLocationURI(":::::::");
425 }
426
427 @Test
428 public void testWithoutNormalize() throws Exception {
429 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
430 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
431 HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect");
432 response.addHeader("Location", "http://somewhere.com//foo");
433 final HttpClientContext context1 = new HttpClientContext(new BasicHttpContext());
434 context1.setRequestConfig(RequestConfig.custom().setNormalizeUri(false).build());
435 Assert.assertEquals("http://somewhere.com//foo", redirectStrategy.getRedirect(
436 new HttpGet("http://localhost/stuff"), response, context1).getURI().toASCIIString());
437 }
438 }