Skip to content

Commit 6064af9

Browse files
committed
Added MongoClientSettings test
1 parent 01dfdb1 commit 6064af9

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/MongoDB.Driver/Core/Connections/Socks5AuthenticationSettings.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public static Socks5AuthenticationSettings UsernamePassword(string username, str
4242
/// </summary>
4343
public sealed class NoAuthenticationSettings : Socks5AuthenticationSettings
4444
{
45+
/// <inheritdoc />
46+
public override bool Equals(object obj)
47+
{
48+
return obj is Socks5AuthenticationSettings;
49+
}
50+
51+
/// <inheritdoc />
52+
public override int GetHashCode()
53+
{
54+
return 1;
55+
}
4556
}
4657

4758
/// <summary>

src/MongoDB.Driver/Core/Connections/Socks5ProxySettings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public override int GetHashCode()
8080
.GetHashCode();
8181
}
8282

83-
8483
/// <inheritdoc/>
8584
public override string ToString()
8685
{

tests/MongoDB.Driver.Tests/MongoClientSettingsTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using MongoDB.Driver.Core.Clusters;
2626
using MongoDB.Driver.Core.Compression;
2727
using MongoDB.Driver.Core.Configuration;
28+
using MongoDB.Driver.Core.Connections;
2829
using MongoDB.Driver.Core.Servers;
2930
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
3031
using MongoDB.Driver.Encryption;
@@ -233,6 +234,8 @@ public void TestConnectTimeout()
233234
Assert.Throws<InvalidOperationException>(() => { settings.ConnectTimeout = connectTimeout; });
234235
}
235236

237+
//TODO I understand we want to keep tests in alphabetical order, but I think it would make sense to group them by scope
238+
//Tests like this, that should be modified for every new setting added (for instance) should be at the top of this test suite.
236239
[Fact]
237240
public void TestDefaults()
238241
{
@@ -266,6 +269,7 @@ public void TestDefaults()
266269
Assert.Equal(ServerMonitoringMode.Auto, settings.ServerMonitoringMode);
267270
Assert.Equal(MongoDefaults.ServerSelectionTimeout, settings.ServerSelectionTimeout);
268271
Assert.Equal(MongoDefaults.SocketTimeout, settings.SocketTimeout);
272+
Assert.Equal(null, settings.Socks5ProxySettings);
269273
Assert.Null(settings.SslSettings);
270274
#pragma warning disable 618
271275
Assert.Equal(false, settings.UseSsl);
@@ -435,6 +439,10 @@ public void TestEquals()
435439
clone.SocketTimeout = new TimeSpan(1, 2, 3);
436440
Assert.False(clone.Equals(settings));
437441

442+
clone = settings.Clone();
443+
clone.Socks5ProxySettings = Socks5ProxySettings.Create("host.com", null, null, null);
444+
Assert.False(clone.Equals(settings));
445+
438446
clone = settings.Clone();
439447
clone.SslSettings = new SslSettings { CheckCertificateRevocation = false };
440448
Assert.False(clone.Equals(settings));
@@ -475,6 +483,7 @@ public void TestEquals()
475483
settings.ReadConcern = ReadConcern.Majority;
476484
settings.ReadEncoding = new UTF8Encoding(false, false);
477485
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
486+
settings.Socks5ProxySettings = Socks5ProxySettings.Create("host.com", 8080, null, null);
478487
settings.WriteConcern = WriteConcern.W2;
479488
settings.WriteEncoding = new UTF8Encoding(false, false);
480489

@@ -485,6 +494,7 @@ public void TestEquals()
485494
clone.ReadEncoding = new UTF8Encoding(false, false);
486495
clone.ReadPreference = clone.ReadPreference.With(settings.ReadPreference.ReadPreferenceMode);
487496
clone.ServerApi = new ServerApi(settings.ServerApi.Version);
497+
clone.Socks5ProxySettings = Socks5ProxySettings.Create("host.com", 8080, null, null);
488498
clone.WriteConcern = WriteConcern.FromBsonDocument(settings.WriteConcern.ToBsonDocument());
489499
clone.WriteEncoding = new UTF8Encoding(false, false);
490500

@@ -582,7 +592,8 @@ public void TestFromUrl()
582592
"maxConnecting=3;maxIdleTime=124;maxLifeTime=125;maxPoolSize=126;minPoolSize=127;readConcernLevel=majority;" +
583593
"readPreference=secondary;readPreferenceTags=a:1,b:2;readPreferenceTags=c:3,d:4;retryReads=false;retryWrites=true;socketTimeout=129;" +
584594
"serverMonitoringMode=Stream;serverSelectionTimeout=20s;tls=true;sslVerifyCertificate=false;waitqueuesize=130;waitQueueTimeout=131;" +
585-
"w=1;fsync=true;journal=true;w=2;wtimeout=131;gssapiServiceName=other";
595+
"w=1;fsync=true;journal=true;w=2;wtimeout=131;gssapiServiceName=other" +
596+
"&proxyHost=host.com&proxyPort=2020&proxyUsername=user&proxyPassword=passw";
586597
var builder = new MongoUrlBuilder(connectionString);
587598
var url = builder.ToMongoUrl();
588599

@@ -620,6 +631,10 @@ public void TestFromUrl()
620631
Assert.Equal(ServerMonitoringMode.Stream, settings.ServerMonitoringMode);
621632
Assert.Equal(url.ServerSelectionTimeout, settings.ServerSelectionTimeout);
622633
Assert.Equal(url.SocketTimeout, settings.SocketTimeout);
634+
Assert.Equal(url.ProxyHost, settings.Socks5ProxySettings.Host);
635+
Assert.Equal(url.ProxyPort, settings.Socks5ProxySettings.Port);
636+
Assert.Equal(url.ProxyUsername, ((Socks5AuthenticationSettings.UsernamePasswordAuthenticationSettings)settings.Socks5ProxySettings.Authentication).Username);
637+
Assert.Equal(url.ProxyPassword, ((Socks5AuthenticationSettings.UsernamePasswordAuthenticationSettings)settings.Socks5ProxySettings.Authentication).Password);
623638
#pragma warning disable 618
624639
Assert.Equal(url.TlsDisableCertificateRevocationCheck, !settings.SslSettings.CheckCertificateRevocation);
625640
Assert.Equal(url.UseSsl, settings.UseSsl);
@@ -1175,6 +1190,21 @@ public void TestSocketTimeout()
11751190
Assert.Throws<InvalidOperationException>(() => { settings.SocketTimeout = socketTimeout; });
11761191
}
11771192

