Skip to content

Commit

Permalink
fixed clone bug in Mojo::Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 24, 2012
1 parent 04b2a9f commit 414ac32
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -4,6 +4,7 @@
- Improved support for relative redirects in Mojo::UserAgent::Transactor.
- Improved documentation.
- Improved tests.
- Fixed clone bug in Mojo::Parameters.

3.60 2012-11-22
- Added unexpected event to Mojo::Transaction::HTTP.
Expand Down
7 changes: 5 additions & 2 deletions lib/Mojo/Parameters.pm
Expand Up @@ -31,10 +31,13 @@ sub append {
}

sub clone {
my $self = shift;
my $clone = Mojo::Parameters->new->pair_separator($self->pair_separator);
my $self = shift;

my $clone = Mojo::Parameters->new->charset($self->charset)
->pair_separator($self->pair_separator);
if (defined $self->{string}) { $clone->{string} = $self->{string} }
else { $clone->params([@{$self->params}]) }

return $clone;
}

Expand Down
10 changes: 6 additions & 4 deletions lib/Mojo/Path.pm
Expand Up @@ -75,7 +75,8 @@ sub parse {
my ($self, $path) = @_;

$path = url_unescape $path // '';
$path = decode($self->charset, $path) // $path;
my $charset = $self->charset;
$path = decode($charset, $path) // $path if $charset;
$self->leading_slash($path =~ s!^/!! ? 1 : undef);
$self->trailing_slash($path =~ s!/$!! ? 1 : undef);

Expand All @@ -90,10 +91,11 @@ sub to_abs_string {
sub to_string {
my $self = shift;

my $chars = '^A-Za-z0-9\-._~!$&\'()*+,;=:@';
my @parts = @{$self->parts};
my $charset = $self->charset;
my @parts = map { url_escape(encode($charset, $_), $chars) } @{$self->parts};
my $path = join '/', @parts;
@parts = map { encode $charset, $_ } @parts if $charset;
my $path = join '/',
map { url_escape $_, '^A-Za-z0-9\-._~!$&\'()*+,;=:@' } @parts;
$path = "/$path" if $self->leading_slash;
$path = "$path/" if $self->trailing_slash;

Expand Down
7 changes: 4 additions & 3 deletions t/mojo/parameters.t
Expand Up @@ -199,8 +199,9 @@ is $p->param('bar'), 23, 'right value';
is "$p", '!$\'()*,:@/foo?=!$\'()*,:@/?&bar=23', 'right result';

# No charset
$p = Mojo::Parameters->new('foo=%E2%98%83')->charset(undef);
is $p->param('foo'), "\xe2\x98\x83", 'right value';
is "$p", 'foo=%E2%98%83', 'right result';
$p = Mojo::Parameters->new('foo=%E4')->charset(undef);
is $p->param('foo'), "\xe4", 'right value';
is "$p", 'foo=%E4', 'right result';
is $p->clone->to_string, 'foo=%E4', 'right result';

done_testing();
11 changes: 11 additions & 0 deletions t/mojo/path.t
Expand Up @@ -278,9 +278,20 @@ is $path->to_abs_string, '/foo%2Fbar', 'right result';
$path = Mojo::Path->new->charset('Latin-1')->parse('/foob%E4r');
is $path->parts->[0], 'foobär', 'right part';
is $path->parts->[1], undef, 'no part';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
is "$path", '/foob%E4r', 'right result';
is $path->to_string, '/foob%E4r', 'right result';
is $path->to_abs_string, '/foob%E4r', 'right result';
is $path->clone->to_string, '/foob%E4r', 'right result';

# No charset
$path = Mojo::Path->new->charset(undef)->parse('/%E4');
is $path->parts->[0], "\xe4", 'right part';
is $path->parts->[1], undef, 'no part';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
is "$path", '/%E4', 'right result';
is $path->clone->to_string, '/%E4', 'right result';

done_testing();

0 comments on commit 414ac32

Please sign in to comment.