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.core5.testing.nio.extension;
29
30 import org.apache.hc.core5.http.URIScheme;
31 import org.apache.hc.core5.reactor.IOReactorConfig;
32 import org.apache.hc.core5.testing.SSLTestContexts;
33 import org.apache.hc.core5.testing.nio.Http1TestClient;
34 import org.apache.hc.core5.testing.nio.Http1TestServer;
35 import org.apache.hc.core5.util.TimeValue;
36 import org.apache.hc.core5.util.Timeout;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.extension.AfterEachCallback;
39 import org.junit.jupiter.api.extension.BeforeEachCallback;
40 import org.junit.jupiter.api.extension.ExtensionContext;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class Http1TestResources implements BeforeEachCallback, AfterEachCallback {
45
46 private static final Logger LOG = LoggerFactory.getLogger(Http1TestResources.class);
47
48 private final URIScheme scheme;
49 private final Timeout socketTimeout;
50
51 private Http1TestServer server;
52 private Http1TestClient client;
53
54 public Http1TestResources(final URIScheme scheme, final Timeout socketTimeout) {
55 this.scheme = scheme;
56 this.socketTimeout = socketTimeout;
57 }
58
59 @Override
60 public void beforeEach(final ExtensionContext extensionContext) throws Exception {
61 LOG.debug("Starting up test server");
62 server = new Http1TestServer(
63 IOReactorConfig.custom()
64 .setSoTimeout(socketTimeout)
65 .build(),
66 scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null, null, null);
67
68 LOG.debug("Starting up test client");
69 client = new Http1TestClient(
70 IOReactorConfig.custom()
71 .setSoTimeout(socketTimeout)
72 .build(),
73 scheme == URIScheme.HTTPS ? SSLTestContexts.createClientSSLContext() : null, null, null);
74 }
75
76 @Override
77 public void afterEach(final ExtensionContext extensionContext) throws Exception {
78 LOG.debug("Shutting down test client");
79 if (client != null) {
80 client.shutdown(TimeValue.ofSeconds(5));
81 }
82
83 LOG.debug("Shutting down test server");
84 if (server != null) {
85 server.shutdown(TimeValue.ofSeconds(5));
86 }
87 }
88
89 public Http1TestClient client() {
90 Assertions.assertNotNull(client);
91 return client;
92 }
93
94 public Http1TestServer server() {
95 Assertions.assertNotNull(server);
96 return server;
97 }
98
99 }