Changes to avoid IndexOutOfBoundsException when only one embedded Credential is present in the url. (#980)

* Changes to handle IndexOutOFBoundsException
This commit is contained in:
indraneelb1903
2018-04-18 09:11:56 -04:00
committed by Ryan Baxter
parent 2b1248da48
commit 69d8bfc841
2 changed files with 50 additions and 4 deletions

View File

@@ -233,14 +233,16 @@ public class ConfigClientProperties {
String bare = UriComponentsBuilder.fromHttpUrl(uri).userInfo(null).build()
.toUriString();
result.uri = bare;
// handle the password only case
// if userInfo does not contain a :, then append a : to it
if (!userInfo.contains(":")) {
userInfo = userInfo + ":";
}
String[] split = userInfo.split(":");
int sepIndex=userInfo.indexOf(":");
// set username and password from uri
result.username = split[0];
result.password = split[1];
result.username = userInfo.substring(0, sepIndex);
result.password = userInfo.substring(sepIndex +1);
// override password if explicitly set
if (explicitCredentials.password != null) {

View File

@@ -59,7 +59,51 @@ public class ConfigClientPropertiesTests {
assertEquals("foo", locator.getUsername());
assertEquals("secret", locator.getPassword());
}
@Test
public void testIfNoColonPresentInUriCreds() {
locator.setUri("http://foobar@localhost:9999");
locator.setPassword("secret");
assertEquals("http://localhost:9999", locator.getRawUri());
assertEquals("foobar", locator.getUsername());
assertEquals("secret", locator.getPassword());
}
@Test
public void testIfColonPresentAtTheEndInUriCreds() {
locator.setUri("http://foobar:@localhost:9999");
locator.setPassword("secret");
assertEquals("http://localhost:9999", locator.getRawUri());
assertEquals("foobar", locator.getUsername());
assertEquals("secret", locator.getPassword());
}
@Test
public void testIfColonPresentAtTheStartInUriCreds() {
locator.setUri("http://:foobar@localhost:9999");
assertEquals("http://localhost:9999", locator.getRawUri());
assertEquals("", locator.getUsername());
assertEquals("foobar", locator.getPassword());
}
@Test
public void testIfColonPresentAtTheStartAndEndInUriCreds() {
locator.setUri("http://:foobar:@localhost:9999");
assertEquals("http://localhost:9999", locator.getRawUri());
assertEquals("", locator.getUsername());
assertEquals("foobar:", locator.getPassword());
}
@Test
public void testIfsolonPresentAtTheStartAndEndInUriCreds() {
locator.setUri("http:// @localhost:9999");
locator.setPassword("secret");
assertEquals("http://localhost:9999", locator.getRawUri());
assertEquals(" ", locator.getUsername());
assertEquals("secret", locator.getPassword());
}
@Test
public void changeNameInOverride() {
locator.setName("one");