PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/Core/Configuration/ConnectionStringTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 667 lines | 554 code | 99 blank | 14 comment | 1 complexity | 6c89b3c09d944715f8d1216624790d74 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Driver.Core.Clusters;
  23. using MongoDB.Driver.Core.Configuration;
  24. using MongoDB.Driver.Core.Operations;
  25. using NUnit.Framework;
  26. namespace MongoDB.Driver.Core.Configuration
  27. {
  28. [TestFixture]
  29. public class ConnectionStringTests
  30. {
  31. [Test]
  32. public void With_one_host_and_no_port()
  33. {
  34. var subject = new ConnectionString("mongodb://localhost");
  35. subject.Hosts.Count().Should().Be(1);
  36. subject.Hosts.Single().Should().Be(new DnsEndPoint("localhost", 27017));
  37. }
  38. [Test]
  39. public void With_one_host_and_port()
  40. {
  41. var subject = new ConnectionString("mongodb://localhost:27092");
  42. subject.Hosts.Count().Should().Be(1);
  43. subject.Hosts.Single().Should().Be(new DnsEndPoint("localhost", 27092));
  44. }
  45. [Test]
  46. public void With_two_hosts_and_one_port()
  47. {
  48. var subject = new ConnectionString("mongodb://localhost:27092,remote");
  49. subject.Hosts.Count().Should().Be(2);
  50. subject.Hosts[0].Should().Be(new DnsEndPoint("localhost", 27092));
  51. subject.Hosts[1].Should().Be(new DnsEndPoint("remote", 27017));
  52. }
  53. [Test]
  54. public void With_two_hosts_and_one_port2()
  55. {
  56. var subject = new ConnectionString("mongodb://localhost,remote:27092");
  57. subject.Hosts.Count().Should().Be(2);
  58. subject.Hosts[0].Should().Be(new DnsEndPoint("localhost", 27017));
  59. subject.Hosts[1].Should().Be(new DnsEndPoint("remote", 27092));
  60. }
  61. [Test]
  62. public void With_two_hosts_and_two_ports()
  63. {
  64. var subject = new ConnectionString("mongodb://localhost:30000,remote:27092");
  65. subject.Hosts.Count().Should().Be(2);
  66. subject.Hosts[0].Should().Be(new DnsEndPoint("localhost", 30000));
  67. subject.Hosts[1].Should().Be(new DnsEndPoint("remote", 27092));
  68. }
  69. [Test]
  70. public void With_an_ipv4_host()
  71. {
  72. var subject = new ConnectionString("mongodb://127.0.0.1");
  73. subject.Hosts.Count().Should().Be(1);
  74. subject.Hosts[0].Should().Be(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27017));
  75. }
  76. [Test]
  77. public void With_an_ipv4_host_and_port()
  78. {
  79. var subject = new ConnectionString("mongodb://127.0.0.1:28017");
  80. subject.Hosts.Count().Should().Be(1);
  81. subject.Hosts[0].Should().Be(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 28017));
  82. }
  83. [Test]
  84. public void With_an_ipv6_host()
  85. {
  86. var subject = new ConnectionString("mongodb://[::1]");
  87. subject.Hosts.Count().Should().Be(1);
  88. subject.Hosts[0].Should().Be(new IPEndPoint(IPAddress.Parse("[::1]"), 27017));
  89. }
  90. [Test]
  91. public void With_a_2_ipv6_hosts()
  92. {
  93. var subject = new ConnectionString("mongodb://[::1],[::2]");
  94. subject.Hosts.Count().Should().Be(2);
  95. subject.Hosts[0].Should().Be(new IPEndPoint(IPAddress.Parse("[::1]"), 27017));
  96. subject.Hosts[1].Should().Be(new IPEndPoint(IPAddress.Parse("[::2]"), 27017));
  97. }
  98. [Test]
  99. public void With_an_ipv6_host_and_port()
  100. {
  101. var subject = new ConnectionString("mongodb://[::1]:28017");
  102. subject.Hosts.Count().Should().Be(1);
  103. subject.Hosts[0].Should().Be(new IPEndPoint(IPAddress.Parse("[::1]"), 28017));
  104. }
  105. [Test]
  106. public void With_three_hosts_of_different_types()
  107. {
  108. var subject = new ConnectionString("mongodb://localhost,10.0.0.1:30000,[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:28017");
  109. subject.Hosts.Count().Should().Be(3);
  110. subject.Hosts[0].Should().Be(new DnsEndPoint("localhost", 27017, AddressFamily.Unspecified));
  111. subject.Hosts[1].Should().Be(new IPEndPoint(IPAddress.Parse("10.0.0.1"), 30000));
  112. subject.Hosts[2].Should().Be(new IPEndPoint(IPAddress.Parse("[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]"), 28017));
  113. }
  114. [Test]
  115. [TestCase("mongodb://localhost")]
  116. [TestCase("mongodb://localhost/")]
  117. public void When_nothing_is_specified(string connectionString)
  118. {
  119. var subject = new ConnectionString(connectionString);
  120. subject.AuthMechanism.Should().BeNull();
  121. subject.AuthSource.Should().BeNull();
  122. subject.Connect.Should().Be(ClusterConnectionMode.Automatic);
  123. subject.ConnectTimeout.Should().Be(null);
  124. subject.DatabaseName.Should().BeNull();
  125. subject.FSync.Should().Be(null);
  126. subject.Ipv6.Should().Be(null);
  127. subject.Journal.Should().Be(null);
  128. subject.MaxIdleTime.Should().Be(null);
  129. subject.MaxLifeTime.Should().Be(null);
  130. subject.MaxPoolSize.Should().Be(null);
  131. subject.MinPoolSize.Should().Be(null);
  132. subject.Password.Should().BeNull();
  133. subject.ReadPreference.Should().BeNull();
  134. subject.ReadPreferenceTags.Should().BeNull();
  135. subject.ReplicaSet.Should().BeNull();
  136. subject.LocalThreshold.Should().Be(null);
  137. subject.SocketTimeout.Should().Be(null);
  138. subject.Ssl.Should().Be(null);
  139. subject.SslVerifyCertificate.Should().Be(null);
  140. subject.Username.Should().BeNull();
  141. subject.UuidRepresentation.Should().BeNull();
  142. subject.WaitQueueMultiple.Should().Be(null);
  143. subject.WaitQueueSize.Should().Be(null);
  144. subject.WaitQueueTimeout.Should().Be(null);
  145. subject.W.Should().BeNull();
  146. subject.WTimeout.Should().Be(null);
  147. }
  148. [Test]
  149. public void When_everything_is_specified()
  150. {
  151. var connectionString = @"mongodb://user:pass@localhost1,localhost2:30000/test?" +
  152. "authMechanism=GSSAPI;" +
  153. "authMechanismProperties=CANONICALIZE_HOST_NAME:true;" +
  154. "authSource=admin;" +
  155. "connect=replicaSet;" +
  156. "connectTimeout=15ms;" +
  157. "fsync=true;" +
  158. "ipv6=false;" +
  159. "j=true;" +
  160. "maxIdleTime=10ms;" +
  161. "maxLifeTime=5ms;" +
  162. "maxPoolSize=20;" +
  163. "minPoolSize=15;" +
  164. "readPreference=primary;" +
  165. "readPreferenceTags=dc:1;" +
  166. "replicaSet=funny;" +
  167. "localThreshold=50ms;" +
  168. "socketTimeout=40ms;" +
  169. "ssl=false;" +
  170. "sslVerifyCertificate=true;" +
  171. "uuidRepresentation=standard;" +
  172. "waitQueueMultiple=10;" +
  173. "waitQueueSize=30;" +
  174. "waitQueueTimeout=60ms;" +
  175. "w=4;" +
  176. "wtimeout=20ms";
  177. var subject = new ConnectionString(connectionString);
  178. subject.AuthMechanism.Should().Be("GSSAPI");
  179. subject.AuthMechanismProperties.Count.Should().Be(1);
  180. subject.AuthMechanismProperties["canonicalize_host_name"].Should().Be("true");
  181. subject.AuthSource.Should().Be("admin");
  182. subject.Connect.Should().Be(ClusterConnectionMode.ReplicaSet);
  183. subject.ConnectTimeout.Should().Be(TimeSpan.FromMilliseconds(15));
  184. subject.DatabaseName.Should().Be("test");
  185. subject.FSync.Should().BeTrue();
  186. subject.Ipv6.Should().BeFalse();
  187. subject.Journal.Should().BeTrue();
  188. subject.MaxIdleTime.Should().Be(TimeSpan.FromMilliseconds(10));
  189. subject.MaxLifeTime.Should().Be(TimeSpan.FromMilliseconds(5));
  190. subject.MaxPoolSize.Should().Be(20);
  191. subject.MinPoolSize.Should().Be(15);
  192. subject.Password.Should().Be("pass");
  193. subject.ReadPreference.Should().Be(ReadPreferenceMode.Primary);
  194. subject.ReadPreferenceTags.Single().Should().Be(new TagSet(new[] { new Tag("dc", "1") }));
  195. subject.ReplicaSet.Should().Be("funny");
  196. subject.LocalThreshold.Should().Be(TimeSpan.FromMilliseconds(50));
  197. subject.SocketTimeout.Should().Be(TimeSpan.FromMilliseconds(40));
  198. subject.Ssl.Should().BeFalse();
  199. subject.SslVerifyCertificate.Should().Be(true);
  200. subject.Username.Should().Be("user");
  201. subject.UuidRepresentation.Should().Be(GuidRepresentation.Standard);
  202. subject.WaitQueueMultiple.Should().Be(10);
  203. subject.WaitQueueSize.Should().Be(30);
  204. subject.WaitQueueTimeout.Should().Be(TimeSpan.FromMilliseconds(60));
  205. subject.W.Should().Be(WriteConcern.WValue.Parse("4"));
  206. subject.WTimeout.Should().Be(TimeSpan.FromMilliseconds(20));
  207. }
  208. [Test]
  209. [TestCase("mongodb://localhost?authMechanism=GSSAPI", "GSSAPI")]
  210. [TestCase("mongodb://localhost?authMechanism=MONGODB-CR", "MONGODB-CR")]
  211. [TestCase("mongodb://localhost?authMechanism=PLAIN", "PLAIN")]
  212. [TestCase("mongodb://localhost?authMechanism=MONGODB-X509", "MONGODB-X509")]
  213. public void When_authMechanism_is_specified(string connectionString, string authMechanism)
  214. {
  215. var subject = new ConnectionString(connectionString);
  216. subject.AuthMechanism.Should().Be(authMechanism);
  217. }
  218. [Test]
  219. public void When_authMechanismProperties_is_specified()
  220. {
  221. var connectionString = "mongodb://localhost?authMechanismProperties=ONE:1,TWO:2";
  222. var subject = new ConnectionString(connectionString);
  223. subject.AuthMechanismProperties.Count.Should().Be(2);
  224. subject.AuthMechanismProperties["one"].Should().Be("1");
  225. subject.AuthMechanismProperties["TWO"].Should().Be("2");
  226. }
  227. [Test]
  228. [TestCase("mongodb://localhost?authSource=admin", "admin")]
  229. [TestCase("mongodb://localhost?authSource=awesome", "awesome")]
  230. public void When_authSource_is_specified(string connectionString, string authSource)
  231. {
  232. var subject = new ConnectionString(connectionString);
  233. subject.AuthSource.Should().Be(authSource);
  234. }
  235. [Test]
  236. [TestCase("mongodb://localhost?connect=automatic", ClusterConnectionMode.Automatic)]
  237. [TestCase("mongodb://localhost?connect=direct", ClusterConnectionMode.Direct)]
  238. [TestCase("mongodb://localhost?connect=replicaSet", ClusterConnectionMode.ReplicaSet)]
  239. [TestCase("mongodb://localhost?connect=sharded", ClusterConnectionMode.Sharded)]
  240. [TestCase("mongodb://localhost?connect=ShardRouter", ClusterConnectionMode.Sharded)]
  241. [TestCase("mongodb://localhost?connect=sTaNdAlOnE", ClusterConnectionMode.Standalone)]
  242. public void When_connect_is_specified(string connectionString, ClusterConnectionMode connect)
  243. {
  244. var subject = new ConnectionString(connectionString);
  245. subject.Connect.Should().Be(connect);
  246. }
  247. [Test]
  248. [TestCase("mongodb://localhost?connectTimeout=15ms", 15)]
  249. [TestCase("mongodb://localhost?connectTimeoutMS=15", 15)]
  250. [TestCase("mongodb://localhost?connectTimeout=15", 1000 * 15)]
  251. [TestCase("mongodb://localhost?connectTimeout=15s", 1000 * 15)]
  252. [TestCase("mongodb://localhost?connectTimeout=15m", 1000 * 60 * 15)]
  253. [TestCase("mongodb://localhost?connectTimeout=15h", 1000 * 60 * 60 * 15)]
  254. public void When_connect_timeout_is_specified(string connectionString, int milliseconds)
  255. {
  256. var subject = new ConnectionString(connectionString);
  257. subject.ConnectTimeout.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  258. }
  259. [Test]
  260. [TestCase("mongodb://localhost/awesome", "awesome")]
  261. [TestCase("mongodb://localhost/awesome/", "awesome")]
  262. public void When_a_database_name_is_specified(string connectionString, string db)
  263. {
  264. var subject = new ConnectionString(connectionString);
  265. subject.DatabaseName.Should().Be(db);
  266. }
  267. [Test]
  268. [TestCase("mongodb://localhost?fsync=true", true)]
  269. [TestCase("mongodb://localhost?fsync=false", false)]
  270. public void When_fsync_is_specified(string connectionString, bool fsync)
  271. {
  272. var subject = new ConnectionString(connectionString);
  273. subject.FSync.Should().Be(fsync);
  274. }
  275. [Test]
  276. [TestCase("mongodb://localhost?gssapiServiceName=serviceName", "serviceName")]
  277. [TestCase("mongodb://localhost?gssapiServiceName=mongodb", "mongodb")]
  278. [TestCase("mongodb://localhost?authMechanismProperties=SERVICE_NAME:serviceName", "serviceName")]
  279. public void When_gssapiServiceName_is_specified(string connectionString, string gssapiServiceName)
  280. {
  281. var subject = new ConnectionString(connectionString);
  282. subject.AuthMechanismProperties["service_name"].Should().Be(gssapiServiceName);
  283. }
  284. [Test]
  285. [TestCase("mongodb://localhost?ipv6=true", true)]
  286. [TestCase("mongodb://localhost?ipv6=false", false)]
  287. public void When_ipv6_is_specified(string connectionString, bool ipv6)
  288. {
  289. var subject = new ConnectionString(connectionString);
  290. subject.Ipv6.Should().Be(ipv6);
  291. }
  292. [Test]
  293. [TestCase("mongodb://localhost?j=true", true)]
  294. [TestCase("mongodb://localhost?j=false", false)]
  295. public void When_j_is_specified(string connectionString, bool j)
  296. {
  297. var subject = new ConnectionString(connectionString);
  298. subject.Journal.Should().Be(j);
  299. }
  300. [Test]
  301. [TestCase("mongodb://localhost?maxIdleTime=15ms", 15)]
  302. [TestCase("mongodb://localhost?maxIdleTimeMS=15", 15)]
  303. [TestCase("mongodb://localhost?maxIdleTime=15", 1000 * 15)]
  304. [TestCase("mongodb://localhost?maxIdleTime=15s", 1000 * 15)]
  305. [TestCase("mongodb://localhost?maxIdleTime=15m", 1000 * 60 * 15)]
  306. [TestCase("mongodb://localhost?maxIdleTime=15h", 1000 * 60 * 60 * 15)]
  307. public void When_maxIdleTime_is_specified(string connectionString, int milliseconds)
  308. {
  309. var subject = new ConnectionString(connectionString);
  310. subject.MaxIdleTime.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  311. }
  312. [Test]
  313. [TestCase("mongodb://localhost?maxLifeTime=15ms", 15)]
  314. [TestCase("mongodb://localhost?maxLifeTimeMS=15", 15)]
  315. [TestCase("mongodb://localhost?maxLifeTime=15", 1000 * 15)]
  316. [TestCase("mongodb://localhost?maxLifeTime=15s", 1000 * 15)]
  317. [TestCase("mongodb://localhost?maxLifeTime=15m", 1000 * 60 * 15)]
  318. [TestCase("mongodb://localhost?maxLifeTime=15h", 1000 * 60 * 60 * 15)]
  319. public void When_maxLifeTime_is_specified(string connectionString, int milliseconds)
  320. {
  321. var subject = new ConnectionString(connectionString);
  322. subject.MaxLifeTime.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  323. }
  324. [Test]
  325. [TestCase("mongodb://localhost?maxPoolSize=-1", -1)]
  326. [TestCase("mongodb://localhost?maxPoolSize=0", 0)]
  327. [TestCase("mongodb://localhost?maxPoolSize=1", 1)]
  328. [TestCase("mongodb://localhost?maxPoolSize=20", 20)]
  329. public void When_maxPoolSize_is_specified(string connectionString, int maxPoolSize)
  330. {
  331. var subject = new ConnectionString(connectionString);
  332. subject.MaxPoolSize.Should().Be(maxPoolSize);
  333. }
  334. [Test]
  335. [TestCase("mongodb://localhost?minPoolSize=-1", -1)]
  336. [TestCase("mongodb://localhost?minPoolSize=0", 0)]
  337. [TestCase("mongodb://localhost?minPoolSize=1", 1)]
  338. [TestCase("mongodb://localhost?minPoolSize=20", 20)]
  339. public void When_minPoolSize_is_specified(string connectionString, int minPoolSize)
  340. {
  341. var subject = new ConnectionString(connectionString);
  342. subject.MinPoolSize.Should().Be(minPoolSize);
  343. }
  344. [Test]
  345. [TestCase("mongodb://a:yes@localhost", "yes")]
  346. [TestCase("mongodb://a:password@localhost", "password")]
  347. [TestCase("mongodb://a:@localhost", "")]
  348. public void When_password_is_specified(string connectionString, string password)
  349. {
  350. var subject = new ConnectionString(connectionString);
  351. subject.Password.Should().Be(password);
  352. }
  353. [Test]
  354. [TestCase("mongodb://localhost?readPreference=primary", ReadPreferenceMode.Primary)]
  355. [TestCase("mongodb://localhost?readPreference=primaryPreferred", ReadPreferenceMode.PrimaryPreferred)]
  356. [TestCase("mongodb://localhost?readPreference=secondaryPreferred", ReadPreferenceMode.SecondaryPreferred)]
  357. [TestCase("mongodb://localhost?readPreference=secondary", ReadPreferenceMode.Secondary)]
  358. [TestCase("mongodb://localhost?readPreference=nearest", ReadPreferenceMode.Nearest)]
  359. public void When_readPreference_is_specified(string connectionString, ReadPreferenceMode readPreference)
  360. {
  361. var subject = new ConnectionString(connectionString);
  362. subject.ReadPreference.Should().Be(readPreference);
  363. }
  364. [Test]
  365. public void When_one_set_of_readPreferenceTags_is_specified()
  366. {
  367. var subject = new ConnectionString("mongodb://localhost?readPreferenceTags=dc:east,rack:1");
  368. var tagSet = new TagSet(new List<Tag>
  369. {
  370. new Tag("dc", "east"),
  371. new Tag("rack", "1")
  372. });
  373. subject.ReadPreferenceTags.Count.Should().Be(1);
  374. Assert.AreEqual(tagSet, subject.ReadPreferenceTags.Single());
  375. }
  376. [Test]
  377. public void When_two_sets_of_readPreferenceTags_are_specified()
  378. {
  379. var subject = new ConnectionString("mongodb://localhost?readPreferenceTags=dc:east,rack:1&readPreferenceTags=dc:west,rack:2");
  380. var tagSet1 = new TagSet(new List<Tag>
  381. {
  382. new Tag("dc", "east"),
  383. new Tag("rack", "1")
  384. });
  385. var tagSet2 = new TagSet(new List<Tag>
  386. {
  387. new Tag("dc", "west"),
  388. new Tag("rack", "2")
  389. });
  390. subject.ReadPreferenceTags.Count.Should().Be(2);
  391. subject.ReadPreferenceTags[0].Should().Be(tagSet1);
  392. subject.ReadPreferenceTags[1].Should().Be(tagSet2);
  393. }
  394. [Test]
  395. [TestCase("mongodb://localhost?replicaSet=yeah", "yeah")]
  396. public void When_replicaSet_is_specified(string connectionString, string replicaSet)
  397. {
  398. var subject = new ConnectionString(connectionString);
  399. subject.ReplicaSet.Should().Be(replicaSet);
  400. }
  401. [Test]
  402. [TestCase("mongodb://localhost/?safe=false", 0)]
  403. [TestCase("mongodb://localhost/?w=1;safe=false", 0)]
  404. [TestCase("mongodb://localhost/?w=2;safe=false", 0)]
  405. [TestCase("mongodb://localhost/?w=mode;safe=false", 0)]
  406. [TestCase("mongodb://localhost/?safe=true", 1)]
  407. [TestCase("mongodb://localhost/?w=0;safe=true", 1)]
  408. [TestCase("mongodb://localhost/?w=2;safe=true", 2)]
  409. [TestCase("mongodb://localhost/?w=mode;safe=true", "mode")]
  410. public void When_safe_is_specified(string connectionString, object wobj)
  411. {
  412. var expectedW = (wobj == null) ? null : (wobj is int) ? (WriteConcern.WValue)(int)wobj : (string)wobj;
  413. var subject = new ConnectionString(connectionString);
  414. subject.W.Should().Be(expectedW);
  415. }
  416. [Test]
  417. [TestCase("mongodb://localhost?localThreshold=15ms", 15)]
  418. [TestCase("mongodb://localhost?localThresholdMS=15", 15)]
  419. [TestCase("mongodb://localhost?localThreshold=15", 1000 * 15)]
  420. [TestCase("mongodb://localhost?localThreshold=15s", 1000 * 15)]
  421. [TestCase("mongodb://localhost?localThreshold=15m", 1000 * 60 * 15)]
  422. [TestCase("mongodb://localhost?localThreshold=15h", 1000 * 60 * 60 * 15)]
  423. public void When_localThreshold_is_specified(string connectionString, int milliseconds)
  424. {
  425. var subject = new ConnectionString(connectionString);
  426. subject.LocalThreshold.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  427. }
  428. [Test]
  429. [TestCase("mongodb://localhost?secondaryAcceptableLatency=15ms", 15)]
  430. [TestCase("mongodb://localhost?secondaryAcceptableLatencyMS=15", 15)]
  431. [TestCase("mongodb://localhost?secondaryAcceptableLatency=15", 1000 * 15)]
  432. [TestCase("mongodb://localhost?secondaryAcceptableLatency=15s", 1000 * 15)]
  433. [TestCase("mongodb://localhost?secondaryAcceptableLatency=15m", 1000 * 60 * 15)]
  434. [TestCase("mongodb://localhost?secondaryAcceptableLatency=15h", 1000 * 60 * 60 * 15)]
  435. public void When_secondaryAcceptableLatency_is_specified(string connectionString, int milliseconds)
  436. {
  437. var subject = new ConnectionString(connectionString);
  438. subject.LocalThreshold.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  439. }
  440. [Test]
  441. [TestCase("mongodb://localhost?serverSelectionTimeout=15ms", 15)]
  442. [TestCase("mongodb://localhost?serverSelectionTimeoutMS=15", 15)]
  443. [TestCase("mongodb://localhost?serverSelectionTimeout=15", 1000 * 15)]
  444. [TestCase("mongodb://localhost?serverSelectionTimeout=15s", 1000 * 15)]
  445. [TestCase("mongodb://localhost?serverSelectionTimeout=15m", 1000 * 60 * 15)]
  446. [TestCase("mongodb://localhost?serverSelectionTimeout=15h", 1000 * 60 * 60 * 15)]
  447. public void When_serverSelectionTimeout_is_specified(string connectionString, int milliseconds)
  448. {
  449. var subject = new ConnectionString(connectionString);
  450. subject.ServerSelectionTimeout.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  451. }
  452. [Test]
  453. [TestCase("mongodb://localhost?socketTimeout=15ms", 15)]
  454. [TestCase("mongodb://localhost?socketTimeoutMS=15", 15)]
  455. [TestCase("mongodb://localhost?socketTimeout=15", 1000 * 15)]
  456. [TestCase("mongodb://localhost?socketTimeout=15s", 1000 * 15)]
  457. [TestCase("mongodb://localhost?socketTimeout=15m", 1000 * 60 * 15)]
  458. [TestCase("mongodb://localhost?socketTimeout=15h", 1000 * 60 * 60 * 15)]
  459. public void When_socketTimeout_is_specified(string connectionString, int milliseconds)
  460. {
  461. var subject = new ConnectionString(connectionString);
  462. subject.SocketTimeout.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  463. }
  464. [Test]
  465. [TestCase("mongodb://localhost?ssl=true", true)]
  466. [TestCase("mongodb://localhost?ssl=false", false)]
  467. public void When_ssl_is_specified(string connectionString, bool ssl)
  468. {
  469. var subject = new ConnectionString(connectionString);
  470. subject.Ssl.Should().Be(ssl);
  471. }
  472. [Test]
  473. [TestCase("mongodb://localhost?sslVerifyCertificate=true", true)]
  474. [TestCase("mongodb://localhost?sslVerifyCertificate=false", false)]
  475. public void When_sslVerifyCertificate_is_specified(string connectionString, bool sslVerifyCertificate)
  476. {
  477. var subject = new ConnectionString(connectionString);
  478. subject.SslVerifyCertificate.Should().Be(sslVerifyCertificate);
  479. }
  480. [Test]
  481. [TestCase("mongodb://yes@localhost", "yes")]
  482. [TestCase("mongodb://username@localhost", "username")]
  483. public void When_username_is_specified(string connectionString, string username)
  484. {
  485. var subject = new ConnectionString(connectionString);
  486. subject.Username.Should().Be(username);
  487. }
  488. [Test]
  489. [TestCase("mongodb://localhost?uuidRepresentation=standard", GuidRepresentation.Standard)]
  490. [TestCase("mongodb://localhost?guids=standard", GuidRepresentation.Standard)]
  491. [TestCase("mongodb://localhost?uuidRepresentation=csharpLegacy", GuidRepresentation.CSharpLegacy)]
  492. [TestCase("mongodb://localhost?guids=csharpLegacy", GuidRepresentation.CSharpLegacy)]
  493. [TestCase("mongodb://localhost?uuidRepresentation=javaLegacy", GuidRepresentation.JavaLegacy)]
  494. [TestCase("mongodb://localhost?guids=javaLegacy", GuidRepresentation.JavaLegacy)]
  495. [TestCase("mongodb://localhost?uuidRepresentation=pythonLegacy", GuidRepresentation.PythonLegacy)]
  496. [TestCase("mongodb://localhost?guids=pythonLegacy", GuidRepresentation.PythonLegacy)]
  497. public void When_uuidRepresentation_is_specified(string connectionString, GuidRepresentation representation)
  498. {
  499. var subject = new ConnectionString(connectionString);
  500. subject.UuidRepresentation.Should().Be(representation);
  501. }
  502. [Test]
  503. [TestCase("mongodb://localhost?w=0", "0")]
  504. [TestCase("mongodb://localhost?w=1", "1")]
  505. [TestCase("mongodb://localhost?w=majority", "majority")]
  506. public void When_w_is_specified(string connectionString, string w)
  507. {
  508. var subject = new ConnectionString(connectionString);
  509. var expectedW = WriteConcern.WValue.Parse(w);
  510. subject.W.Should().Be(expectedW);
  511. }
  512. [Test]
  513. [TestCase("mongodb://localhost?wtimeout=15ms", 15)]
  514. [TestCase("mongodb://localhost?wtimeoutMS=15", 15)]
  515. [TestCase("mongodb://localhost?wtimeout=15", 1000 * 15)]
  516. [TestCase("mongodb://localhost?wtimeout=15s", 1000 * 15)]
  517. [TestCase("mongodb://localhost?wtimeout=15m", 1000 * 60 * 15)]
  518. [TestCase("mongodb://localhost?wtimeout=15h", 1000 * 60 * 60 * 15)]
  519. public void When_wtimeout_is_specified(string connectionString, int milliseconds)
  520. {
  521. var subject = new ConnectionString(connectionString);
  522. subject.WTimeout.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  523. }
  524. [Test]
  525. [TestCase("mongodb://localhost?waitQueueMultiple=-1", -1)]
  526. [TestCase("mongodb://localhost?waitQueueMultiple=0", 0)]
  527. [TestCase("mongodb://localhost?waitQueueMultiple=1", 1)]
  528. [TestCase("mongodb://localhost?waitQueueMultiple=20", 20)]
  529. [TestCase("mongodb://localhost?waitQueueMultiple=2.3", 2.3)]
  530. public void When_waitQueueMultiple_is_specified(string connectionString, double waitQueueMultiple)
  531. {
  532. var subject = new ConnectionString(connectionString);
  533. subject.WaitQueueMultiple.Should().Be(waitQueueMultiple);
  534. }
  535. [Test]
  536. [TestCase("mongodb://localhost?waitQueueSize=-1", -1)]
  537. [TestCase("mongodb://localhost?waitQueueSize=0", 0)]
  538. [TestCase("mongodb://localhost?waitQueueSize=1", 1)]
  539. [TestCase("mongodb://localhost?waitQueueSize=20", 20)]
  540. public void When_waitQueueSize_is_specified(string connectionString, int waitQueueSize)
  541. {
  542. var subject = new ConnectionString(connectionString);
  543. subject.WaitQueueSize.Should().Be(waitQueueSize);
  544. }
  545. [Test]
  546. [TestCase("mongodb://localhost?waitQueueTimeout=15ms", 15)]
  547. [TestCase("mongodb://localhost?waitQueueTimeoutMS=15", 15)]
  548. [TestCase("mongodb://localhost?waitQueueTimeout=15", 1000 * 15)]
  549. [TestCase("mongodb://localhost?waitQueueTimeout=15s", 1000 * 15)]
  550. [TestCase("mongodb://localhost?waitQueueTimeout=15m", 1000 * 60 * 15)]
  551. [TestCase("mongodb://localhost?waitQueueTimeout=15h", 1000 * 60 * 60 * 15)]
  552. public void When_waitQueueTimeout_is_specified(string connectionString, int milliseconds)
  553. {
  554. var subject = new ConnectionString(connectionString);
  555. subject.WaitQueueTimeout.Should().Be(TimeSpan.FromMilliseconds(milliseconds));
  556. }
  557. [Test]
  558. public void When_uknown_options_exist()
  559. {
  560. var subject = new ConnectionString("mongodb://localhost?one=1;two=2");
  561. subject.AllUnknownOptionNames.Count().Should().Be(2);
  562. subject.AllUnknownOptionNames.Should().Contain("one");
  563. subject.AllUnknownOptionNames.Should().Contain("two");
  564. subject.GetOption("one").Should().Be("1");
  565. subject.GetOption("two").Should().Be("2");
  566. }
  567. }
  568. }