@@ -554,12 +554,15 @@ def test_port_with_whitespace(self):
554
554
with self .assertRaisesRegex (ValueError , r"Port contains whitespace character: '\\n'" ):
555
555
parse_uri ("mongodb://localhost:27\n 017" )
556
556
557
- def test_allow_srv_hosts_with_fewer_than_three_dot_separated_parts (self ):
557
+ # Initial DNS Seedlist Discovery prose tests
558
+ # https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
559
+
560
+ def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts (self ):
558
561
with patch ("dns.resolver.resolve" ):
559
562
parse_uri ("mongodb+srv://localhost/" )
560
563
parse_uri ("mongodb+srv://mongo.local/" )
561
564
562
- def test_error_when_return_address_does_not_end_with_srv_domain (self ):
565
+ def test_2_throw_when_return_address_does_not_end_with_srv_domain (self ):
563
566
test_cases = [
564
567
{
565
568
"query" : "_mongodb._tcp.localhost" ,
@@ -576,6 +579,27 @@ def test_error_when_return_address_does_not_end_with_srv_domain(self):
576
579
"mock_target" : "test_1.evil.com" ,
577
580
"expected_error" : "Invalid SRV host" ,
578
581
},
582
+ ]
583
+ for case in test_cases :
584
+ with patch ("dns.resolver.resolve" ) as mock_resolver :
585
+
586
+ def mock_resolve (query , record_type , * args , ** kwargs ):
587
+ mock_srv = MagicMock ()
588
+ mock_srv .target .to_text .return_value = case ["mock_target" ]
589
+ return [mock_srv ]
590
+
591
+ mock_resolver .side_effect = mock_resolve
592
+ domain = case ["query" ].split ("._tcp." )[1 ]
593
+ connection_string = f"mongodb+srv://{ domain } "
594
+ try :
595
+ parse_uri (connection_string )
596
+ except ConfigurationError as e :
597
+ self .assertIn (case ["expected_error" ], str (e ))
598
+ else :
599
+ self .fail (f"ConfigurationError was not raised for query: { case ['query' ]} " )
600
+
601
+ def test_3_throw_when_return_address_is_identical_to_srv_hostname (self ):
602
+ test_cases = [
579
603
{
580
604
"query" : "_mongodb._tcp.localhost" ,
581
605
"mock_target" : "localhost" ,
@@ -586,6 +610,28 @@ def test_error_when_return_address_does_not_end_with_srv_domain(self):
586
610
"mock_target" : "mongo.local" ,
587
611
"expected_error" : "Invalid SRV host" ,
588
612
},
613
+ ]
614
+ for case in test_cases :
615
+ with patch ("dns.resolver.resolve" ) as mock_resolver :
616
+
617
+ def mock_resolve (query , record_type , * args , ** kwargs ):
618
+ mock_srv = MagicMock ()
619
+ mock_srv .target .to_text .return_value = case ["mock_target" ]
620
+ return [mock_srv ]
621
+
622
+ mock_resolver .side_effect = mock_resolve
623
+ domain = case ["query" ].split ("._tcp." )[1 ]
624
+ connection_string = f"mongodb+srv://{ domain } "
625
+ try :
626
+ parse_uri (connection_string )
627
+ except ConfigurationError as e :
628
+ self .assertIn (case ["expected_error" ], str (e ))
629
+ else :
630
+ self .fail (f"ConfigurationError was not raised for query: { case ['query' ]} " )
631
+
632
+ def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain (self ):
633
+ test_cases = [
634
+
589
635
{
590
636
"query" : "_mongodb._tcp.localhost" ,
591
637
"mock_target" : "test_1.cluster_1localhost" ,
0 commit comments