X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flib%2FTestSession.pm;h=ab234aa61166cd81393e1243f32ca2f49a1d58bf;hb=HEAD;hp=d642d9e9ce7dce43d8076ee55a8f2d9cb80f39d4;hpb=f331b3e0957efa44b2e86ad76e7c5a1f6914b247;p=catagits%2FWeb-Session.git diff --git a/t/lib/TestSession.pm b/t/lib/TestSession.pm index d642d9e..ab234aa 100644 --- a/t/lib/TestSession.pm +++ b/t/lib/TestSession.pm @@ -3,36 +3,53 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal qw(lives_ok); +use Plack::Middleware::Session; +use Plack::Session; + +sub create_session { + my($mw, $env) = @_; + + my $session; + my $app = sub { + my $env = shift; + $session = Plack::Session->new($env); + return sub { + my $responder = shift; + $responder->([ 200, [], [] ]); + }; + }; + + my $res = $mw->($app)->($env); + + return ($session, $res); +} sub run_all_tests { my %params = @_; my ( - $request_creator, + $env_cb, $state, $storage, $response_test ) = @params{qw[ - request_creator + env_cb state store response_test ]}; - $response_test = sub { - my ($response, $session_id, $check_expired) = @_; + my $m = sub { Plack::Middleware::Session->wrap($_[0], state => $state, store => $storage) }; + + $response_test ||= sub { + my($res_cb, $session_id, $check_expired) = @_; + $res_cb->(sub { my $res = shift }); }; my @sids; { - my $r = $request_creator->(); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->()); push @sids, $s->id; @@ -44,23 +61,21 @@ sub run_all_tests { is($s->get('foo'), 'bar', '... got the foo value back successfully from session'); - my $resp = $r->new_response; + ok(!$s->get('bar'), '... no value stored in foo for session'); lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; + $s->set( bar => 'baz' ); + } '... set the value successfully in session'; + + is($s->get('bar'), 'baz', '... got the foo value back successfully from session'); - $response_test->( $resp, $sids[0] ); + is_deeply( $s->dump, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected'); + + $response_test->($res, $sids[0]); } { - my $r = $request_creator->(); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->()); push @sids, $s->id; @@ -73,100 +88,60 @@ sub run_all_tests { is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); - my $resp = $r->new_response; + is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected'); - lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; - - $response_test->( $resp, $sids[1] ); + $response_test->($res, $sids[1]); } { - my $r = $request_creator->({ plack_session => $sids[0] }); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); - + my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] })); is($s->id, $sids[0], '... got a basic session id'); is($s->get('foo'), 'bar', '... got the value for foo back successfully from session'); + lives_ok { $s->remove( 'foo' ); } '... removed the foo value successfully from session'; ok(!$s->get('foo'), '... no value stored for foo in session'); - my $resp = $r->new_response; + is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected'); - lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; - - $response_test->( $resp, $sids[0] ); + $response_test->( $res, $sids[0] ); } { - my $r = $request_creator->({ plack_session => $sids[1] }); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] })); is($s->id, $sids[1], '... got a basic session id'); is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); - my $resp = $r->new_response; + is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected'); - lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; - - $response_test->( $resp, $sids[1] ); + $response_test->( $res, $sids[1] ); } { - my $r = $request_creator->({ plack_session => $sids[0] }); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] })); is($s->id, $sids[0], '... got a basic session id'); ok(!$s->get('foo'), '... no value stored for foo in session'); lives_ok { - $s->set( bar => 'baz' ); + $s->set( baz => 'gorch' ); } '... set the bar value successfully in session'; - my $resp = $r->new_response; - - lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; + is_deeply( $s->dump, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected'); - $response_test->( $resp, $sids[0] ); + $response_test->( $res, $sids[0] ); } { - my $r = $request_creator->({ plack_session => $sids[0] }); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] })); is($s->get('bar'), 'baz', '... got the bar value back successfully from session'); @@ -174,59 +149,54 @@ sub run_all_tests { $s->expire; } '... expired session successfully'; - my $resp = $r->new_response; - - lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; + $response_test->( $res, $sids[0], 1 ); - $response_test->( $resp, $sids[0], 1 ); + is_deeply( $s->dump, {}, '... got the session dump we expected'); } { - my $r = $request_creator->({ plack_session => $sids[0] }); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] })); push @sids, $s->id; isnt($s->id, $sids[0], 'expired ... got a new session id'); ok(!$s->get('bar'), '... no bar value stored'); - my $resp = $r->new_response; - - lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; + is_deeply( $s->dump, {}, '... got the session dump we expected'); - $response_test->( $resp, $sids[2] ); + $response_test->( $res, $sids[2] ); } { - my $r = $request_creator->({ plack_session => $sids[1] }); - - my $s = Plack::Session->new( - state => $state, - store => $storage, - request => $r, - ); + my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] })); is($s->id, $sids[1], '... got a basic session id'); is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); - my $resp = $r->new_response; + is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected'); + + $response_test->( $res, $sids[1] ); + } + + { + # wrong format session_id + my($s, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" })); + + isnt('../wrong' => $s->id, '... regenerate session id'); + + ok(!$s->get('foo'), '... no value stored for foo in session'); lives_ok { - $s->finalize( $resp ); - } '... finalized session successfully'; + $s->set( foo => 'baz' ); + } '... set the value successfully'; + + is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); + + is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected'); - $response_test->( $resp, $sids[1] ); + $response_test->( $res, $s->id ); } } -1; \ No newline at end of file +1;