2222namespace {
2323 using mongo::ConnectionString;
2424
25- struct URLTestCase {
26- std::string URL ;
25+ struct URITestCase {
26+ std::string URI ;
2727 std::string uname;
2828 std::string password;
2929 mongo::ConnectionString::ConnectionType type;
@@ -33,10 +33,15 @@ namespace {
3333 std::string database;
3434 };
3535
36+ struct InvalidURITestCase {
37+ std::string URI;
38+ };
39+
3640 const mongo::ConnectionString::ConnectionType kMaster = mongo::ConnectionString::MASTER;
3741 const mongo::ConnectionString::ConnectionType kSet = mongo::ConnectionString::SET;
42+ const mongo::ConnectionString::ConnectionType kPair = mongo::ConnectionString::PAIR;
3843
39- const URLTestCase cases [] = {
44+ const URITestCase validCases [] = {
4045
4146 { " mongodb://user:pwd@127.0.0.1" , " user" , " pwd" , kMaster , " " , 1 , 0 , " " },
4247
@@ -111,16 +116,32 @@ namespace {
111116 { " mongodb://[::1]:1234,[::1]:1234/dbName?foo=a&c=b&replicaSet=replName" , " " , " " , kSet , " replName" , 2 , 3 , " dbName" },
112117 };
113118
119+ const InvalidURITestCase invalidCases[] = {
120+
121+ { " localhost:27017" },
122+
123+ { " 127.0.0.2:1234,104.9.12.3:27017" },
114124
115- TEST (ConnectionString, GoodTrickyURLs) {
125+ { " 127.0.0.2:1234,104.9.12.3:27017/?replName=shaun " },
116126
117- const size_t numCases = sizeof (cases) / sizeof (cases[0 ]);
127+ { " replSetName/localhost:27027" },
128+
129+ { " anything,anything/?thatDoesntStartWith=mongodb://" },
130+
131+ { " mongodb://" },
132+
133+ { " mongodb://localhost:27017,localhost:27018?replicaSet=missingSlash" },
134+ };
135+
136+ TEST (ConnectionString, GoodTrickyURIs) {
137+
138+ const size_t numCases = sizeof (validCases) / sizeof (validCases[0 ]);
118139
119140 for (size_t i = 0 ; i != numCases; ++i) {
120- const URLTestCase testCase = cases [i];
121- std::cout << " Testing URL : " << testCase.URL << ' \n ' ;
141+ const URITestCase testCase = validCases [i];
142+ std::cout << " Testing URI : " << testCase.URI << ' \n ' ;
122143 std::string errMsg;
123- const ConnectionString result = ConnectionString::parse (testCase.URL , errMsg);
144+ const ConnectionString result = ConnectionString::parse (testCase.URI , errMsg);
124145 ASSERT_TRUE (result.isValid ());
125146 ASSERT_TRUE (errMsg.empty ());
126147 ASSERT_EQ (testCase.uname , result.getUser ());
@@ -136,4 +157,72 @@ namespace {
136157 }
137158 }
138159
160+ TEST (ConnectionString, InvalidURIs) {
161+ const size_t numCases = sizeof (invalidCases) / sizeof (invalidCases[0 ]);
162+
163+ for (size_t i = 0 ; i != numCases; ++i) {
164+ const InvalidURITestCase testCase = invalidCases[i];
165+ std::cout << " Testing URI: " << testCase.URI << ' \n ' ;
166+ std::string errMsg;
167+ const ConnectionString result = ConnectionString::parse (testCase.URI , errMsg);
168+ ASSERT_FALSE (result.isValid ());
169+ }
170+ }
171+
172+ // Test Deprecated URI Parsing
173+
174+ struct DeprecatedURITestCase {
175+ std::string URI;
176+ size_t numservers;
177+ mongo::ConnectionString::ConnectionType type;
178+ std::string setname;
179+ };
180+
181+ const DeprecatedURITestCase validDeprecatedCases[] = {
182+
183+ { " localhost:27017" , 1 , kMaster , " " },
184+
185+ { " localhost:27017,localhost:30000" , 2 , kPair , " " },
186+
187+ { " replName/localhost:27017,127.0.0.2:1234" , 2 , kSet , " replName" },
188+
189+ { " localhost:1050,localhost:1055/?replicaSet=rs-1234" , 1 , kSet , " localhost:1050,localhost:1055" },
190+ };
191+
192+ const InvalidURITestCase invalidDeprecatedCases[] = {
193+
194+ { " 1.2.3.4:5555,6.7.8.9:1000,127.0.0.2:1234" },
195+
196+ { " localhost:27017,localhost:27018,localhost:27019,localhost:27020?replicaSet=myReplicaSet" },
197+
198+ };
199+
200+ TEST (ConnectionString, GoodDeprecatedURIs) {
201+ const size_t numCases = sizeof (validDeprecatedCases) / sizeof (validDeprecatedCases[0 ]);
202+
203+ for (size_t i = 0 ; i != numCases; ++i) {
204+ const DeprecatedURITestCase testCase = validDeprecatedCases[i];
205+ std::cout << " Testing URI: " << testCase.URI << ' \n ' ;
206+ std::string errMsg;
207+ const ConnectionString result = ConnectionString::parseDeprecated (testCase.URI , errMsg);
208+ ASSERT_TRUE (result.isValid ());
209+ ASSERT_TRUE (errMsg.empty ());
210+ ASSERT_EQ (testCase.numservers , result.getServers ().size ());
211+ ASSERT_EQ (testCase.type , result.type ());
212+ ASSERT_EQ (testCase.setname , result.getSetName ());
213+ }
214+ }
215+
216+ TEST (ConnectionString, InvalidDeprecatedURIs) {
217+ const size_t numCases = sizeof (invalidDeprecatedCases) / sizeof (invalidDeprecatedCases[0 ]);
218+
219+ for (size_t i = 0 ; i != numCases; ++i) {
220+ const InvalidURITestCase testCase = invalidDeprecatedCases[i];
221+ std::cout << " Testing URI: " << testCase.URI << ' \n ' ;
222+ std::string errMsg;
223+ const ConnectionString result = ConnectionString::parseDeprecated (testCase.URI , errMsg);
224+ ASSERT_FALSE (result.isValid ());
225+ }
226+ }
227+
139228} // namespace
0 commit comments