Skip to content

Commit 1cb8f5c

Browse files
rcugutscotthernandez
authored andcommitted
Bugfix: JAVA-299 - MongoURI does not parse URIs as specified in the documentation (http://www.mongodb.org/display/DOCS/Connections)
1 parent 42d8c8c commit 1cb8f5c

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/main/com/mongodb/MongoURI.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public MongoURI( String uri ){
6060
List<String> all = new LinkedList<String>();
6161

6262

63-
if ( serverPart.indexOf( "@" ) > 0 ){
64-
int idx = serverPart.indexOf( "@" );
65-
_username = serverPart.substring( 0 , idx );
63+
int idx = serverPart.indexOf( "@" );
64+
65+
if ( idx > 0 ){
66+
String authPart = serverPart.substring( 0 , idx );
6667
serverPart = serverPart.substring( idx + 1 );
6768

68-
idx = serverPart.indexOf( ":" );
69-
_password = serverPart.substring( 0 , idx ).toCharArray();
70-
serverPart = serverPart.substring( idx + 1 );
69+
idx = authPart.indexOf( ":" );
70+
_username = authPart.substring( 0, idx );
71+
_password = authPart.substring( idx + 1 ).toCharArray();
7172
}
7273
else {
7374
_username = null;

src/test/com/mongodb/MongoURITest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,34 @@ public void testBasic2(){
5454

5555
@Test()
5656
public void testUserPass(){
57-
MongoURI u = new MongoURI( "mongodb://aaa@bbb:foo/bar" );
57+
MongoURI u = new MongoURI( "mongodb://user:pass@host/bar" );
5858
assertEquals( 1 , u.getHosts().size() );
59-
assertEquals( "foo" , u.getHosts().get(0) );
60-
assertEquals( "aaa" , u.getUsername() );
61-
assertEquals( "bbb" , new String( u.getPassword() ) );
59+
assertEquals( "host" , u.getHosts().get(0) );
60+
assertEquals( "user" , u.getUsername() );
61+
assertEquals( "pass" , new String( u.getPassword() ) );
62+
}
63+
64+
@Test()
65+
public void testUserPassAndPort(){
66+
MongoURI u = new MongoURI( "mongodb://user:pass@host:27011/bar" );
67+
assertEquals( 1 , u.getHosts().size() );
68+
assertEquals( "host:27011" , u.getHosts().get(0) );
69+
assertEquals( "user" , u.getUsername() );
70+
assertEquals( "pass" , new String( u.getPassword() ) );
71+
}
72+
73+
@Test()
74+
public void testUserPassAndMultipleHostsWithPort(){
75+
MongoURI u = new MongoURI( "mongodb://user:pass@host:27011,host2:27012,host3:27013/bar" );
76+
assertEquals( 3 , u.getHosts().size() );
77+
assertEquals( "host:27011" , u.getHosts().get(0) );
78+
assertEquals( "host2:27012" , u.getHosts().get(1) );
79+
assertEquals( "host3:27013" , u.getHosts().get(2) );
80+
assertEquals( "user" , u.getUsername() );
81+
assertEquals( "pass" , new String( u.getPassword() ) );
6282
}
6383

84+
6485
@Test()
6586
public void testOptions(){
6687
MongoURI uAmp = new MongoURI( "mongodb://localhost/test?" +

0 commit comments

Comments
 (0)