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.io.BasicHttpClientConnectionManager;
31  import org.apache.hc.client5.testing.classic.RandomHandler;
32  import org.apache.hc.client5.testing.extension.sync.ClientProtocolLevel;
33  import org.apache.hc.client5.testing.extension.sync.TestClient;
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.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  public class TestBasicConnectionManager extends AbstractIntegrationTestBase {
41  
42      public TestBasicConnectionManager() {
43          super(URIScheme.HTTP, ClientProtocolLevel.STANDARD);
44      }
45  
46      @Test
47      public void testBasics() throws Exception {
48          configureServer(bootstrap -> bootstrap
49                  .register("/random/*", new RandomHandler()));
50          final HttpHost target = startServer();
51  
52          configureClient(builder -> builder
53                  .setConnectionManager(new BasicHttpClientConnectionManager()));
54          final TestClient client = client();
55  
56          final HttpGet get = new HttpGet("/random/1024");
57          client.execute(target, get, response -> {
58              Assertions.assertEquals(200, response.getCode());
59              EntityUtils.consume(response.getEntity());
60              return null;
61          });
62      }
63  
64      @Test
65      public void testConnectionStillInUse() throws Exception {
66          configureServer(bootstrap -> bootstrap
67                  .register("/random/*", new RandomHandler()));
68          final HttpHost target = startServer();
69  
70          configureClient(builder -> builder
71                  .setConnectionManager(new BasicHttpClientConnectionManager()));
72          final TestClient client = client();
73  
74          final HttpGet get1 = new HttpGet("/random/1024");
75          client.executeOpen(target, get1, null);
76          final HttpGet get2 = new HttpGet("/random/1024");
77          Assertions.assertThrows(IllegalStateException.class, () ->
78                  client.executeOpen(target, get2, null));
79      }
80  
81  }