Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ static String normalizeAddress(String urlString, boolean usePlaintext) {

final String scheme = usePlaintext ? "http" : "https";
if (uri.getHost() == null) {
// if there is no host then we are likely dealing with a host and port
try {
uri = new URI(scheme, null, uri.getScheme(), Integer.parseInt(uri.getSchemeSpecificPart()), null, null, null);
} catch (URISyntaxException e) {
throw new SDKException("error trying to create URL for host and port[" + urlString + "]", e);
// if there is no host and no scheme, then we assume the input is a hostname with no port or scheme
Comment thread
mkleene marked this conversation as resolved.
if (uri.getScheme() == null) {
try {
uri = new URI(scheme, null, uri.getSchemeSpecificPart(), -1, null, null, null);
} catch (URISyntaxException e) {
throw new SDKException("error trying to create URL for hostname [" + urlString + "]", e);
}
} else {
// otherwise, we have a scheme but no host, so we assume the scheme is actually the host and the SSP is the port
try {
uri = new URI(scheme, null, uri.getScheme(), Integer.parseInt(uri.getSchemeSpecificPart()), null, null, null);
} catch (URISyntaxException | NumberFormatException e) {
throw new SDKException("error trying to create URL for host and port[" + urlString + "]", e);
}
Comment thread
mkleene marked this conversation as resolved.
}
}
final int port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import static io.opentdf.platform.sdk.AddressNormalizer.normalizeAddress;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertThrows;
Comment thread
mkleene marked this conversation as resolved.
Outdated

class AddressNormalizerTest {

Expand All @@ -14,6 +15,7 @@ void testAddressNormalizationWithHTTPSClient() {
// default to https if no scheme is provided
assertThat(normalizeAddress("example.org:1234", false)).isEqualTo("https://example.org:1234");
assertThat(normalizeAddress("ftp://example.org", false)).isEqualTo("https://example.org:443");
assertThat(normalizeAddress("keycloak.vm", false)).isEqualTo("https://keycloak.vm:443");
}

@Test
Expand All @@ -23,5 +25,15 @@ void testAddressNormaliationWithInsecureHTTPClient() {
// default to http if no scheme is provided
assertThat(normalizeAddress("example.org:1234", true)).isEqualTo("http://example.org:1234");
assertThat(normalizeAddress("sftp://example.org", true)).isEqualTo("http://example.org:80");
assertThat(normalizeAddress("keycloak.vm", true)).isEqualTo("http://keycloak.vm:80");
}

@Test
void testAddressNormalizationWithInvalidPort() {
var thrown = assertThrows(SDKException.class, () -> normalizeAddress("example.org:notaport", true));
assertThat(thrown.getMessage()).contains("example.org:notaport");

thrown = assertThrows(SDKException.class, () -> normalizeAddress("http://example.org:notaport", true));
assertThat(thrown.getMessage()).contains("http://example.org:notaport");
}
}
Loading