1+ package org .sourcelab .kafka .connect .apiclient .rest ;
2+
3+ import org .junit .BeforeClass ;
4+ import org .junit .Test ;
5+ import org .sourcelab .kafka .connect .apiclient .Configuration ;
6+ import org .sourcelab .kafka .connect .apiclient .request .Request ;
7+ import org .sourcelab .kafka .connect .apiclient .request .RequestMethod ;
8+ import testserver .TestHttpServer ;
9+
10+ import java .io .File ;
11+
12+ import static org .junit .Assert .assertEquals ;
13+
14+ public class HttpClientRestClientTest {
15+
16+ private static final int HTTP_PORT = 10880 ;
17+ private static final int HTTPS_PORT = 10881 ;
18+ private static final String RESPONSE_DATA = "My Test Response" ;
19+
20+ private static String KEYSTORE_PATH ;
21+ private static String TRUSTSTORE_PATH ;
22+
23+ private static final String DUMMY_PASSWORD = "password" ;
24+
25+ @ BeforeClass
26+ public static void setup () {
27+ KEYSTORE_PATH = HttpClientRestClientTest .class
28+ .getClassLoader ()
29+ .getResource ("certificates/server.keystore.jks" )
30+ .getFile ();
31+
32+ TRUSTSTORE_PATH = HttpClientRestClientTest .class
33+ .getClassLoader ()
34+ .getResource ("certificates/server.truststore.jks" )
35+ .getFile ();
36+ }
37+
38+ /**
39+ * Test against Http server.
40+ */
41+ @ Test
42+ public void doHttpTest () throws Exception {
43+
44+ try (final TestHttpServer httpServer = new TestHttpServer ()
45+ .withHttp (HTTP_PORT )
46+ .withMockData (RESPONSE_DATA )
47+ .start ()
48+ ) {
49+
50+ // Create client
51+ final Configuration configuration = new Configuration ("http://localhost:" + HTTP_PORT );
52+ final HttpClientRestClient restClient = new HttpClientRestClient ();
53+ restClient .init (configuration );
54+
55+ // Make request
56+ final RestResponse result = restClient .submitRequest (new DummyRequest ());
57+
58+ // Validate response.
59+ assertEquals (RESPONSE_DATA , result .getResponseStr ());
60+ }
61+ }
62+
63+ /**
64+ * Test against Https server.
65+ */
66+ @ Test
67+ public void doHttps_noClientValidation_disableCertificateValidation_Test () throws Exception {
68+
69+ try (final TestHttpServer httpServer = new TestHttpServer ()
70+ .withHttps (KEYSTORE_PATH , DUMMY_PASSWORD , HTTPS_PORT )
71+ .withMockData (RESPONSE_DATA )
72+ .start ()
73+ ) {
74+
75+ final Configuration configuration = new Configuration ("https://localhost:" + HTTPS_PORT )
76+ .useInsecureSslCertificates ();
77+
78+ final HttpClientRestClient restClient = new HttpClientRestClient ();
79+ restClient .init (configuration );
80+
81+ final RestResponse result = restClient .submitRequest (new DummyRequest ());
82+ assertEquals (RESPONSE_DATA , result .getResponseStr ());
83+ }
84+ }
85+
86+ /**
87+ * Test against Https server with server certificate validation.
88+ */
89+ @ Test
90+ public void doHttps_noClientValidation_withCertificateValidation_Test () throws Exception {
91+
92+ try (final TestHttpServer httpServer = new TestHttpServer ()
93+ .withHttps (KEYSTORE_PATH , DUMMY_PASSWORD , HTTPS_PORT )
94+ .withMockData (RESPONSE_DATA )
95+ .start ()
96+ ) {
97+
98+ final Configuration configuration = new Configuration ("https://localhost:" + HTTPS_PORT )
99+ .useTrustStore (new File (TRUSTSTORE_PATH ), DUMMY_PASSWORD );
100+
101+ final HttpClientRestClient restClient = new HttpClientRestClient ();
102+ restClient .init (configuration );
103+
104+ final RestResponse result = restClient .submitRequest (new DummyRequest ());
105+ assertEquals (RESPONSE_DATA , result .getResponseStr ());
106+ }
107+ }
108+
109+ /**
110+ * Test against Https server with server certificate and client validation.
111+ */
112+ @ Test
113+ public void doHttps_withClientValidation_withCertificateValidation_Test () throws Exception {
114+
115+ try (final TestHttpServer httpServer = new TestHttpServer ()
116+ .withHttps (KEYSTORE_PATH , DUMMY_PASSWORD , HTTPS_PORT )
117+ .withValidateClientCertificate (TRUSTSTORE_PATH , DUMMY_PASSWORD )
118+ .withMockData (RESPONSE_DATA )
119+ .start ()
120+ ) {
121+
122+ final Configuration configuration = new Configuration ("https://localhost:" + HTTPS_PORT )
123+ .useTrustStore (new File (TRUSTSTORE_PATH ), DUMMY_PASSWORD )
124+ .useKeyStore (new File (KEYSTORE_PATH ), DUMMY_PASSWORD );
125+
126+ final HttpClientRestClient restClient = new HttpClientRestClient ();
127+ restClient .init (configuration );
128+
129+ final RestResponse result = restClient .submitRequest (new DummyRequest ());
130+ assertEquals (RESPONSE_DATA , result .getResponseStr ());
131+ }
132+ }
133+
134+ /**
135+ * Represents a dummy request.
136+ */
137+ private static class DummyRequest implements Request {
138+ private final String endPoint ;
139+ private final RequestMethod requestMethod ;
140+ private final String requestBody ;
141+
142+ public DummyRequest () {
143+ this ("/" , RequestMethod .GET , null );
144+ }
145+
146+ public DummyRequest (final String endPoint , final RequestMethod requestMethod , final String requestBody ) {
147+ this .endPoint = endPoint ;
148+ this .requestMethod = requestMethod ;
149+ this .requestBody = requestBody ;
150+ }
151+
152+ @ Override
153+ public String getApiEndpoint () {
154+ return endPoint ;
155+ }
156+
157+ @ Override
158+ public RequestMethod getRequestMethod () {
159+ return requestMethod ;
160+ }
161+
162+ @ Override
163+ public Object getRequestBody () {
164+ return requestBody ;
165+ }
166+
167+ @ Override
168+ public Object parseResponse (final String responseStr ) {
169+ return responseStr ;
170+ }
171+ }
172+ }
0 commit comments