Skip to content

Fix for bug #4003. Better message instead of ArrayIndexOutOfBoundsExce #4109

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

Merged
merged 8 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/main/java/redis/clients/jedis/util/JedisURIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.RedisProtocol;
import redis.clients.jedis.exceptions.JedisException;

public final class JedisURIHelper {

Expand Down Expand Up @@ -33,7 +34,11 @@ public static String getUser(URI uri) {
public static String getPassword(URI uri) {
String userInfo = uri.getUserInfo();
if (userInfo != null) {
return userInfo.split(":", 2)[1];
String[] userAndPassword = userInfo.split(":", 2);
if (userAndPassword.length < 2) {
throw new JedisException("AUTH error. Password not provided in uri");
}
return userAndPassword[1];
}
return null;
}
Expand Down
16 changes: 12 additions & 4 deletions src/test/java/redis/clients/jedis/util/JedisURIHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package redis.clients.jedis.util;

import static org.junit.Assert.*;
import static redis.clients.jedis.util.JedisURIHelper.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import redis.clients.jedis.RedisProtocol;
import redis.clients.jedis.exceptions.JedisException;

public class JedisURIHelperTest {

Expand Down Expand Up @@ -73,4 +72,13 @@ public void shouldGetProtocolFromDefinition() {
assertEquals(RedisProtocol.RESP3, getRedisProtocol(URI.create("redis://host:1234/1?protocol=3")));
assertEquals(RedisProtocol.RESP3, getRedisProtocol(URI.create("redis://host:1234/1/?protocol=3")));
}

@Test
public void shouldReturnNullIfURIDoesNotHavePassword() throws URISyntaxException {
URI uri = new URI("redis://user@host:9000/0");
JedisException jedisException = assertThrows(JedisException.class, () -> {
getPassword(uri);
});
assertEquals(jedisException.getMessage(), "AUTH error. Password not provided in uri");
}
}
Loading