1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }