diff --git a/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java b/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java index 4d89f856..67802f98 100644 --- a/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java +++ b/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java @@ -1,5 +1,7 @@ package io.smallrye.stork.utils; +import java.net.InetAddress; + import static java.lang.String.format; /** @@ -7,6 +9,9 @@ */ public final class StorkAddressUtils { + public static final String HTTP_PREFIX = "http://"; + public static final String HTTPS_PREFIX = "https://"; + /** * Creates a new {@link HostAndPort} instance from an address. * @@ -23,10 +28,13 @@ public static HostAndPort parseToHostAndPort(String address, } if (address.charAt(0) == '[') { return parseIpV6AddressWithSquareBracket(address, defaultPort, configPlace); - } else if (countColons(address) > 1) { + } else if (countColons(address) > 2) { // IPv6. return new HostAndPort(address, defaultPort); } else { + if (address.startsWith(HTTP_PREFIX) || address.startsWith(HTTPS_PREFIX)) { + address = address.replaceFirst("^https?://", ""); + } return parseNonIpv6Address(address, defaultPort, configPlace); } } diff --git a/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java b/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java index 9f18ac47..4d3361e5 100644 --- a/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java +++ b/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java @@ -50,6 +50,25 @@ void shouldParseIpV4WithoutPort() { assertThat(result.path).isEmpty(); } + @Test + void shouldParseIpV4WithoutPortButWithScheme() { + String ipV4WithoutPort = "127.0.0.1"; + + String ipV4WithoutPortWithScheme = "http://" + ipV4WithoutPort; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(ipV4WithoutPortWithScheme, 8383, + "serviceUsingIPv4AdressWithoutPortButScheme"); + assertThat(result.host).isEqualTo(ipV4WithoutPort); + assertThat(result.port).isEqualTo(8383); + assertThat(result.path).isEmpty(); + + String ipV4WithoutPortWithSecureScheme = "https://" + ipV4WithoutPort; + result = StorkAddressUtils.parseToHostAndPort(ipV4WithoutPortWithSecureScheme, 8383, + "serviceUsingIPv4AdressWithoutPortButScheme"); + assertThat(result.host).isEqualTo(ipV4WithoutPort); + assertThat(result.port).isEqualTo(8383); + assertThat(result.path).isEmpty(); + } + @Test void shouldParseIpV4WithPort() { String address = "127.0.0.1"; @@ -60,14 +79,53 @@ void shouldParseIpV4WithPort() { assertThat(result.path).isEmpty(); } + @Test + void shouldParseIpV4WithPortAndScheme() { + String address = "127.0.0.1"; + String addressWithPortAndScheme = "http://" + address + ":8787"; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndScheme, 8383, + "serviceUsingIPv4AddressWithPortAndSheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8787); + assertThat(result.path).isEmpty(); + + String addressWithPortAndSecureScheme = "https://" + address + ":8787"; + result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndSecureScheme, 8383, + "serviceUsingIPv4AddressWithPortAndSheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8787); + assertThat(result.path).isEmpty(); + } + @Test void shouldParseIpV4WithPortAndPath() { String address = "127.0.0.1"; - String addressWithPort = address + ":8787/test"; - HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPort, 8383, "serviceUsingIPv4AddressWithPort"); + String addressWithPortAndPath = address + ":8787/test"; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndPath, 8383, + "serviceUsingIPv4AddressWithPort"); assertThat(result.host).isEqualTo(address); assertThat(result.port).isEqualTo(8787); assertThat(result.path).contains("/test"); + + } + + @Test + void shouldParseIpV4WithPortAndPathAndScheme() { + String address = "127.0.0.1"; + String addressWithPortAndPathAndScheme = "http://" + address + ":8787/test"; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndPathAndScheme, 8383, + "serviceUsingIPv4AddressWithPortAndPathAndScheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8787); + assertThat(result.path).contains("/test"); + + String addressWithPortAndPathAndSecureScheme = "https://" + address + ":8787/test"; + result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndPathAndSecureScheme, 8383, + "serviceUsingIPv4AddressWithPortAndPathAndScheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8787); + assertThat(result.path).contains("/test"); + } @Test @@ -76,12 +134,49 @@ void shouldParseHostnameWithoutPort() { HostAndPort result = StorkAddressUtils.parseToHostAndPort(address, 8383, "serviceUsingHostnameWithoutPort"); assertThat(result.host).isEqualTo(address); assertThat(result.port).isEqualTo(8383); + assertThat(result.path).isEmpty(); + } + + @Test + void shouldParseHostnameWithoutPortButScheme() { + String address = "my-host.com.pl"; + String addressWithScheme = "http://" + address; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithScheme, 8383, + "serviceUsingHostnameWithoutPortButScheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8383); + assertThat(result.path).isEmpty(); + + String addressWithSecureScheme = "https://" + address; + result = StorkAddressUtils.parseToHostAndPort(addressWithSecureScheme, 8383, + "serviceUsingHostnameWithoutPortButScheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8383); + assertThat(result.path).isEmpty(); } @Test void shouldParseHostnameWithoutPortButWithPath() { String address = "my-host.com.pl/foo/bar"; - HostAndPort result = StorkAddressUtils.parseToHostAndPort(address, 8383, "serviceUsingHostnameWithoutPort"); + HostAndPort result = StorkAddressUtils.parseToHostAndPort(address, 8383, "serviceUsingHostnameWithoutPortButWithPath"); + assertThat(result.host).isEqualTo("my-host.com.pl"); + assertThat(result.port).isEqualTo(8383); + assertThat(result.path).contains("/foo/bar"); + } + + @Test + void shouldParseHostnameWithoutPortButWithPathAndSchema() { + String address = "my-host.com.pl/foo/bar"; + String addressWithScheme = "http://" + address; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithScheme, 8383, + "serviceUsingHostnameWithoutPortButWithPathAndSchema"); + assertThat(result.host).isEqualTo("my-host.com.pl"); + assertThat(result.port).isEqualTo(8383); + assertThat(result.path).contains("/foo/bar"); + + String addressWithSecureScheme = "https://" + address; + result = StorkAddressUtils.parseToHostAndPort(addressWithSecureScheme, 8383, + "serviceUsingHostnameWithoutPortButWithPathAndSchema"); assertThat(result.host).isEqualTo("my-host.com.pl"); assertThat(result.port).isEqualTo(8383); assertThat(result.path).contains("/foo/bar"); @@ -97,10 +192,46 @@ void shouldParseHostnameWithPort() { assertThat(result.path).isEmpty(); } + @Test + void shouldParseHostnameWithPortAndScheme() { + String address = "my-host.com.pl"; + String addressWithPortAndScheme = "http://" + address + ":8989"; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndScheme, 8383, + "serviceUsingHostnameWithPortAndScheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8989); + assertThat(result.path).isEmpty(); + + String addressWithPortAndSecureScheme = "https://" + address + ":8989"; + result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndSecureScheme, 8383, + "serviceUsingHostnameWithPortAndScheme"); + assertThat(result.host).isEqualTo(address); + assertThat(result.port).isEqualTo(8989); + assertThat(result.path).isEmpty(); + } + @Test void shouldParseHostnameWithPortWithPath() { String addressWithPort = "my-host.com.pl:8989/test/123"; - HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPort, 8383, "serviceUsingHostnameWithPort"); + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPort, 8383, "serviceUsingHostnameWithPortAndPath"); + assertThat(result.host).isEqualTo("my-host.com.pl"); + assertThat(result.port).isEqualTo(8989); + assertThat(result.path).contains("/test/123"); + } + + @Test + void shouldParseHostnameWithPortWithPathAndScheme() { + String addressWithPort = "my-host.com.pl:8989/test/123"; + String addressWithPortAndScheme = "http://" + addressWithPort; + HostAndPort result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndScheme, 8383, + "serviceUsingHostnameWithPortAndPathAndScheme"); + assertThat(result.host).isEqualTo("my-host.com.pl"); + assertThat(result.port).isEqualTo(8989); + assertThat(result.path).contains("/test/123"); + + String addressWithPortAndSecureScheme = "https://" + addressWithPort; + result = StorkAddressUtils.parseToHostAndPort(addressWithPortAndSecureScheme, 8383, + "serviceUsingHostnameWithPortAndPathAndScheme"); assertThat(result.host).isEqualTo("my-host.com.pl"); assertThat(result.port).isEqualTo(8989); assertThat(result.path).contains("/test/123"); diff --git a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java index af06fd6f..62b7a871 100644 --- a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java +++ b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java @@ -35,6 +35,9 @@ void setUp() { TestConfigProvider.addServiceConfig("secured-service", null, "static", null, Map.of("address-list", "localhost:443, localhost")); + TestConfigProvider.addServiceConfig("scheme-service", null, "static", + null, Map.of("address-list", "http://localhost:8080, https://localhost:8081")); + stork = StorkTestUtils.getNewStorkInstance(); } @@ -52,6 +55,18 @@ void testSecureDetection() { assertThat(instances.get(0).isSecure()).isTrue(); } + @Test + void testSchemeDetection() { + List instances = stork.getService("scheme-service").getInstances().await() + .atMost(Duration.ofSeconds(5)); + + assertThat(instances).hasSize(2); + assertThat(instances.stream().map(ServiceInstance::getHost)).containsExactlyInAnyOrder("localhost", + "localhost"); + assertThat(instances.stream().map(ServiceInstance::getPort)).containsExactlyInAnyOrder(8080, + 8081); + } + @Test void shouldGetAllServiceInstances() { List serviceInstances = stork.getService("first-service")