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 static org.hamcrest.MatcherAssert.assertThat;
30  
31  import java.util.concurrent.Future;
32  import java.util.concurrent.atomic.AtomicReference;
33  
34  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
35  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
36  import org.apache.hc.client5.http.config.TlsConfig;
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.core5.http.HttpHost;
41  import org.apache.hc.core5.http.HttpVersion;
42  import org.apache.hc.core5.http.ProtocolVersion;
43  import org.apache.hc.core5.http.URIScheme;
44  import org.apache.hc.core5.http2.HttpVersionPolicy;
45  import org.hamcrest.CoreMatchers;
46  import org.junit.jupiter.api.Test;
47  
48  public abstract class TestHttpAsyncProtocolPolicy extends AbstractIntegrationTestBase {
49  
50      private final HttpVersion version;
51  
52      public TestHttpAsyncProtocolPolicy(final URIScheme scheme, final HttpVersion version) {
53          super(scheme, ClientProtocolLevel.STANDARD, version == HttpVersion.HTTP_2 ? ServerProtocolLevel.H2_ONLY : ServerProtocolLevel.STANDARD);
54          this.version = version;
55      }
56  
57      @Test
58      public void testRequestContext() throws Exception {
59          configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new));
60          final HttpHost target = startServer();
61  
62          final AtomicReference<ProtocolVersion> versionRef = new AtomicReference<>();
63          configureClient(builder -> builder
64                  .setDefaultTlsConfig(TlsConfig.custom()
65                          .setVersionPolicy(version.greaterEquals(HttpVersion.HTTP_2) ? HttpVersionPolicy.FORCE_HTTP_2 : HttpVersionPolicy.FORCE_HTTP_1)
66                          .build())
67                  .addRequestInterceptorFirst((request, entity, context) ->
68                          versionRef.set(context.getProtocolVersion())));
69          final TestAsyncClient client = startClient();
70  
71          final Future<SimpleHttpResponse> future = client.execute(
72                  SimpleRequestBuilder.get()
73                          .setHttpHost(target)
74                          .setPath("/random/2048")
75                          .build(), null);
76          final SimpleHttpResponse response = future.get();
77          assertThat(response, CoreMatchers.notNullValue());
78          assertThat(response.getCode(), CoreMatchers.equalTo(200));
79          final String body = response.getBodyText();
80          assertThat(body, CoreMatchers.notNullValue());
81          assertThat(body.length(), CoreMatchers.equalTo(2048));
82          assertThat(versionRef.get(), CoreMatchers.equalTo(version));
83      }
84  
85  }