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
28 package org.apache.hc.client5.testing.sync;
29
30 import java.io.IOException;
31
32 import org.apache.hc.client5.http.config.RequestConfig;
33 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
34 import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
35 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
36 import org.apache.hc.client5.testing.SSLTestContexts;
37 import org.apache.hc.client5.testing.classic.EchoHandler;
38 import org.apache.hc.client5.testing.classic.RandomHandler;
39 import org.apache.hc.core5.function.Decorator;
40 import org.apache.hc.core5.http.HttpHost;
41 import org.apache.hc.core5.http.URIScheme;
42 import org.apache.hc.core5.http.config.Http1Config;
43 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
44 import org.apache.hc.core5.http.io.SocketConfig;
45 import org.apache.hc.core5.http.protocol.HttpProcessor;
46 import org.apache.hc.core5.io.CloseMode;
47 import org.apache.hc.core5.io.Closer;
48 import org.apache.hc.core5.testing.classic.ClassicTestServer;
49 import org.apache.hc.core5.util.Timeout;
50 import org.junit.Rule;
51 import org.junit.rules.ExternalResource;
52
53
54
55
56 public abstract class LocalServerTestBase {
57
58 public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
59 public static final Timeout LONG_TIMEOUT = Timeout.ofMinutes(3);
60
61 public LocalServerTestBase(final URIScheme scheme) {
62 this.scheme = scheme;
63 }
64
65 public LocalServerTestBase() {
66 this(URIScheme.HTTP);
67 }
68
69 protected final URIScheme scheme;
70
71 protected ClassicTestServer server;
72
73 @Rule
74 public ExternalResource serverResource = new ExternalResource() {
75
76 @Override
77 protected void before() throws Throwable {
78 server = new ClassicTestServer(
79 scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
80 SocketConfig.custom()
81 .setSoTimeout(TIMEOUT)
82 .build());
83 server.registerHandler("/echo/*", new EchoHandler());
84 server.registerHandler("/random/*", new RandomHandler());
85 }
86
87 @Override
88 protected void after() {
89 if (server != null) {
90 try {
91 server.shutdown(CloseMode.IMMEDIATE);
92 server = null;
93 } catch (final Exception ignore) {
94 }
95 }
96 }
97
98 };
99
100 protected PoolingHttpClientConnectionManager connManager;
101 protected HttpClientBuilder clientBuilder;
102 protected CloseableHttpClient httpclient;
103
104 @Rule
105 public ExternalResource clientResource = new ExternalResource() {
106
107 @Override
108 protected void before() throws Throwable {
109 connManager = new PoolingHttpClientConnectionManager();
110 connManager.setDefaultSocketConfig(SocketConfig.custom()
111 .setSoTimeout(TIMEOUT)
112 .build());
113 clientBuilder = HttpClientBuilder.create()
114 .setDefaultRequestConfig(RequestConfig.custom()
115 .setConnectionRequestTimeout(TIMEOUT)
116 .setConnectTimeout(TIMEOUT)
117 .build())
118 .setConnectionManager(connManager);
119 }
120
121 @Override
122 protected void after() {
123 Closer.closeQuietly(httpclient);
124 httpclient = null;
125 }
126
127 };
128
129 public HttpHost start(
130 final Http1Config http1Config,
131 final HttpProcessor httpProcessor,
132 final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
133 this.server.start(http1Config, httpProcessor, handlerDecorator);
134
135 if (this.httpclient == null) {
136 this.httpclient = this.clientBuilder.build();
137 }
138
139 return new HttpHost(this.scheme.name(), "localhost", this.server.getPort());
140 }
141
142 public HttpHost start(
143 final HttpProcessor httpProcessor,
144 final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
145 return start(null, httpProcessor, handlerDecorator);
146 }
147
148 public HttpHost start() throws Exception {
149 return start(null, null, null);
150 }
151
152 }