Docs and a little more fixing for $c->session_expires
Yuval Kogman [Thu, 29 Dec 2005 08:30:25 +0000 (08:30 +0000)]
lib/Catalyst/Plugin/Session.pm
t/03_flash.t

index 625a356..f306997 100644 (file)
@@ -14,14 +14,15 @@ use Object::Signature   ();
 
 our $VERSION = "0.04";
 
+my @session_data_accessors; # used in delete_session
 BEGIN {
     __PACKAGE__->mk_accessors(
-        qw/
+        "_session_delete_reason",
+        @session_data_accessors = qw/
           _sessionid
           _session
           _session_expires
           _session_data_sig
-          _session_delete_reason
           _flash
           _flash_stale_keys
           /
@@ -201,12 +202,8 @@ sub delete_session {
     $c->delete_session_data("${_}:${sid}") for qw/session expires flash/;
 
     # reset the values in the context object
-    $c->$_(undef) for qw/
-      _sessionid
-      _session
-      _session_expires
-      _session_data_sig
-      /;
+    # see the BEGIN block
+    $c->$_(undef) for @session_data_accessors;
 
     $c->_session_delete_reason($msg);
 }
@@ -481,6 +478,18 @@ requests.
 This method will automatically create a new session and session ID if none
 exists.
 
+=item session_expires
+
+=item session_expires $reset
+
+This method returns the time when the current session will expire, or 0 if
+there is no current session. If there is a session and it already expired, it
+will delete the session and return 0 as well.
+
+If the C<$reset> parameter is true, and there is a session ID the expiry time
+will be reset to the current time plus the time to live (see
+L</CONFIGURATION>). This is used when creating a new session.
+
 =item flash
 
 This is like Ruby on Rails' flash data structure. Think of it as a stash that
@@ -595,6 +604,13 @@ called by the C<session> method if appropriate.
 Creates a new session id using C<generate_session_id> if there is no session ID
 yet.
 
+=item validate_session_id SID
+
+Make sure a session ID is of the right format.
+
+This currently ensures that the session ID string is any amount of case
+insensitive hexadecimal characters.
+
 =item generate_session_id
 
 This method will return a string that can be used as a session ID. It is
@@ -609,13 +625,6 @@ overridable in case you want to provide more random data.
 
 Currently it returns a concatenated string which contains:
 
-=item validate_session_id SID
-
-Make sure a session ID is of the right format.
-
-This currently ensures that the session ID string is any amount of case
-insensitive hexadecimal characters.
-
 =over 4
 
 =item *
@@ -727,7 +736,7 @@ are automatically set:
 
 =item __expires
 
-This key no longer exists. This data is now saved elsewhere.
+This key no longer exists. Use C<session_expires> instead.
 
 =item __updated
 
index a56c43b..5bd3305 100644 (file)
@@ -10,17 +10,23 @@ use Test::Exception;
 my $m;
 BEGIN { use_ok( $m = "Catalyst::Plugin::Session" ) }
 
-my $c = Test::MockObject::Extends->new( $m );
-
-$c->set_always( get_session_data => { } );
-$c->set_true( "store_session_data" );
-$c->set_always( _sessionid => "deadbeef");
-$c->set_always( config => { } );
-$c->set_always( stash => { } );
+my $c = Test::MockObject::Extends->new($m);
+
+my $flash = {};
+$c->mock(
+    get_session_data => sub {
+        my ( $c, $key ) = @_;
+        return $key =~ /expire/ ? time() + 1000 : $flash;
+    }
+);
+$c->set_true("store_session_data");
+$c->set_always( _sessionid => "deadbeef" );
+$c->set_always( config     => { session => { expires => 1000 } } );
+$c->set_always( stash      => {} );
 
 $c->_load_flash;
 
-is_deeply( $c->flash, {}, "nothing in flash");
+is_deeply( $c->flash, {}, "nothing in flash" );
 
 $c->flash->{foo} = "moose";
 
@@ -31,7 +37,7 @@ is_deeply( $c->flash, { foo => "moose" }, "one key in flash" );
 
 $c->flash->{bar} = "gorch";
 
-is_deeply( $c->flash, { foo => "moose", bar => "gorch" }, "two keys in flash");
+is_deeply( $c->flash, { foo => "moose", bar => "gorch" }, "two keys in flash" );
 
 $c->finalize;
 $c->_load_flash;
@@ -41,7 +47,7 @@ is_deeply( $c->flash, { bar => "gorch" }, "one key in flash" );
 $c->finalize;
 $c->_load_flash;
 
-is_deeply( $c->flash, {}, "nothing in flash");
+is_deeply( $c->flash, {}, "nothing in flash" );
 
 $c->flash->{bar} = "gorch";