Rewrote all the tests and now all tests pass!
Tatsuhiko Miyagawa [Sat, 9 Jan 2010 18:48:21 +0000 (10:48 -0800)]
(Skips the null store tests since it doesn't make much sense now.)

12 files changed:
lib/Plack/Session.pm
lib/Plack/Session/Store/Cache.pm
lib/Plack/Session/Store/File.pm
t/001_basic.t
t/002_basic_w_cookie.t [changed mode: 0644->0755]
t/003_basic_w_file_store.t [changed mode: 0644->0755]
t/004_basic_file_w_customs.t [changed mode: 0644->0755]
t/005_basic_w_cache_store.t [changed mode: 0644->0755]
t/006_basic_w_null_store.t [deleted file]
t/010_middleware.t
t/011_middleware_w_custom_session.t
t/lib/TestSession.pm

index b437e29..b8a4336 100644 (file)
@@ -12,6 +12,11 @@ sub new {
     bless { %params } => $class;
 }
 
+sub id {
+    my $self = shift;
+    $self->options->{id};
+}
+
 ## Data Managment
 
 sub dump {
@@ -45,6 +50,9 @@ sub keys {
 
 sub expire {
     my $self = shift;
+    for my $key ($self->keys) {
+        delete $self->_data->{$key};
+    }
     $self->options->{expire} = 1;
 }
 
index 3a2397e..76f1c36 100644 (file)
@@ -30,7 +30,7 @@ sub fetch {
 
 sub store {
     my ($self, $session_id, $session) = @_;
-    $self->cache->set($session_id => $session->dump);
+    $self->cache->set($session_id => $session);
 }
 
 sub cleanup {
index 0a27952..65ce648 100644 (file)
@@ -41,7 +41,7 @@ sub fetch {
 sub store {
     my ($self, $session_id, $session) = @_;
     my $file_path = $self->_get_session_file_path( $session_id );
-    $self->serializer->( $session->dump, $file_path );
+    $self->serializer->( $session, $file_path );
 }
 
 sub cleanup {
index 257946f..1be9517 100755 (executable)
@@ -13,9 +13,9 @@ use Plack::Session::Store;
 use t::lib::TestSession;
 
 t::lib::TestSession::run_all_tests(
-    store           => Plack::Session::Store->new,
-    state           => Plack::Session::State->new,
-    request_creator => sub {
+    store  => Plack::Session::Store->new,
+    state  => Plack::Session::State->new,
+    env_cb => sub {
         open my $in, '<', \do { my $d };
         my $env = {
             'psgi.version'    => [ 1, 0 ],
@@ -24,10 +24,8 @@ t::lib::TestSession::run_all_tests(
             'psgi.url_scheme' => 'http',
             SERVER_PORT       => 80,
             REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
         };
-        my $r = Plack::Request->new( $env );
-        $r->parameters( @_ );
-        $r;
     },
 );
 
old mode 100644 (file)
new mode 100755 (executable)
index 9ba9191..cfab243
@@ -9,13 +9,14 @@ use Plack::Request;
 use Plack::Session;
 use Plack::Session::State::Cookie;
 use Plack::Session::Store;
+use Plack::Util;
 
 use t::lib::TestSession;
 
 t::lib::TestSession::run_all_tests(
-    store           => Plack::Session::Store->new,
-    state           => Plack::Session::State::Cookie->new,
-    request_creator => sub {
+    store  => Plack::Session::Store->new,
+    state  => Plack::Session::State::Cookie->new,
+    env_cb => sub {
         my $cookies = shift;
         open my $in, '<', \do { my $d };
         my $env = {
@@ -27,23 +28,19 @@ t::lib::TestSession::run_all_tests(
             REQUEST_METHOD    => 'GET',
             HTTP_COOKIE       => join "; " => map { $_ . "=" . $cookies->{ $_ } } keys %$cookies,
         };
-        return Plack::Request->new( $env );
     },
     response_test   => sub {
-        my ($response, $session_id, $check_expired) = @_;
-        is_deeply(
-            $response->cookies,
-            {
-                plack_session => {
-                    value => $session_id,
-                    path  => '/',
-                    ($check_expired
-                        ? ( expires => '0' )
-                        : ())
-                }
-            },
-            '... got the right cookies in the response'
-        );
+        my ($res_cb, $session_id, $check_expired) = @_;
+        my $cookie;
+        $res_cb->(sub {
+            my $res = shift;
+            $cookie = Plack::Util::header_get($res->[1], 'Set-Cookie');
+        });
+
+        like $cookie, qr/plack_session=$session_id/;
+        if ($check_expired) {
+            like $cookie, qr/expires=/;
+        }
     }
 );
 
old mode 100644 (file)
new mode 100755 (executable)
index e5f6817..9963f0a
@@ -19,9 +19,9 @@ if ( !-d $TMP ) {
 }
 
 t::lib::TestSession::run_all_tests(
-    store           => Plack::Session::Store::File->new( dir => $TMP ),
-    state           => Plack::Session::State->new,
-    request_creator => sub {
+    store  => Plack::Session::Store::File->new( dir => $TMP ),
+    state  => Plack::Session::State->new,
+    env_cb => sub {
         open my $in, '<', \do { my $d };
         my $env = {
             'psgi.version'    => [ 1, 0 ],
@@ -30,10 +30,8 @@ t::lib::TestSession::run_all_tests(
             'psgi.url_scheme' => 'http',
             SERVER_PORT       => 80,
             REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
         };
-        my $r = Plack::Request->new( $env );
-        $r->parameters( @_ );
-        $r;
     },
 );
 
old mode 100644 (file)
new mode 100755 (executable)
index de79650..0c663cd
@@ -20,13 +20,13 @@ if ( !-d $TMP ) {
 }
 
 t::lib::TestSession::run_all_tests(
-    store           => Plack::Session::Store::File->new(
+    store  => Plack::Session::Store::File->new(
         dir          => $TMP,
         serializer   => sub { YAML::DumpFile( reverse @_ ) }, # YAML takes it's args the opposite of Storable
         deserializer => sub { YAML::LoadFile( @_ ) },
     ),
-    state           => Plack::Session::State->new,
-    request_creator => sub {
+    state  => Plack::Session::State->new,
+    env_cb => sub {
         open my $in, '<', \do { my $d };
         my $env = {
             'psgi.version'    => [ 1, 0 ],
@@ -35,10 +35,8 @@ t::lib::TestSession::run_all_tests(
             'psgi.url_scheme' => 'http',
             SERVER_PORT       => 80,
             REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
         };
-        my $r = Plack::Request->new( $env );
-        $r->parameters( @_ );
-        $r;
     },
 );
 
old mode 100644 (file)
new mode 100755 (executable)
index ebd1f80..3cb9b24
@@ -39,9 +39,9 @@ use t::lib::TestSession;
 }
 
 t::lib::TestSession::run_all_tests(
-    store           => Plack::Session::Store::Cache->new( cache => TestCache->new ),
-    state           => Plack::Session::State->new,
-    request_creator => sub {
+    store  => Plack::Session::Store::Cache->new( cache => TestCache->new ),
+    state  => Plack::Session::State->new,
+    env_cb => sub {
         open my $in, '<', \do { my $d };
         my $env = {
             'psgi.version'    => [ 1, 0 ],
@@ -50,10 +50,8 @@ t::lib::TestSession::run_all_tests(
             'psgi.url_scheme' => 'http',
             SERVER_PORT       => 80,
             REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
         };
-        my $r = Plack::Request->new( $env );
-        $r->parameters( @_ );
-        $r;
     },
 );
 
diff --git a/t/006_basic_w_null_store.t b/t/006_basic_w_null_store.t
deleted file mode 100755 (executable)
index 794bfc5..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-
-use Test::More;
-use Test::Exception;
-
-use Plack::Request;
-use Plack::Session;
-use Plack::Session::State;
-use Plack::Session::Store::Null;
-use Plack::Middleware::Session;
-
-my $storage         = Plack::Session::Store::Null->new;
-my $state           = Plack::Session::State->new;
-my $m               = Plack::Middleware::Session->new(store => $storage, state => $state);
-my $request_creator = sub {
-    open my $in, '<', \do { my $d };
-    my $env = {
-        'psgi.version'    => [ 1, 0 ],
-        'psgi.input'      => $in,
-        'psgi.errors'     => *STDERR,
-        'psgi.url_scheme' => 'http',
-        SERVER_PORT       => 80,
-        REQUEST_METHOD    => 'GET',
-    };
-    my $r = Plack::Request->new( $env );
-    $r->parameters( @_ );
-    $r;
-};
-
-{
-    my $r = $request_creator->();
-
-    my $s = Plack::Session->fetch_or_create($r, $m);
-
-    ok($s->id, '... got a session id');
-
-    ok(!$s->get('foo'), '... no value stored in foo for session');
-
-    lives_ok {
-        $s->set( foo => 'bar' );
-    } '... set the value successfully in session';
-
-    is($s->get('foo'), 'bar', 'No store, but session works in the session lifetime');
-
-    lives_ok {
-        $s->remove('foo');
-    } '... removed the value successfully in session';
-
-    lives_ok {
-        $s->expire;
-    } '... expire session successfully';
-
-    my $resp = $r->new_response;
-
-    lives_ok {
-        $s->finalize( $m, $resp );
-    } '... finalized session successfully';
-}
-
-
-done_testing;
index 4a71f8f..8d2b517 100644 (file)
@@ -6,16 +6,16 @@ use HTTP::Cookies;
 
 my $app = sub {
     my $env = shift;
-    my $counter = $env->{'psgix.session'}->get('counter') || 0;
+    my $counter = $env->{'plack.session'}->get('counter') || 0;
 
     my $body = "Counter=$counter";
     $counter++;
-    $env->{'psgix.session'}->set(counter => $counter);
+    $env->{'plack.session'}->set(counter => $counter);
 
     return [ 200, [], [ $body ] ];
 };
 
-$app = Plack::Middleware::Session->wrap($app);
+$app = Plack::Middleware::Session->wrap($app, session_class => "Plack::Session");
 
 test_psgi $app, sub {
     my $cb = shift;
index e23c553..0b266e5 100644 (file)
@@ -4,30 +4,23 @@ use Test::More;
 use HTTP::Request::Common;
 use HTTP::Cookies;
 
-{
-    package My::Custom::Session;
-    use strict;
-    use warnings;
-    use parent 'Plack::Session';
-}
-
 my $app = sub {
     my $env = shift;
 
-    isa_ok($env->{'psgix.session'}, 'My::Custom::Session');
+    isa_ok($env->{'plack.session'}, 't::MyCustomSession');
 
-    my $counter = $env->{'psgix.session'}->get('counter') || 0;
+    my $counter = $env->{'plack.session'}->get('counter') || 0;
 
     my $body = "Counter=$counter";
     $counter++;
-    $env->{'psgix.session'}->set(counter => $counter);
+    $env->{'plack.session'}->set(counter => $counter);
 
     return [ 200, [], [ $body ] ];
 };
 
 $app = Plack::Middleware::Session->wrap(
     $app,
-    session_class => 'My::Custom::Session'
+    session_class => 't::MyCustomSession'
 );
 
 test_psgi $app, sub {
index aeb8923..c35f34d 100644 (file)
@@ -6,32 +6,49 @@ use Test::More;
 use Test::Exception;
 use Plack::Middleware::Session;
 
+sub create_session {
+    my($mw, $env) = @_;
+
+    my $session;
+    my $app = sub {
+        my $env = shift;
+        $session = $env->{'plack.session'};
+        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
     ]};
 
-    my $m = Plack::Middleware::Session->new(state => $state, store => $storage);
+    my $m = sub { Plack::Middleware::Session->wrap($_[0], session_class => "Plack::Session", state => $state, store => $storage) };
 
-    $response_test = sub {
-        my ($response, $session_id, $check_expired) = @_;
+    $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->fetch_or_create($r, $m);
+        my($s, $res) = create_session($m, $env_cb->());
 
         push @sids, $s->id;
 
@@ -51,21 +68,13 @@ sub run_all_tests {
 
         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] );
+        $response_test->($res, $sids[0]);
     }
 
     {
-        my $r = $request_creator->();
-
-        my $s = Plack::Session->fetch_or_create($r, $m);
+        my($s, $res) = create_session($m, $env_cb->());
 
         push @sids, $s->id;
 
@@ -78,22 +87,13 @@ sub run_all_tests {
 
         is($s->get('foo'), '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 => 'baz' }, '... got the session dump we expected');
 
-        $response_test->( $resp, $sids[1] );
+        $response_test->($res, $sids[1]);
     }
 
     {
-        my $r = $request_creator->({ plack_session => $sids[0] });
-
-        my $s = Plack::Session->fetch_or_create($r, $m);
-
+        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');
@@ -105,42 +105,26 @@ sub run_all_tests {
 
         ok(!$s->get('foo'), '... no value stored for foo in session');
 
-        my $resp = $r->new_response;
-
-        lives_ok {
-            $s->finalize( $resp );
-        } '... finalized session successfully';
-
         is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected');
 
-        $response_test->( $resp, $sids[0] );
+        $response_test->( $res, $sids[0] );
     }
 
 
     {
-        my $r = $request_creator->({ plack_session => $sids[1] });
-
-        my $s = Plack::Session->fetch_or_create($r, $m);
+        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;
-
-        lives_ok {
-            $s->finalize( $resp );
-        } '... finalized session successfully';
-
         is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
 
-        $response_test->( $resp, $sids[1] );
+        $response_test->( $res, $sids[1] );
     }
 
     {
-        my $r = $request_creator->({ plack_session => $sids[0] });
-
-        my $s = Plack::Session->fetch_or_create($r, $m);
+        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
 
         is($s->id, $sids[0], '... got a basic session id');
 
@@ -150,21 +134,13 @@ sub run_all_tests {
             $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->fetch_or_create($r, $m);
+        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');
 
@@ -172,63 +148,39 @@ 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 );
 
         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->fetch_or_create($r, $m);
+        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->fetch_or_create($r, $m);
+        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;
-
-        lives_ok {
-            $s->finalize( $resp );
-        } '... finalized session successfully';
-
         is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
 
-        $response_test->( $resp, $sids[1] );
+        $response_test->( $res, $sids[1] );
     }
 
     {
         # wrong format session_id
-        my $r = $request_creator->({ plack_session => '../wrong' });
-
-        my $s = Plack::Session->fetch_or_create($r, $m);
+        my($s, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));
 
         isnt('../wrong' => $s->id, '... regenerate session id');
 
@@ -240,15 +192,9 @@ sub run_all_tests {
 
         is($s->get('foo'), '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 => 'baz' }, '... got the session dump we expected');
 
-        $response_test->( $resp, $s );
+        $response_test->( $res, $s->id );
     }
 }