Skip to content

Commit

Permalink
added multi-name support to a few more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 31, 2014
1 parent a0dac22 commit f2b6ef9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@

5.02 2014-05-31
- Added multi-name support to cookie and signed_cookie methods in
Mojolicious::Controller.
- Added multi-name support to cookie and upload methods in Mojo::Message.
- Fixed bug where Mojo::DOM::HTML could not handle tags with lots of
attributes.

Expand Down
18 changes: 12 additions & 6 deletions lib/Mojo/Message.pm
Expand Up @@ -61,7 +61,7 @@ sub build_body { shift->_build('get_body_chunk') }
sub build_headers { shift->_build('get_header_chunk') }
sub build_start_line { shift->_build('get_start_line_chunk') }

sub cookie { shift->_cache(cookies => @_) }
sub cookie { shift->_cache(cookie => @_) }

sub cookies { croak 'Method "cookies" not implemented by subclass' }

Expand Down Expand Up @@ -194,7 +194,7 @@ sub to_string {
return $self->build_start_line . $self->build_headers . $self->build_body;
}

sub upload { shift->_cache(uploads => @_) }
sub upload { shift->_cache(upload => @_) }

sub uploads {
my $self = shift;
Expand Down Expand Up @@ -236,7 +236,11 @@ sub _build {
sub _cache {
my ($self, $method, $name) = @_;

# Multiple names
return map { scalar $self->$method($_) } @$name if ref $name eq 'ARRAY';

# Cache objects by name
$method .= 's';
unless ($self->{$method}) {
$self->{$method} = {};
push @{$self->{$method}{$_->name}}, $_ for @{$self->$method};
Expand Down Expand Up @@ -453,8 +457,9 @@ Render start line.
=head2 cookie
my $cookie = $msg->cookie('foo');
my @cookies = $msg->cookie('foo');
my $foo = $msg->cookie('foo');
my @foo = $msg->cookie('foo');
my ($foo, $bar) = $msg->cookie(['foo', 'bar']);
Access message cookies, usually L<Mojo::Cookie::Request> or
L<Mojo::Cookie::Response> objects. Note that this method caches all data, so
Expand Down Expand Up @@ -613,8 +618,9 @@ Render whole message.
=head2 upload
my $upload = $msg->upload('foo');
my @uploads = $msg->upload('foo');
my $foo = $msg->upload('foo');
my @foo = $msg->upload('foo');
my ($foo, $bar) = $msg->upload(['foo', 'bar']);
Access C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects.
Note that this method caches all data, so it should not be called before the
Expand Down
25 changes: 17 additions & 8 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -42,6 +42,9 @@ sub continue { $_[0]->app->routes->continue($_[0]) }
sub cookie {
my ($self, $name) = (shift, shift);

# Multiple names
return map { scalar $self->cookie($_) } @$name if ref $name eq 'ARRAY';

# Response cookie
if (@_) {

Expand Down Expand Up @@ -271,6 +274,10 @@ sub session {
sub signed_cookie {
my ($self, $name, $value, $options) = @_;

# Multiple names
return map { scalar $self->signed_cookie($_) } @$name
if ref $name eq 'ARRAY';

# Response cookie
my $secrets = $self->stash->{'mojo.secrets'};
return $self->cookie($name,
Expand Down Expand Up @@ -513,10 +520,11 @@ Continue dispatch chain with L<Mojolicious::Routes/"continue">.
=head2 cookie
my $value = $c->cookie('foo');
my @values = $c->cookie('foo');
$c = $c->cookie(foo => 'bar');
$c = $c->cookie(foo => 'bar', {path => '/'});
my $foo = $c->cookie('foo');
my @foo = $c->cookie('foo');
my ($foo, $bar) = $c->cookie(['foo', 'bar']);
$c = $c->cookie(foo => 'bar');
$c = $c->cookie(foo => 'bar', {path => '/'});
Access request cookie values and create new response cookies.
Expand Down Expand Up @@ -873,10 +881,11 @@ browser.
=head2 signed_cookie
my $value = $c->signed_cookie('foo');
my @values = $c->signed_cookie('foo');
$c = $c->signed_cookie(foo => 'bar');
$c = $c->signed_cookie(foo => 'bar', {path => '/'});
my $foo = $c->signed_cookie('foo');
my @foo = $c->signed_cookie('foo');
my ($foo, $bar) = $c->signed_cookie(['foo', 'bar']);
$c = $c->signed_cookie(foo => 'bar');
$c = $c->signed_cookie(foo => 'bar', {path => '/'});
Access signed request cookie values and create new signed response cookies.
Cookies failing HMAC-SHA1 signature verification will be automatically
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/request.t
Expand Up @@ -1590,6 +1590,8 @@ is $req2->url->to_abs, 'http://127.0.0.1/foo/bar', 'right absolute URL';
is_deeply [map { $_->value } $req2->cookie('foo')], [qw(bar baz yada)],
'right values';
is_deeply [map { $_->value } $req2->cookie('bar')], ['foo'], 'right values';
is_deeply [map { $_->value } $req2->cookie([qw(foo bar)])], [qw(bar foo)],
'right values';

# Parse full HTTP 1.0 request with cookies and progress callback
$req = Mojo::Message::Request->new;
Expand Down
33 changes: 33 additions & 0 deletions t/mojolicious/group_lite_app.t
Expand Up @@ -13,6 +13,16 @@ use Test::Mojo;

app->secrets(['test1']);

get '/multi' => sub {
my $self = shift;
$self->cookie(unsigned1 => 'one');
$self->cookie(unsigned1 => 'two', {path => '/multi'});
$self->cookie(unsigned2 => 'three');
$self->signed_cookie(signed1 => 'four');
$self->signed_cookie(signed1 => 'five', {path => '/multi'});
$self->signed_cookie(signed2 => 'six');
};

get '/expiration' => sub {
my $self = shift;
if ($self->param('redirect')) {
Expand Down Expand Up @@ -189,6 +199,15 @@ $t->get_ok('/expiration?redirect=1')->status_is(200)
ok !$t->tx->res->cookie('mojolicious')->expires, 'no expiration';
$t->reset_session;

# Multiple cookies with same name
$t->get_ok('/multi')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')->content_is("\n\n\n\n\n\n\n\n");

# Multiple cookies with same name (again)
$t->get_ok('/multi')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("one\nthree\none\ntwo\nfour\nsix\nfour\nfive\n");

# Missing action behind bridge
$t->get_ok('/missing')->status_is(404)->content_is("Oops!\n");

Expand Down Expand Up @@ -451,6 +470,20 @@ __DATA__
@@ not_found.html.epl
Oops!
@@ multi.html.ep
% my ($one, $three) = $self->cookie([qw(unsigned1 unsigned2)]);
%= $one // ''
%= $three // '';
% my @unsigned1 = $self->cookie('unsigned1');
%= $unsigned1[0] // ''
%= $unsigned1[1] // ''
% my ($four, $six) = $self->signed_cookie([qw(signed1 signed2)]);
%= $four // ''
%= $six // '';
% my @signed1 = $self->signed_cookie('signed1');
%= $signed1[0] // ''
%= $signed1[1] // ''
@@ param_auth.html.epl
Bender!
Expand Down

0 comments on commit f2b6ef9

Please sign in to comment.