Huge rewrite of the code: $session->get, ->set etc. do not read from
[catagits/Web-Session.git] / t / lib / TestSession.pm
index 3f6371b..aeb8923 100644 (file)
@@ -4,6 +4,7 @@ use warnings;
 
 use Test::More;
 use Test::Exception;
+use Plack::Middleware::Session;
 
 sub run_all_tests {
     my %params = @_;
@@ -20,6 +21,8 @@ sub run_all_tests {
         response_test
     ]};
 
+    my $m = Plack::Middleware::Session->new(state => $state, store => $storage);
+
     $response_test = sub {
         my ($response, $session_id, $check_expired) = @_;
     };
@@ -28,11 +31,7 @@ sub run_all_tests {
     {
         my $r = $request_creator->();
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         push @sids, $s->id;
 
@@ -44,23 +43,29 @@ sub run_all_tests {
 
         is($s->get('foo'), 'bar', '... got the foo value back successfully from session');
 
+        ok(!$s->get('bar'), '... no value stored in foo for session');
+
+        lives_ok {
+            $s->set( bar => 'baz' );
+        } '... set the value successfully in session';
+
+        is($s->get('bar'), 'baz', '... got the foo value back successfully from session');
+
         my $resp = $r->new_response;
 
         lives_ok {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[0] );
     }
 
     {
         my $r = $request_creator->();
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         push @sids, $s->id;
 
@@ -79,22 +84,21 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[1] );
     }
 
     {
         my $r = $request_creator->({ plack_session => $sids[0] });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         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';
@@ -107,6 +111,8 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[0] );
     }
 
@@ -114,11 +120,7 @@ sub run_all_tests {
     {
         my $r = $request_creator->({ plack_session => $sids[1] });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         is($s->id, $sids[1], '... got a basic session id');
 
@@ -130,24 +132,22 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[1] );
     }
 
     {
         my $r = $request_creator->({ plack_session => $sids[0] });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         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;
@@ -156,17 +156,15 @@ sub run_all_tests {
             $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] );
     }
 
     {
         my $r = $request_creator->({ plack_session => $sids[0] });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         is($s->get('bar'), 'baz', '... got the bar value back successfully from session');
 
@@ -180,17 +178,15 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, {}, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[0], 1 );
     }
 
     {
         my $r = $request_creator->({ plack_session => $sids[0] });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         push @sids, $s->id;
         isnt($s->id, $sids[0], 'expired ... got a new session id');
@@ -203,17 +199,15 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, {}, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[2] );
     }
 
     {
         my $r = $request_creator->({ plack_session => $sids[1] });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         is($s->id, $sids[1], '... got a basic session id');
 
@@ -225,6 +219,8 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
+
         $response_test->( $resp, $sids[1] );
     }
 
@@ -232,12 +228,7 @@ sub run_all_tests {
         # wrong format session_id
         my $r = $request_creator->({ plack_session => '../wrong' });
 
-        my $s = Plack::Session->new(
-            state   => $state,
-            store   => $storage,
-            request => $r,
-        );
-
+        my $s = Plack::Session->fetch_or_create($r, $m);
 
         isnt('../wrong' => $s->id, '... regenerate session id');
 
@@ -255,6 +246,8 @@ sub run_all_tests {
             $s->finalize( $resp );
         } '... finalized session successfully';
 
+        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
+
         $response_test->( $resp, $s );
     }
 }