Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit e554b85

Browse files
committed
PERL-1126 - Reset str to bool for URI options during SRV re-parsing
1 parent 16b2782 commit e554b85

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

‎lib/MongoDB/_URI.pm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ sub _parse_srv_uri {
443443
%{ $result{options} || {} },
444444
};
445445

446+
# Reset str to bool options to string value, as _parse_options changes it to 0/1 if it exists during parsing
447+
# means we get the correct value when re-building the uri below.
448+
for my $stb_key ( keys %{ $self->_valid_str_to_bool_options } ) {
449+
# use exists just in case
450+
next unless exists $options->{ $stb_key };
451+
$options->{ $stb_key } = ($options->{ $stb_key } || $options->{ $stb_key } eq 'true') ? 'true' : 'false';
452+
}
453+
446454
my $auth = "";
447455
if ( defined $result{username} || defined $result{password} ) {
448456
$auth = join(":", map { $_ // "" } $result{username}, $result{password});

‎t/unit/uri_srv.t

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /