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.jupiter.api.Assertions;
41 import org.junit.jupiter.api.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 Assertions.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
52 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
53 Assertions.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 Assertions.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 Assertions.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
72 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
73 Assertions.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 Assertions.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
83 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
84 Assertions.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 Assertions.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
94 response.setHeader(HttpHeaders.LOCATION, "http://localhost/blah");
95 Assertions.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 Assertions.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
105 final HttpPost httppost = new HttpPost("http://localhost/");
106 Assertions.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 Assertions.assertThrows(NullPointerException.class, () ->
116 redirectStrategy.isRedirected(null, response, context));
117 Assertions.assertThrows(NullPointerException.class, () ->
118 redirectStrategy.isRedirected(httpget, null, context));
119 }
120
121 @Test
122 public void testGetLocationUri() throws Exception {
123 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
124 final HttpClientContext context = HttpClientContext.create();
125 final HttpGet httpget = new HttpGet("http://localhost/");
126 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
127 response.addHeader("Location", "http://localhost/stuff");
128 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
129 Assertions.assertEquals(URI.create("http://localhost/stuff"), uri);
130 }
131
132 @Test
133 public void testGetLocationUriMissingHeader() throws Exception {
134 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
135 final HttpClientContext context = HttpClientContext.create();
136 final HttpGet httpget = new HttpGet("http://localhost/");
137 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
138 Assertions.assertThrows(HttpException.class, () ->
139 redirectStrategy.getLocationURI(httpget, response, context));
140 }
141
142 @Test
143 public void testGetLocationUriInvalidLocation() throws Exception {
144 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
145 final HttpClientContext context = HttpClientContext.create();
146 final HttpGet httpget = new HttpGet("http://localhost/");
147 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
148 response.addHeader("Location", "http://localhost/not valid");
149 Assertions.assertThrows(ProtocolException.class, () ->
150 redirectStrategy.getLocationURI(httpget, response, context));
151 }
152
153 @Test
154 public void testGetLocationUriRelative() throws Exception {
155 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
156 final HttpClientContext context = HttpClientContext.create();
157 final HttpGet httpget = new HttpGet("http://localhost/");
158 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
159 response.addHeader("Location", "/stuff");
160 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
161 Assertions.assertEquals(URI.create("http://localhost/stuff"), uri);
162 }
163
164 @Test
165 public void testGetLocationUriRelativeWithFragment() throws Exception {
166 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
167 final HttpClientContext context = HttpClientContext.create();
168 final HttpGet httpget = new HttpGet("http://localhost/");
169 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
170 response.addHeader("Location", "/stuff#fragment");
171 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
172 Assertions.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
173 }
174
175 @Test
176 public void testGetLocationUriAbsoluteWithFragment() throws Exception {
177 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
178 final HttpClientContext context = HttpClientContext.create();
179 final HttpGet httpget = new HttpGet("http://localhost/");
180 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
181 response.addHeader("Location", "http://localhost/stuff#fragment");
182 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
183 Assertions.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
184 }
185
186 @Test
187 public void testGetLocationUriNormalized() throws Exception {
188 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
189 final HttpClientContext context = HttpClientContext.create();
190 final HttpGet httpget = new HttpGet("http://localhost/");
191 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
192 response.addHeader("Location", "http://localhost/././stuff/../morestuff");
193 final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
194 Assertions.assertEquals(URI.create("http://localhost/morestuff"), uri);
195 }
196
197 @Test
198 public void testGetLocationUriInvalidInput() throws Exception {
199 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
200 final HttpClientContext context = HttpClientContext.create();
201 final HttpGet httpget = new HttpGet("http://localhost/");
202 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
203 response.addHeader("Location", "http://localhost/stuff");
204 Assertions.assertThrows(NullPointerException.class, () ->
205 redirectStrategy.getLocationURI(null, response, context));
206 Assertions.assertThrows(NullPointerException.class, () ->
207 redirectStrategy.getLocationURI(httpget, null, context));
208 Assertions.assertThrows(NullPointerException.class, () ->
209 redirectStrategy.getLocationURI(httpget, response, null));
210 }
211
212 @Test
213 public void testCreateLocationURI() throws Exception {
214 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
215 Assertions.assertEquals("http://blahblah/",
216 redirectStrategy.createLocationURI("http://BlahBlah").toASCIIString());
217 }
218
219 @Test
220 public void testCreateLocationURIInvalid() throws Exception {
221 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
222 Assertions.assertThrows(ProtocolException.class, () ->
223 redirectStrategy.createLocationURI(":::::::"));
224 }
225
226 @Test
227 public void testResolveRelativeLocation() throws Exception {
228 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
229 final HttpClientContext context = HttpClientContext.create();
230 final HttpGet request = new HttpGet("http://localhost/");
231 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
232 response.addHeader("Location", "/foo;bar=baz");
233
234 final URI locationURI = redirectStrategy.getLocationURI(request, response, context);
235
236 Assertions.assertEquals(URI.create("http://localhost/foo;bar=baz"), locationURI);
237 }
238
239 @Test
240 public void testUseAbsoluteLocation() throws Exception {
241 final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
242 final HttpClientContext context = HttpClientContext.create();
243 final HttpGet request = new HttpGet("http://localhost/");
244 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
245 response.addHeader("Location", "http://localhost/foo;bar=baz");
246
247 final URI locationURI = redirectStrategy.getLocationURI(request, response, context);
248
249 Assertions.assertEquals(URI.create("http://localhost/foo;bar=baz"), locationURI);
250 }
251
252 }