Skip to content

Commit aa40ddf

Browse files
authored
Merge pull request #341 from LangInteger/master
feat - check wrong connectionString syntax via host&port parse result
2 parents 61c3c5d + 2cf4c3c commit aa40ddf

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/java/io/vertx/redis/client/impl/RedisURI.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,17 @@ public RedisURI(String connectionString) {
8080
try {
8181
final URI uri = new URI(connectionString);
8282

83-
final String host = uri.getHost() == null ? DEFAULT_HOST : uri.getHost();
84-
final int port = uri.getPort() == -1 ? DEFAULT_PORT : uri.getPort();
83+
boolean directRedisScheme = "rediss".equals(uri.getScheme()) || "redis".equals(uri.getScheme());
84+
if (directRedisScheme && uri.getHost() == null) {
85+
throw new IllegalArgumentException("Fail to parse connection string host");
86+
}
87+
final String host = uri.getHost();
88+
89+
if (directRedisScheme && uri.getPort() == -1) {
90+
throw new IllegalArgumentException("Fail to parse connection string port");
91+
}
92+
final int port = uri.getPort();
93+
8594
final String path = (uri.getPath() == null || uri.getPath().isEmpty()) ? "/" : uri.getPath();
8695

8796
// According to https://www.iana.org/assignments/uri-schemes/prov/redis there is no specified order of decision

src/test/java/io/vertx/redis/client/impl/RedisURITest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package io.vertx.redis.client.impl;
22

33
import org.junit.Assert;
4+
import org.junit.Rule;
45
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
57

68
/**
79
* @author <a href="mailto:artursletter@gmail.com">Artur Badretdinov</a>
810
*/
911
public class RedisURITest {
1012

13+
@Rule
14+
public ExpectedException exceptionRule = ExpectedException.none();
15+
1116
@Test
1217
public void testHostAndPort() {
1318
RedisURI redisURI = new RedisURI("redis://redis-1234.hosted.com:1234");
@@ -82,4 +87,18 @@ public void testColon() {
8287
RedisURI redisURI = new RedisURI("redis://:admin%3Aqwer@localhost:6379/1");
8388
Assert.assertEquals("admin:qwer", redisURI.password());
8489
}
90+
91+
@Test
92+
public void testRightSyntax() {
93+
RedisURI redisURI = new RedisURI("redis://your-redis-domain:6379");
94+
Assert.assertEquals(6379, redisURI.socketAddress().port());
95+
Assert.assertEquals("your-redis-domain", redisURI.socketAddress().host());
96+
}
97+
98+
@Test
99+
public void testWrongSyntax() {
100+
exceptionRule.expect(IllegalArgumentException.class);
101+
exceptionRule.expectMessage("Fail to parse connection string host");
102+
new RedisURI("redis://:your-redis-domain:6379");
103+
}
85104
}

0 commit comments

Comments
 (0)