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.testing.async;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.concurrent.Future;
32  
33  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
34  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
35  import org.apache.hc.client5.http.protocol.HttpClientContext;
36  import org.apache.hc.client5.testing.OldPathRedirectResolver;
37  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
38  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
39  import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
40  import org.apache.hc.client5.testing.redirect.Redirect;
41  import org.apache.hc.core5.http.Header;
42  import org.apache.hc.core5.http.HttpHeaders;
43  import org.apache.hc.core5.http.HttpHost;
44  import org.apache.hc.core5.http.HttpRequest;
45  import org.apache.hc.core5.http.HttpResponse;
46  import org.apache.hc.core5.http.HttpStatus;
47  import org.apache.hc.core5.http.URIScheme;
48  import org.apache.hc.core5.http.message.BasicHeader;
49  import org.junit.jupiter.api.Assertions;
50  import org.junit.jupiter.api.Test;
51  
52  /**
53   * Redirection test cases.
54   */
55  public abstract class TestHttp1AsyncRedirects extends AbstractHttpAsyncRedirectsTest {
56  
57      public TestHttp1AsyncRedirects(final URIScheme scheme) {
58          super(scheme, ClientProtocolLevel.STANDARD, ServerProtocolLevel.STANDARD);
59      }
60  
61      @Test
62      public void testBasicRedirect300NoKeepAlive() throws Exception {
63          configureServer(bootstrap -> bootstrap
64                  .register("/random/*", AsyncRandomHandler::new)
65                  .setExchangeHandlerDecorator(exchangeHandler -> new RedirectingAsyncDecorator(
66                          exchangeHandler,
67                          new OldPathRedirectResolver("/oldlocation", "/random", HttpStatus.SC_MULTIPLE_CHOICES,
68                                  Redirect.ConnControl.CLOSE))));
69          final HttpHost target = startServer();
70  
71          final TestAsyncClient client = startClient();
72  
73          final HttpClientContext context = HttpClientContext.create();
74          final Future<SimpleHttpResponse> future = client.execute(SimpleRequestBuilder.get()
75                          .setHttpHost(target)
76                          .setPath("/oldlocation/")
77                          .build(), context, null);
78          final HttpResponse response = future.get();
79          Assertions.assertNotNull(response);
80  
81          final HttpRequest request = context.getRequest();
82  
83          Assertions.assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getCode());
84          Assertions.assertEquals("/oldlocation/", request.getRequestUri());
85      }
86  
87      @Test
88      public void testBasicRedirect301NoKeepAlive() throws Exception {
89          configureServer(bootstrap -> bootstrap
90                  .register("/random/*", AsyncRandomHandler::new)
91                  .setExchangeHandlerDecorator(exchangeHandler -> new RedirectingAsyncDecorator(
92                          exchangeHandler,
93                          new OldPathRedirectResolver("/oldlocation", "/random", HttpStatus.SC_MOVED_PERMANENTLY,
94                                  Redirect.ConnControl.CLOSE))));
95          final HttpHost target = startServer();
96  
97          final TestAsyncClient client = startClient();
98  
99          final HttpClientContext context = HttpClientContext.create();
100         final Future<SimpleHttpResponse> future = client.execute(SimpleRequestBuilder.get()
101                         .setHttpHost(target)
102                         .setPath("/oldlocation/100")
103                         .build(), context, null);
104         final HttpResponse response = future.get();
105         Assertions.assertNotNull(response);
106 
107         final HttpRequest request = context.getRequest();
108 
109         Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
110         Assertions.assertEquals("/random/100", request.getRequestUri());
111         Assertions.assertEquals(target, new HttpHost(request.getScheme(), request.getAuthority()));
112     }
113 
114     @Test
115     public void testDefaultHeadersRedirect() throws Exception {
116         configureServer(bootstrap -> bootstrap
117                 .register("/random/*", AsyncRandomHandler::new)
118                 .setExchangeHandlerDecorator(exchangeHandler -> new RedirectingAsyncDecorator(
119                         exchangeHandler,
120                         new OldPathRedirectResolver("/oldlocation", "/random", HttpStatus.SC_MOVED_PERMANENTLY,
121                                 Redirect.ConnControl.CLOSE))));
122         final HttpHost target = startServer();
123 
124         final List<Header> defaultHeaders = new ArrayList<>(1);
125         defaultHeaders.add(new BasicHeader(HttpHeaders.USER_AGENT, "my-test-client"));
126         configureClient(builder -> builder
127                 .setDefaultHeaders(defaultHeaders)
128         );
129         final TestAsyncClient client = startClient();
130 
131         final HttpClientContext context = HttpClientContext.create();
132         final Future<SimpleHttpResponse> future = client.execute(SimpleRequestBuilder.get()
133                 .setHttpHost(target)
134                 .setPath("/oldlocation/123")
135                 .build(), context, null);
136         final HttpResponse response = future.get();
137         Assertions.assertNotNull(response);
138 
139         final HttpRequest request = context.getRequest();
140 
141         Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
142         Assertions.assertEquals("/random/123", request.getRequestUri());
143 
144         final Header header = request.getFirstHeader(HttpHeaders.USER_AGENT);
145         Assertions.assertEquals("my-test-client", header.getValue());
146     }
147 
148 }