1193+
[Fact]
1194+
public void TestSocks5ProxySettings()
1195+
{
1196+
var settings = new MongoClientSettings();
1197+
Assert.Equal(null, settings.Socks5ProxySettings);
1198+
1199+
var newProxySettings = Socks5ProxySettings.Create("host.com", 280, "test", "test");
1200+
settings.Socks5ProxySettings = newProxySettings;
1201+
Assert.Equal(newProxySettings, settings.Socks5ProxySettings);
1202+
1203+
settings.Freeze();
1204+
Assert.Equal(newProxySettings, settings.Socks5ProxySettings);
1205+
Assert.Throws<InvalidOperationException>(() => { settings.Socks5ProxySettings = newProxySettings; });
1206+
}
1207+
11781208
[Fact]
11791209
public void TestSslSettings()
11801210
{
@@ -1326,6 +1356,7 @@ public void ToClusterKey_should_copy_relevant_values()
13261356
ServerMonitoringMode = ServerMonitoringMode.Poll,
13271357
ServerSelectionTimeout = TimeSpan.FromSeconds(6),
13281358
SocketTimeout = TimeSpan.FromSeconds(4),
1359+
Socks5ProxySettings = Socks5ProxySettings.Create("host", 2020, null, null),
13291360
SslSettings = sslSettings,
13301361
UseTls = true,
13311362
#pragma warning disable 618
@@ -1362,6 +1393,7 @@ public void ToClusterKey_should_copy_relevant_values()
13621393
result.ServerMonitoringMode.Should().Be(ServerMonitoringMode.Poll);
13631394
result.ServerSelectionTimeout.Should().Be(subject.ServerSelectionTimeout);
13641395
result.SocketTimeout.Should().Be(subject.SocketTimeout);
1396+
result.Socks5ProxySettings.Should().Be(subject.Socks5ProxySettings);
13651397
result.SslSettings.Should().Be(subject.SslSettings);
13661398
result.UseTls.Should().Be(subject.UseTls);
13671399
#pragma warning disable 618

0 commit comments

Comments
 (0)