fix null sslPort issue (#4696)

* fix null sslPort issue

* add tests
This commit is contained in:
Parag Jain 2017-08-24 15:19:07 -05:00 committed by Himanshu
parent 59a48a560a
commit 421e02203f
2 changed files with 68 additions and 3 deletions

View File

@ -54,11 +54,18 @@ public class ServerDiscoverySelector implements DiscoverySelector<Server>
public Server apply(final ServiceInstance instance)
{
Preconditions.checkState(
instance.getPort() >= 0 || instance.getSslPort() >= 0,
instance.getPort() >= 0 || (instance.getSslPort() != null && instance.getSslPort() >= 0),
"WTH?! Both port and sslPort not set"
);
final int port = instance.getSslPort() >= 0 ? instance.getSslPort() : instance.getPort();
final String scheme = instance.getSslPort() >= 0 ? "https" : "http";
final int port;
final String scheme;
if (instance.getSslPort() == null) {
port = instance.getPort();
scheme = "http";
} else {
port = instance.getSslPort() >= 0 ? instance.getSslPort() : instance.getPort();
scheme = instance.getSslPort() >= 0 ? "https" : "http";
}
return new Server()
{
@Override

View File

@ -38,6 +38,7 @@ public class ServerDiscoverySelectorTest
private ServerDiscoverySelector serverDiscoverySelector;
private ServiceInstance instance;
private static final int PORT = 8080;
private static final int SSL_PORT = 8280;
private static final String ADDRESS = "localhost";
@Before
@ -77,6 +78,63 @@ public class ServerDiscoverySelectorTest
Assert.assertEquals("http", uri.getScheme());
}
@Test
public void testPickWithNullSslPort() throws Exception
{
EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes();
EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes();
EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes();
EasyMock.expect(instance.getSslPort()).andReturn(null).anyTimes();
EasyMock.replay(instance, serviceProvider);
Server server = serverDiscoverySelector.pick();
Assert.assertEquals(PORT, server.getPort());
Assert.assertEquals(ADDRESS, server.getAddress());
Assert.assertTrue(server.getHost().contains(Integer.toString(PORT)));
Assert.assertTrue(server.getHost().contains(ADDRESS));
Assert.assertEquals("http", server.getScheme());
EasyMock.verify(instance, serviceProvider);
final URI uri = new URI(
server.getScheme(),
null,
server.getAddress(),
server.getPort(),
"/druid/indexer/v1/action",
null,
null
);
Assert.assertEquals(PORT, uri.getPort());
Assert.assertEquals(ADDRESS, uri.getHost());
Assert.assertEquals("http", uri.getScheme());
}
@Test
public void testPickWithSslPort() throws Exception
{
EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes();
EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes();
EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes();
EasyMock.expect(instance.getSslPort()).andReturn(SSL_PORT).anyTimes();
EasyMock.replay(instance, serviceProvider);
Server server = serverDiscoverySelector.pick();
Assert.assertEquals(SSL_PORT, server.getPort());
Assert.assertEquals(ADDRESS, server.getAddress());
Assert.assertTrue(server.getHost().contains(Integer.toString(SSL_PORT)));
Assert.assertTrue(server.getHost().contains(ADDRESS));
Assert.assertEquals("https", server.getScheme());
EasyMock.verify(instance, serviceProvider);
final URI uri = new URI(
server.getScheme(),
null,
server.getAddress(),
server.getPort(),
"/druid/indexer/v1/action",
null,
null
);
Assert.assertEquals(SSL_PORT, uri.getPort());
Assert.assertEquals(ADDRESS, uri.getHost());
Assert.assertEquals("https", uri.getScheme());
}
@Test
public void testPickIPv6() throws Exception