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.http.nio.testserver;
29
30 import java.net.URL;
31
32 import javax.net.ssl.SSLContext;
33
34 import org.apache.http.impl.nio.pool.BasicNIOConnFactory;
35 import org.apache.http.nio.util.TestingSupport;
36 import org.apache.http.ssl.SSLContextBuilder;
37 import org.junit.After;
38
39
40
41
42
43 public abstract class HttpCoreNIOTestBase {
44
45 private static int JRE_LEVEL = TestingSupport.determineJRELevel();
46
47 public enum ProtocolScheme { http, https }
48
49 private final ProtocolScheme scheme;
50
51 protected HttpServerNio server;
52 protected HttpClientNio client;
53
54 public HttpCoreNIOTestBase(final ProtocolScheme scheme) {
55 this.scheme = scheme;
56 }
57
58 public HttpCoreNIOTestBase() {
59 this(ProtocolScheme.http);
60 }
61
62 public ProtocolScheme getScheme() {
63 return this.scheme;
64 }
65
66 protected SSLContext createServerSSLContext() throws Exception {
67 if (JRE_LEVEL >= 8) {
68 final URL keyStoreURL = getClass().getResource("/test-server.p12");
69 final String storePassword = "nopassword";
70 return SSLContextBuilder.create()
71 .setKeyStoreType("pkcs12")
72 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
73 .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
74 .build();
75 } else {
76 final URL keyStoreURL = getClass().getResource("/test.keystore");
77 final String storePassword = "nopassword";
78 return SSLContextBuilder.create()
79 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
80 .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
81 .build();
82 }
83 }
84
85 protected SSLContext createClientSSLContext() throws Exception {
86 if (JRE_LEVEL >= 8) {
87 final URL keyStoreURL = getClass().getResource("/test-client.p12");
88 final String storePassword = "nopassword";
89 return SSLContextBuilder.create()
90 .setKeyStoreType("pkcs12")
91 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
92 .build();
93 } else {
94 final URL keyStoreURL = getClass().getResource("/test.keystore");
95 final String storePassword = "nopassword";
96 return SSLContextBuilder.create()
97 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
98 .build();
99 }
100 }
101
102 protected ServerConnectionFactory createServerConnectionFactory() throws Exception {
103 return new ServerConnectionFactory(
104 this.scheme.equals(ProtocolScheme.https) ? createServerSSLContext() : null);
105 }
106
107 protected BasicNIOConnFactory createClientConnectionFactory() throws Exception {
108 return new BasicNIOConnFactory(
109 new ClientConnectionFactory(),
110 this.scheme.equals(ProtocolScheme.https) ? new ClientConnectionFactory(createClientSSLContext()) : null);
111
112 }
113
114 public void initServer() throws Exception {
115 this.server = new HttpServerNio();
116 this.server.setConnectionFactory(createServerConnectionFactory());
117 this.server.setTimeout(5000);
118 }
119
120 public void initClient() throws Exception {
121 this.client = new HttpClientNio(createClientConnectionFactory());
122 this.client.setTimeout(5000);
123 }
124
125 @After
126 public void shutDownClient() throws Exception {
127 if (this.client != null) {
128 this.client.shutdown();
129 this.client = null;
130 }
131 }
132
133 @After
134 public void shutDownServer() throws Exception {
135 if (this.server != null) {
136 this.server.shutdown();
137 this.server = null;
138 }
139 }
140
141 }