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.sync;
28  
29  import org.apache.hc.client5.http.classic.methods.HttpGet;
30  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
31  import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
32  import org.apache.hc.client5.testing.classic.RandomHandler;
33  import org.apache.hc.client5.testing.sync.extension.TestClientResources;
34  import org.apache.hc.core5.http.HttpHost;
35  import org.apache.hc.core5.http.URIScheme;
36  import org.apache.hc.core5.http.io.entity.EntityUtils;
37  import org.apache.hc.core5.testing.classic.ClassicTestServer;
38  import org.apache.hc.core5.util.Timeout;
39  import org.junit.jupiter.api.Assertions;
40  import org.junit.jupiter.api.Test;
41  import org.junit.jupiter.api.extension.RegisterExtension;
42  
43  public class TestBasicConnectionManager {
44  
45      public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
46  
47      @RegisterExtension
48      private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
49  
50      @Test
51      public void testBasics() throws Exception {
52          final ClassicTestServer server = testResources.startServer(null, null, null);
53          server.registerHandler("/random/*", new RandomHandler());
54          final HttpHost target = testResources.targetHost();
55  
56          final CloseableHttpClient client = testResources.startClient(builder -> builder
57                  .setConnectionManager(new BasicHttpClientConnectionManager())
58          );
59  
60          final HttpGet get = new HttpGet("/random/1024");
61          client.execute(target, get, response -> {
62              Assertions.assertEquals(200, response.getCode());
63              EntityUtils.consume(response.getEntity());
64              return null;
65          });
66      }
67  
68      @Test
69      public void testConnectionStillInUse() throws Exception {
70          final ClassicTestServer server = testResources.startServer(null, null, null);
71          server.registerHandler("/random/*", new RandomHandler());
72          final HttpHost target = testResources.targetHost();
73  
74          final CloseableHttpClient client = testResources.startClient(builder -> builder
75                  .setConnectionManager(new BasicHttpClientConnectionManager())
76          );
77  
78          final HttpGet get1 = new HttpGet("/random/1024");
79          client.executeOpen(target, get1, null);
80          final HttpGet get2 = new HttpGet("/random/1024");
81          Assertions.assertThrows(IllegalStateException.class, () ->
82                  client.executeOpen(target, get2, null));
83      }
84  
85  }