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.hc.client5.http.impl;
28
29 import java.net.URI;
30
31 import org.apache.hc.client5.http.classic.methods.HttpGet;
32 import org.apache.hc.client5.http.classic.methods.HttpPost;
33 import org.apache.hc.client5.http.protocol.HttpClientContext;
34 import org.apache.hc.core5.http.HttpException;
35 import org.apache.hc.core5.http.HttpHeaders;
36 import org.apache.hc.core5.http.HttpResponse;
37 import org.apache.hc.core5.http.HttpStatus;
38 import org.apache.hc.core5.http.ProtocolException;
39 import org.apache.hc.core5.http.message.BasicHttpResponse;
40 import org.junit.Assert;
41 import org.junit.Test;
42
43 public class TestDefaultRedirectStrategy {
44
45 @Test
46 public void testIsRedirectedMovedTemporary() throws Exception {
47 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
48 final HttpClientContext context = HttpClientContext.create();
49 final HttpGet httpget = new HttpGet("http://localhost/");
50 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
51 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
52 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
53 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
54 }
55
56 @Test
57 public void testIsRedirectedMovedTemporaryNoLocation() throws Exception {
58 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
59 final HttpClientContext context = HttpClientContext.create();
60 final HttpGet httpget = new HttpGet("http://localhost/");
61 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
62 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
63 }
64
65 @Test
66 public void testIsRedirectedMovedPermanently() throws Exception {
67 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
68 final HttpClientContext context = HttpClientContext.create();
69 final HttpGet httpget = new HttpGet("http://localhost/");
70 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_PERMANENTLY, "Redirect");
71 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
72 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
73 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
74 }
75
76 @Test
77 public void testIsRedirectedTemporaryRedirect() throws Exception {
78 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
79 final HttpClientContext context = HttpClientContext.create();
80 final HttpGet httpget = new HttpGet("http://localhost/");
81 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_TEMPORARY_REDIRECT, "Redirect");
82 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
83 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
84 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
85 }
86
87 @Test
88 public void testIsRedirectedSeeOther() throws Exception {
89 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
90 final HttpClientContext context = HttpClientContext.create();
91 final HttpGet httpget = new HttpGet("http://localhost/");
92 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SEE_OTHER, "Redirect");
93 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
94 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
95 Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
96 }
97
98 @Test
99 public void testIsRedirectedUnknownStatus() throws Exception {
100 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
101 final HttpClientContext context = HttpClientContext.create();
102 final HttpGet httpget = new HttpGet("http://localhost/");
103 final HttpResponse response = new BasicHttpResponse(333, "Redirect");
104 Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
105 final HttpPost httppost = new HttpPost("http://localhost/");
106 Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
107 }
108
109 @Test
110 public void testIsRedirectedInvalidInput() throws Exception {
111 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
112 final HttpClientContext context = HttpClientContext.create();
113 final HttpGet httpget = new HttpGet("http://localhost/");
114 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SEE_OTHER, "Redirect");
115 try {
116 redirectStrategy.isRedirected(null, response, context);
117 Assert.fail("NullPointerException expected");
118 } catch (final NullPointerException expected) {
119 }
120 try {
121 redirectStrategy.isRedirected(httpget, null, context);
122 Assert.fail("NullPointerException expected");
123 } catch (final NullPointerException expected) {
124 }
125 }
126
127 @Test
128 public void testGetLocationUri() 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(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
133 response.addHeader("Location", "http://localhost/stuff");
134 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
135 Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
136 }
137
138 @Test(expected=HttpException.class)
139 public void testGetLocationUriMissingHeader() throws Exception {
140 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
141 final HttpClientContext context = HttpClientContext.create();
142 final HttpGet httpget = new HttpGet("http://localhost/");
143 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
144 redirectStrategy.getLocationURI(httpget, response, context);
145 }
146
147 @Test(expected=ProtocolException.class)
148 public void testGetLocationUriInvalidLocation() throws Exception {
149 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
150 final HttpClientContext context = HttpClientContext.create();
151 final HttpGet httpget = new HttpGet("http://localhost/");
152 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
153 response.addHeader("Location", "http://localhost/not valid");
154 redirectStrategy.getLocationURI(httpget, response, context);
155 }
156
157 @Test
158 public void testGetLocationUriRelative() throws Exception {
159 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
160 final HttpClientContext context = HttpClientContext.create();
161 final HttpGet httpget = new HttpGet("http://localhost/");
162 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
163 response.addHeader("Location", "/stuff");
164 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
165 Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
166 }
167
168 @Test
169 public void testGetLocationUriRelativeWithFragment() throws Exception {
170 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
171 final HttpClientContext context = HttpClientContext.create();
172 final HttpGet httpget = new HttpGet("http://localhost/");
173 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
174 response.addHeader("Location", "/stuff#fragment");
175 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
176 Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
177 }
178
179 @Test
180 public void testGetLocationUriAbsoluteWithFragment() throws Exception {
181 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
182 final HttpClientContext context = HttpClientContext.create();
183 final HttpGet httpget = new HttpGet("http://localhost/");
184 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
185 response.addHeader("Location", "http://localhost/stuff#fragment");
186 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
187 Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
188 }
189
190 @Test
191 public void testGetLocationUriNormalized() throws Exception {
192 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
193 final HttpClientContext context = HttpClientContext.create();
194 final HttpGet httpget = new HttpGet("http://localhost/");
195 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
196 response.addHeader("Location", "http://localhost/././stuff/../morestuff");
197 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
198 Assert.assertEquals(URI.create("http://localhost/morestuff"), uri);
199 }
200
201 @Test
202 public void testGetLocationUriInvalidInput() throws Exception {
203 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
204 final HttpClientContext context = HttpClientContext.create();
205 final HttpGet httpget = new HttpGet("http://localhost/");
206 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
207 response.addHeader("Location", "http://localhost/stuff");
208 try {
209 redirectStrategy.getLocationURI(null, response, context);
210 Assert.fail("NullPointerException expected");
211 } catch (final NullPointerException expected) {
212 }
213 try {
214 redirectStrategy.getLocationURI(httpget, null, context);
215 Assert.fail("NullPointerException expected");
216 } catch (final NullPointerException expected) {
217 }
218 try {
219 redirectStrategy.getLocationURI(httpget, response, null);
220 Assert.fail("NullPointerException expected");
221 } catch (final NullPointerException expected) {
222 }
223 }
224
225 @Test
226 public void testCreateLocationURI() throws Exception {
227 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
228 Assert.assertEquals("http://blahblah/",
229 redirectStrategy.createLocationURI("http://BlahBlah").toASCIIString());
230 }
231
232 @Test(expected=ProtocolException.class)
233 public void testCreateLocationURIInvalid() throws Exception {
234 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
235 redirectStrategy.createLocationURI(":::::::");
236 }
237
238 }