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.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 class TestBasicConnectionManager extends AbstractIntegrationTestBase {
41
42 public TestBasicConnectionManager() {
43 super(URIScheme.HTTP, ClientProtocolLevel.STANDARD);
44 }
45
46 @Test
47 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 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 }