Skip to content

Commit c82584e

Browse files
committed
CSHARP-2459: Handle ssl option correctly also when mongodb+srv is used.
1 parent 0f292c2 commit c82584e

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Please see our [guidelines](CONTRIBUTING.md) for contributing to the driver.
8989
* Bar Arnon https://github.com/I3arnon
9090
* Wan Bachtiar https://github.com/sindbach
9191
* Mark Benvenuto https://github.com/markbenvenuto
92+
* Ethan Celletti https://github.com/Gekctek
9293
* Bit Diffusion Limited [email protected]
9394
* Nima Boscarino https://github.com/NimaBoscarino
9495
* Oscar Bralo https://github.com/Oscarbralo

src/MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,19 @@ public override string ToString()
793793
{
794794
query.AppendFormat("ipv6=true;");
795795
}
796-
if (_useSsl)
796+
if (_scheme == ConnectionStringScheme.MongoDBPlusSrv)
797797
{
798-
query.AppendFormat("ssl=true;");
798+
if (!_useSsl)
799+
{
800+
query.AppendFormat("ssl=false;");
801+
}
802+
}
803+
else
804+
{
805+
if (_useSsl)
806+
{
807+
query.AppendFormat("ssl=true;");
808+
}
799809
}
800810
if (!_verifySslCertificate)
801811
{

tests/MongoDB.Driver.Tests/MongoUrlBuilderTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,13 +1206,23 @@ public void TestWTimeout_Range()
12061206
builder.WTimeout = TimeSpan.FromSeconds(1);
12071207
}
12081208

1209-
[Fact]
1210-
public void TestToString_WithSrvSpecified_ParsedSameConnectionString()
1211-
{
1212-
const string connectionString = "mongodb+srv://localhost";
1213-
var builder = new MongoUrlBuilder(connectionString);
1214-
var builder2 = new MongoUrlBuilder(builder.ToString());
1215-
Assert.Equal(builder.ToString(), builder2.ToString());
1209+
[Theory]
1210+
[InlineData("mongodb://localhost", "mongodb://localhost")]
1211+
[InlineData("mongodb://localhost/?ssl=false", "mongodb://localhost")]
1212+
[InlineData("mongodb://localhost/?ssl=true", "mongodb://localhost/?ssl=true")]
1213+
[InlineData("mongodb://localhost:27018", "mongodb://localhost:27018")]
1214+
[InlineData("mongodb://localhost:27018/?ssl=false", "mongodb://localhost:27018")]
1215+
[InlineData("mongodb://localhost:27018/?ssl=true", "mongodb://localhost:27018/?ssl=true")]
1216+
[InlineData("mongodb+srv://localhost", "mongodb+srv://localhost")]
1217+
[InlineData("mongodb+srv://localhost/?ssl=false", "mongodb+srv://localhost/?ssl=false")]
1218+
[InlineData("mongodb+srv://localhost/?ssl=true", "mongodb+srv://localhost")]
1219+
public void ToString_should_return_expected_result_for_scheme_port_and_ssl(string connectionString, string expectedResult)
1220+
{
1221+
var subject = new MongoUrlBuilder(connectionString);
1222+
1223+
var result = subject.ToString();
1224+
1225+
result.Should().Be(expectedResult);
12161226
}
12171227

12181228
// private methods

0 commit comments

Comments
 (0)