Skip to content

Commit 6b0751f

Browse files
author
Stephen Powis
committed
Add integration tests over HttpClientRestClient
1 parent 146a8fb commit 6b0751f

File tree

7 files changed

+404
-0
lines changed

7 files changed

+404
-0
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@
161161
<version>2.6</version>
162162
<scope>test</scope>
163163
</dependency>
164+
165+
<!-- Test Http/Https Client -->
166+
<dependency>
167+
<groupId>org.eclipse.jetty</groupId>
168+
<artifactId>jetty-server</artifactId>
169+
<version>9.4.14.v20181114</version>
170+
<scope>test</scope>
171+
</dependency>
164172
</dependencies>
165173

166174
<build>
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package testserver;
2+
3+
public class RequestProperties {
4+
private final String url;
5+
private final String requestBody;
6+
private final String requestMethod;
7+
8+
public RequestProperties(
9+
final String url,
10+
final String requestBody,
11+
final String requestMethod
12+
) {
13+
this.url = url;
14+
this.requestBody = requestBody;
15+
this.requestMethod = requestMethod;
16+
}
17+
18+
public String getUrl() {
19+
return url;
20+
}
21+
22+
public String getRequestBody() {
23+
return requestBody;
24+
}
25+
26+
public String getRequestMethod() {
27+
return requestMethod;
28+
}
29+
}

0 commit comments

Comments
 (0)