From: Yuval Kogman Date: Sun, 28 May 2006 23:58:54 +0000 (+0000) Subject: More comprehensive cookie tests + the ability to specify an object instead of a hash X-Git-Tag: 5.7099_04~561 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=2832cb5d6e0c019910584d076ee6633afaeecfe1 More comprehensive cookie tests + the ability to specify an object instead of a hash --- diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 7f7a661..ce90738 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -8,6 +8,7 @@ use HTML::Entities; use HTTP::Body; use HTTP::Headers; use URI::QueryParam; +use Scalar::Util (); # input position and length __PACKAGE__->mk_accessors(qw/read_position read_length/); @@ -68,13 +69,17 @@ sub finalize_cookies { my $val = $c->response->cookies->{$name}; - my $cookie = CGI::Simple::Cookie->new( - -name => $name, - -value => $val->{value}, - -expires => $val->{expires}, - -domain => $val->{domain}, - -path => $val->{path}, - -secure => $val->{secure} || 0 + my $cookie = ( + Scalar::Util::blessed($val) + ? $val + : CGI::Simple::Cookie->new( + -name => $name, + -value => $val->{value}, + -expires => $val->{expires}, + -domain => $val->{domain}, + -path => $val->{path}, + -secure => $val->{secure} || 0 + ) ); push @cookies, $cookie->as_string; diff --git a/t/lib/TestApp/Controller/Engine/Response/Cookies.pm b/t/lib/TestApp/Controller/Engine/Response/Cookies.pm index a66fdca..4a9b2b8 100644 --- a/t/lib/TestApp/Controller/Engine/Response/Cookies.pm +++ b/t/lib/TestApp/Controller/Engine/Response/Cookies.pm @@ -3,18 +3,33 @@ package TestApp::Controller::Engine::Response::Cookies; use strict; use base 'Catalyst::Base'; -sub one : Relative { +sub one : Local { my ( $self, $c ) = @_; - $c->res->cookies->{Catalyst} = { value => 'Cool', path => '/' }; + $c->res->cookies->{Catalyst} = { value => 'Cool', path => '/bah' }; $c->res->cookies->{Cool} = { value => 'Catalyst', path => '/' }; $c->forward('TestApp::View::Dump::Request'); } -sub two : Relative { +sub two : Local { my ( $self, $c ) = @_; - $c->res->cookies->{Catalyst} = { value => 'Cool', path => '/' }; + $c->res->cookies->{Catalyst} = { value => 'Cool', path => '/bah' }; $c->res->cookies->{Cool} = { value => 'Catalyst', path => '/' }; $c->res->redirect('http://www.google.com/'); } +sub three : Local { + my ( $self, $c ) = @_; + + $c->res->cookies->{object} = CGI::Simple::Cookie->new( + -name => "this_is_the_real_name", + -value => [qw/foo bar/], + ); + + $c->res->cookies->{hash} = { + value => [qw/a b c/], + }; + + $c->forward('TestApp::View::Dump::Request'); +} + 1; diff --git a/t/live_engine_response_cookies.t b/t/live_engine_response_cookies.t index e87bb1d..abd0476 100644 --- a/t/live_engine_response_cookies.t +++ b/t/live_engine_response_cookies.t @@ -6,12 +6,12 @@ use warnings; use FindBin; use lib "$FindBin::Bin/lib"; -use Test::More tests => 10; +use Test::More tests => 15; use Catalyst::Test 'TestApp'; use HTTP::Headers::Util 'split_header_words'; my $expected = { - Catalyst => [qw|Catalyst Cool path /|], + Catalyst => [qw|Catalyst Cool path /bah|], Cool => [qw|Cool Catalyst path /|] }; @@ -50,3 +50,24 @@ my $expected = { is_deeply( $cookies, $expected, 'Response Cookies' ); } + +{ + ok( my $response = request('http://localhost/engine/response/cookies/three'), + 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->content_type, 'text/plain', 'Response Content-Type' ); + is( $response->header('X-Catalyst-Action'), + 'engine/response/cookies/three', 'Test Action' ); + + my $cookies = {}; + + for my $string ( $response->header('Set-Cookie') ) { + my $cookie = [ split_header_words $string]; + $cookies->{ $cookie->[0]->[0] } = $cookie->[0]; + } + + is_deeply( $cookies, { + hash => [ qw(hash a&b&c path /) ], + this_is_the_real_name => [ qw(this_is_the_real_name foo&bar path /) ], # not "object" + }, 'Response Cookies' ); +}