1
+ # Copyright 2019 - present 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
+ use strict;
16
+ use warnings;
17
+ use Test::More;
18
+
19
+ {
20
+ package Test::MongoDB::_URI ;
21
+ use Moo;
22
+ extends ' MongoDB::_URI' ;
23
+
24
+ has _test_seedlist_args => (
25
+ is => ' rw' ,
26
+ );
27
+
28
+ has _test_seedlist_return => (
29
+ is => ' rw' ,
30
+ default => sub {[]},
31
+ );
32
+
33
+ # Net::DNS is an optional dependency, so the original cannot
34
+ # actually be run in the general test instance.
35
+ # Doesnt stop us from stringing it up for tests though!
36
+ sub _fetch_dns_seedlist {
37
+ my ( $self , @args ) = @_ ;
38
+ $self -> _test_seedlist_args([@args ]);
39
+ return @{ $self -> _test_seedlist_return };
40
+ }
41
+ }
42
+
43
+ my $class = ' Test::MongoDB::_URI' ;
44
+
45
+ subtest " boolean params unchanged" => sub {
46
+ my $uri = new_ok( $class , [
47
+ uri => ' mongodb+srv://testmongo.example.com/?ssl=true' ,
48
+ _test_seedlist_return => [
49
+ [{ target => ' localhost' , port => 27017 }],
50
+ {
51
+ retryWrites => ' true' ,
52
+ retryReads => ' false' ,
53
+ },
54
+ 0
55
+ ]
56
+ ]);
57
+
58
+ is_deeply $uri -> _test_seedlist_args, [ ' testmongo.example.com' , ' init' ],
59
+ ' fetch_dns_seedlist called correctly' ;
60
+
61
+ is_deeply $uri -> hostids, [ ' localhost:27017' ],
62
+ " hostids correct" ;
63
+
64
+ is_deeply $uri -> options,
65
+ { ssl => 1, retrywrites => 1, retryreads => 0 },
66
+ " options correct" ;
67
+
68
+ subtest " force call srv parsing" => sub {
69
+ $uri -> _test_seedlist_return([
70
+ [{ target => ' localhost' , port => 27019 }],
71
+ {
72
+ retryWrites => ' false' ,
73
+ retryReads => ' true' ,
74
+ },
75
+ 1
76
+ ]);
77
+ my ( $new_uri , $expires ) = $uri -> _parse_srv_uri( ' mongodb+srv://testmongo2.example.com/?ssl=true' , ' init' );
78
+
79
+ is_deeply $uri -> _test_seedlist_args, [ ' testmongo2.example.com' , ' init' ],
80
+ ' fetch_dns_seedlist called correctly' ;
81
+
82
+ like $new_uri , qr ! ^mongodb://localhost:27019/?! , ' URI Host correct' ;
83
+
84
+ # Cannot use straight comparison as options are hash shuffled
85
+ my ($readsVal ) = $new_uri =~ qr / retryReads=(\w *)/ ;
86
+ is $readsVal , ' true' , ' Retry Reads true' ;
87
+ my ($writesVal ) = $new_uri =~ qr / retryWrites=(\w *)/ ;
88
+ is $writesVal , ' false' , ' Retry Writes false' ;
89
+ my ($sslVal ) = $new_uri =~ qr / ssl=(\w *)/ ;
90
+ is $sslVal , ' true' , ' SSL true' ;
91
+
92
+ is $expires , 1, ' expires as expected' ;
93
+ }
94
+ };
95
+
96
+ done_testing;
0 commit comments