Skip to content

Commit 7face92

Browse files
authored
Merge pull request #75 from cdapio/feature/add-ssl-enabled-method
Added isSSLEnabled() method to NettyHttpService
2 parents 02fdbb4 + 139ae50 commit 7face92

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

src/main/java/io/cdap/http/NettyHttpService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ public String getServiceName() {
214214
return serviceName;
215215
}
216216

217+
/**
218+
* @return {@code true} if SSL is enabled for this service.
219+
*/
220+
public boolean isSSLEnabled() {
221+
return sslHandlerFactory != null;
222+
}
223+
217224
/**
218225
* Stops the HTTP service gracefully and release all resources. Same as calling {@link #stop(long, long, TimeUnit)}
219226
* with {@code 0} second quiet period and {@code 5} seconds timeout.

src/test/java/io/cdap/http/HttpServerTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
110110
};
111111

112112
protected static NettyHttpService service;
113-
protected static URI baseURI;
114113

115114
protected static NettyHttpService.Builder createBaseNettyHttpServiceBuilder() {
116115
return NettyHttpService.builder("test")
@@ -132,8 +131,6 @@ public void modify(ChannelPipeline pipeline) {
132131
public static void setup() throws Exception {
133132
service = createBaseNettyHttpServiceBuilder().build();
134133
service.start();
135-
int port = service.getBindAddress().getPort();
136-
baseURI = URI.create(String.format("http://localhost:%d", port));
137134
}
138135

139136
@AfterClass
@@ -165,11 +162,16 @@ public static void teardown() throws Exception {
165162
Assert.assertTrue("Some netty threads are still alive. Please see logs above", passed);
166163
}
167164

165+
protected final URI getBaseURI() {
166+
return URI.create(String.format("%s://localhost:%d", service.isSSLEnabled() ? "https" : "http",
167+
service.getBindAddress().getPort()));
168+
}
169+
168170
@Test
169171
public void testUploadDisconnect() throws Exception {
170172
File filePath = new File(tmpFolder.newFolder(), "test.txt");
171173

172-
URI uri = baseURI.resolve("/test/v1/stream/upload/file");
174+
URI uri = getBaseURI().resolve("/test/v1/stream/upload/file");
173175
try (Socket socket = createRawSocket(uri.toURL())) {
174176

175177
// Make a PUT call through socket, so that we can close it prematurely
@@ -378,7 +380,7 @@ public void testNonExistingMethods() throws IOException {
378380

379381
@Test
380382
public void testKeepAlive() throws Exception {
381-
final URL url = baseURI.resolve("/test/v1/tweets/1").toURL();
383+
final URL url = getBaseURI().resolve("/test/v1/tweets/1").toURL();
382384

383385
ThreadFactory threadFactory = new ThreadFactory() {
384386
private final AtomicInteger id = new AtomicInteger(0);
@@ -639,7 +641,7 @@ public void testDefaultQueryParam() throws IOException {
639641

640642
@Test (timeout = 5000)
641643
public void testConnectionClose() throws Exception {
642-
URL url = baseURI.resolve("/test/v1/connectionClose").toURL();
644+
URL url = getBaseURI().resolve("/test/v1/connectionClose").toURL();
643645

644646
// Fire http request using raw socket so that we can verify the connection get closed by the server
645647
// after the response.
@@ -832,7 +834,7 @@ protected Socket createRawSocket(URL url) throws IOException {
832834
}
833835

834836
protected HttpURLConnection request(String path, HttpMethod method, boolean keepAlive) throws IOException {
835-
URL url = baseURI.resolve(path).toURL();
837+
URL url = getBaseURI().resolve(path).toURL();
836838
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
837839
if (method == HttpMethod.POST || method == HttpMethod.PUT) {
838840
urlConn.setDoOutput(true);

src/test/java/io/cdap/http/HttpsServerTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,11 @@ public static void setup() throws Exception {
6868
sslClientContext = new SSLClientContext();
6969
service = builder.build();
7070
service.start();
71-
72-
int port = service.getBindAddress().getPort();
73-
baseURI = URI.create(String.format("https://localhost:%d", port));
7471
}
7572

7673
@Override
7774
protected HttpURLConnection request(String path, HttpMethod method, boolean keepAlive) throws IOException {
78-
URL url = baseURI.resolve(path).toURL();
75+
URL url = getBaseURI().resolve(path).toURL();
7976
HttpsURLConnection.setDefaultSSLSocketFactory(sslClientContext.getClientContext().getSocketFactory());
8077
HostnameVerifier allHostsValid = new HostnameVerifier() {
8178
@Override

src/test/java/io/cdap/http/MutualAuthServerTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.File;
2222
import java.io.InputStream;
23-
import java.net.URI;
2423
import java.nio.file.Files;
2524
import java.nio.file.StandardCopyOption;
2625

@@ -52,8 +51,5 @@ public static void setup() throws Exception {
5251
setSslClientContext(new SSLClientContext(trustKeyStore, trustKeyStorePassword));
5352
service = builder.build();
5453
service.start();
55-
56-
int port = service.getBindAddress().getPort();
57-
baseURI = URI.create(String.format("https://localhost:%d", port));
5854
}
5955
}

0 commit comments

Comments
 (0)