View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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 }