From cba44d475452cdb2211a96006e20e029849a4551 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 8 Sep 2015 13:54:36 +0200 Subject: [PATCH] ERR invalid password when creating a RedisURI with a username #131 --- .../java/com/lambdaworks/redis/RedisURI.java | 10 +++++++++- .../lambdaworks/redis/RedisURIBuilderTest.java | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/lambdaworks/redis/RedisURI.java b/src/main/java/com/lambdaworks/redis/RedisURI.java index c018cd2f9f..10a4e8cbb3 100644 --- a/src/main/java/com/lambdaworks/redis/RedisURI.java +++ b/src/main/java/com/lambdaworks/redis/RedisURI.java @@ -150,8 +150,16 @@ public static RedisURI create(URI uri) { String password = userInfo; if (password.startsWith(":")) { password = password.substring(1); + } else { + + int index = password.indexOf(':'); + if (index > 0) { + password = password.substring(index + 1); + } + } + if (password != null && !password.equals("")) { + builder.withPassword(password); } - builder.withPassword(password); } if (isNotEmpty(uri.getPath()) && builder.redisURI.getSocket() == null) { diff --git a/src/test/java/com/lambdaworks/redis/RedisURIBuilderTest.java b/src/test/java/com/lambdaworks/redis/RedisURIBuilderTest.java index 9324f5f19d..718ec5ca39 100644 --- a/src/test/java/com/lambdaworks/redis/RedisURIBuilderTest.java +++ b/src/test/java/com/lambdaworks/redis/RedisURIBuilderTest.java @@ -45,6 +45,22 @@ public void redisFromUrl() throws Exception { assertThat(result.isSsl()).isFalse(); } + @Test + public void redisFromUrlNoPassword() throws Exception { + RedisURI redisURI = RedisURI.create("redis://localhost:1234/5"); + assertThat(redisURI.getPassword()).isNull(); + + redisURI = RedisURI.create("redis://h:@localhost.com:14589"); + assertThat(redisURI.getPassword()).isNull(); + + } + + @Test + public void redisFromUrlPassword() throws Exception { + RedisURI redisURI = RedisURI.create("redis://h:password@localhost.com:14589"); + assertThat(redisURI.getPassword()).isEqualTo("password".toCharArray()); + } + @Test public void redisWithSSL() throws Exception { RedisURI result = RedisURI.Builder.redis("localhost").withSsl(true).withStartTls(true).build();