Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for rt70740 #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 13 additions & 20 deletions URI/QueryParam.pm
Expand Up @@ -8,38 +8,30 @@ sub URI::_query::query_param {


if (@_ == 0) { if (@_ == 0) {
# get keys # get keys
my %seen; my (%seen, $i);
my @keys; return grep !($i++ % 2 || $seen{$_}++), @old;
for (my $i = 0; $i < @old; $i += 2) {
push(@keys, $old[$i]) unless $seen{$old[$i]}++;
}
return @keys;
} }


my $key = shift; my $key = shift;
my @i; my @i = grep $_ % 2 == 0 && $old[$_] eq $key, 0 .. $#old;

for (my $i = 0; $i < @old; $i += 2) {
push(@i, $i) if $old[$i] eq $key;
}


if (@_) { if (@_) {
my @new = @old; my @new = @old;
my @new_i = @i; my @new_i = @i;
my @vals = map { ref($_) eq 'ARRAY' ? @$_ : $_ } @_; my @vals = map { ref($_) eq 'ARRAY' ? @$_ : $_ } @_;
#print "VALS:@vals [@i]\n";
while (@new_i > @vals) { while (@new_i > @vals) {
#print "REMOVE $new_i[-1]\n"; splice @new, pop @new_i, 2;
splice(@new, pop(@new_i), 2);
} }
while (@vals > @new_i) { if (@vals > @new_i) {
my $i = @new_i ? $new_i[-1] + 2 : @new; my $i = @new_i ? $new_i[-1] + 2 : @new;
#print "SPLICE $i\n"; my @splice = splice @vals, @new_i, @vals - @new_i;
splice(@new, $i, 0, $key => pop(@vals));
splice @new, $i, 0, map { $key => $_ } @splice;
} }
for (@vals) { if (@vals) {
#print "SET $new_i[0]\n"; #print "SET $new_i[0]\n";
$new[shift(@new_i)+1] = $_; @new[ map $_ + 1, @new_i ] = @vals;
} }


$self->query_form(\@new); $self->query_form(\@new);
Expand All @@ -51,7 +43,8 @@ sub URI::_query::query_param {
sub URI::_query::query_param_append { sub URI::_query::query_param_append {
my $self = shift; my $self = shift;
my $key = shift; my $key = shift;
$self->query_form($self->query_form, $key => \@_); # XXX my @vals = map { ref $_ eq 'ARRAY' ? @$_ : $_ } @_;
$self->query_form($self->query_form, $key => \@vals); # XXX
return; return;
} }


Expand Down
65 changes: 25 additions & 40 deletions t/query-param.t
@@ -1,87 +1,72 @@
#!perl -w #!perl -w


print "1..18\n";

use strict; use strict;


use Test::More tests => 19;

use URI; use URI;
use URI::QueryParam; use URI::QueryParam;


my $u = URI->new("http://www.sol.no?foo=4&bar=5&foo=5"); my $u = URI->new("http://www.sol.no?foo=4&bar=5&foo=5");


my $h = $u->query_form_hash; is_deeply(
print "not " unless $h->{foo}[0] eq "4" && $h->{foo}[1] eq "5" && $h->{bar} eq "5"; $u->query_form_hash,
print "ok 1\n"; { foo => [ 4, 5 ], bar => 5 },
'query_form_hash get'
);


$u->query_form_hash({ a => 1, b => 2}); $u->query_form_hash({ a => 1, b => 2});
print "not " unless $u->query eq "a=1&b=2" || $u->query eq "b=2&a=1"; ok $u->query eq "a=1&b=2" || $u->query eq "b=2&a=1", 'query_form_hash set';
print "ok 2\n";


$u->query("a=1&b=2&a=3&b=4&a=5"); $u->query("a=1&b=2&a=3&b=4&a=5");
print "not " unless $u->query_param == 2 && join(":", $u->query_param) eq "a:b"; is join(':', $u->query_param), "a:b", 'query_param list keys';
print "ok 3\n";


print "not " unless $u->query_param("a") eq "1" && is $u->query_param("a"), "1", "query_param scalar return";
join(":", $u->query_param("a")) eq "1:3:5"; is join(":", $u->query_param("a")), "1:3:5", "query_param list return";
print "ok 4\n";


print "not " unless $u->query_param(a => 11 .. 14) eq "1"; is $u->query_param(a => 11 .. 15), 1, "query_param set return";
print "ok 5\n";


print "not " unless $u->query eq "a=11&b=2&a=12&b=4&a=13&a=14"; is $u->query, "a=11&b=2&a=12&b=4&a=13&a=14&a=15", "param order";
print "ok 6\n";


print "not " unless join(":", $u->query_param(a => 11)) eq "11:12:13:14"; is join(":", $u->query_param(a => 11)), "11:12:13:14:15", "old values returned";
print "ok 7\n";


print "not " unless $u->query eq "a=11&b=2&b=4"; is $u->query, "a=11&b=2&b=4";
print "ok 8\n";


print "not " unless $u->query_param_delete("a") eq "11"; is $u->query_param_delete("a"), "11", 'query_param_delete';
print "ok 9\n";


print "not " unless $u->query eq "b=2&b=4"; is $u->query, "b=2&b=4";
print "ok 10\n";


$u->query_param_append(a => 1, 3, 5); $u->query_param_append(a => 1, 3, 5);
$u->query_param_append(b => 6); $u->query_param_append(b => 6);


print "not " unless $u->query eq "b=2&b=4&a=1&a=3&a=5&b=6"; is $u->query, "b=2&b=4&a=1&a=3&a=5&b=6";
print "ok 11\n";


$u->query_param(a => []); # same as $u->query_param_delete("a"); $u->query_param(a => []); # same as $u->query_param_delete("a");


print "not " unless $u->query eq "b=2&b=4&b=6"; is $u->query, "b=2&b=4&b=6", 'delete by assigning empty list';
print "ok 12\n";


$u->query(undef); $u->query(undef);
$u->query_param(a => 1, 2, 3); $u->query_param(a => 1, 2, 3);
$u->query_param(b => 1); $u->query_param(b => 1);


print "not " unless $u->query eq 'a=3&a=2&a=1&b=1'; is $u->query, 'a=1&a=2&a=3&b=1', 'query_param from scratch';
print "ok 13\n";


$u->query_param_delete('a'); $u->query_param_delete('a');
$u->query_param_delete('b'); $u->query_param_delete('b');


print "not " if $u->query; ok ! $u->query;
print "ok 14\n";


print "not " unless $u->as_string eq 'http://www.sol.no'; is $u->as_string, 'http://www.sol.no';
print "ok 15\n";


$u->query(undef); $u->query(undef);
$u->query_param(a => 1, 2, 3); $u->query_param(a => 1, 2, 3);
$u->query_param(b => 1); $u->query_param(b => 1);


print "not " unless $u->query eq 'a=3&a=2&a=1&b=1'; is $u->query, 'a=1&a=2&a=3&b=1';
print "ok 16\n";


$u->query_param('a' => []); $u->query_param('a' => []);
$u->query_param('b' => []); $u->query_param('b' => []);


print "not " if $u->query; ok ! $u->query;
print "ok 17\n";


print "not " unless $u->as_string eq 'http://www.sol.no'; is $u->as_string, 'http://www.sol.no';
print "ok 18\n";