Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow scheme in URL #575

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.smallrye.stork.utils;

import java.net.InetAddress;

import static java.lang.String.format;

/**
* A set of utility methods around addresses.
*/
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.
*
Expand All @@ -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);
}
}
Expand Down
139 changes: 135 additions & 4 deletions core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -52,6 +55,18 @@ void testSecureDetection() {
assertThat(instances.get(0).isSecure()).isTrue();
}

@Test
void testSchemeDetection() {
List<ServiceInstance> 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<ServiceInstance> serviceInstances = stork.getService("first-service")
Expand Down