adding in tests for the raw HASH session
Stevan Little [Sun, 10 Jan 2010 03:29:48 +0000 (22:29 -0500)]
t/001a_basic.t [new file with mode: 0644]
t/002_basic_w_cookie.t
t/002a_basic_w_cookie.t [new file with mode: 0644]
t/003a_basic_w_file_store.t [new file with mode: 0644]
t/004a_basic_file_w_customs.t [new file with mode: 0644]
t/005a_basic_w_cache_store.t [new file with mode: 0644]
t/010a_middleware.t [moved from t/011_middleware_w_custom_session.t with 68% similarity]
t/014_cookiestore_again.t [new file with mode: 0644]
t/lib/TestSessionHash.pm [new file with mode: 0644]

diff --git a/t/001a_basic.t b/t/001a_basic.t
new file mode 100644 (file)
index 0000000..fb0109d
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use Plack::Request;
+use Plack::Session::State;
+use Plack::Session::Store;
+
+use t::lib::TestSessionHash;
+
+t::lib::TestSessionHash::run_all_tests(
+    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 ],
+            'psgi.input'      => $in,
+            'psgi.errors'     => *STDERR,
+            'psgi.url_scheme' => 'http',
+            SERVER_PORT       => 80,
+            REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
+        };
+    },
+);
+
+done_testing;
index cfab243..b7f29e2 100755 (executable)
@@ -37,9 +37,9 @@ t::lib::TestSession::run_all_tests(
             $cookie = Plack::Util::header_get($res->[1], 'Set-Cookie');
         });
 
-        like $cookie, qr/plack_session=$session_id/;
+        like($cookie, qr/plack_session=$session_id/, '... cookie value is as suspected');
         if ($check_expired) {
-            like $cookie, qr/expires=/;
+            like($cookie, qr/expires=/, '... cookie is expriring as suspected');
         }
     }
 );
diff --git a/t/002a_basic_w_cookie.t b/t/002a_basic_w_cookie.t
new file mode 100644 (file)
index 0000000..a16016e
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use Plack::Request;
+use Plack::Session::State::Cookie;
+use Plack::Session::Store;
+use Plack::Util;
+
+use t::lib::TestSessionHash;
+
+t::lib::TestSessionHash::run_all_tests(
+    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 = {
+            'psgi.version'    => [ 1, 0 ],
+            'psgi.input'      => $in,
+            'psgi.errors'     => *STDERR,
+            'psgi.url_scheme' => 'http',
+            SERVER_PORT       => 80,
+            REQUEST_METHOD    => 'GET',
+            HTTP_COOKIE       => join "; " => map { $_ . "=" . $cookies->{ $_ } } keys %$cookies,
+        };
+    },
+    response_test   => sub {
+        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/, '... cookie value is as suspected');
+        if ($check_expired) {
+            like($cookie, qr/expires=/, '... cookie is expriring as suspected');
+        }
+    }
+);
+
+done_testing;
diff --git a/t/003a_basic_w_file_store.t b/t/003a_basic_w_file_store.t
new file mode 100644 (file)
index 0000000..f58c29b
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use File::Spec;
+
+use Test::More;
+
+use Plack::Request;
+use Plack::Session::State::Cookie;
+use Plack::Session::Store::File;
+
+use t::lib::TestSessionHash;
+
+my $TMP = File::Spec->catdir('t', 'tmp');
+if ( !-d $TMP ) {
+    mkdir $TMP;
+}
+
+t::lib::TestSessionHash::run_all_tests(
+    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 ],
+            'psgi.input'      => $in,
+            'psgi.errors'     => *STDERR,
+            'psgi.url_scheme' => 'http',
+            SERVER_PORT       => 80,
+            REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
+        };
+    },
+);
+
+unlink $_ foreach glob( File::Spec->catdir($TMP, '*') );
+
+done_testing;
diff --git a/t/004a_basic_file_w_customs.t b/t/004a_basic_file_w_customs.t
new file mode 100644 (file)
index 0000000..a56649e
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use File::Spec;
+use Test::Requires 'YAML';
+
+use Test::More;
+
+use Plack::Request;
+use Plack::Session::State::Cookie;
+use Plack::Session::Store::File;
+
+use t::lib::TestSessionHash;
+
+my $TMP = File::Spec->catdir('t', 'tmp');
+if ( !-d $TMP ) {
+    mkdir $TMP;
+}
+
+t::lib::TestSessionHash::run_all_tests(
+    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,
+    env_cb => 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',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
+        };
+    },
+);
+
+unlink $_ foreach glob( File::Spec->catdir($TMP, '*') );
+
+done_testing;
diff --git a/t/005a_basic_w_cache_store.t b/t/005a_basic_w_cache_store.t
new file mode 100644 (file)
index 0000000..04b9f7c
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use Plack::Request;
+use Plack::Session::State;
+use Plack::Session::Store::Cache;
+
+use t::lib::TestSessionHash;
+
+{
+    package TestCache;
+
+    sub new {
+        bless {} => shift;
+    }
+
+    sub set {
+        my ($self, $key, $val ) = @_;
+
+        $self->{$key} = $val;
+    }
+
+    sub get {
+        my ($self, $key ) = @_;
+
+        $self->{$key};
+    }
+
+    sub remove {
+        my ($self, $key ) = @_;
+
+        delete $self->{$key};
+    }
+}
+
+t::lib::TestSessionHash::run_all_tests(
+    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 ],
+            'psgi.input'      => $in,
+            'psgi.errors'     => *STDERR,
+            'psgi.url_scheme' => 'http',
+            SERVER_PORT       => 80,
+            REQUEST_METHOD    => 'GET',
+            QUERY_STRING      => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
+        };
+    },
+);
+
+
+done_testing;
similarity index 68%
rename from t/011_middleware_w_custom_session.t
rename to t/010a_middleware.t
index 0b266e5..38f6304 100644 (file)
@@ -6,22 +6,16 @@ use HTTP::Cookies;
 
 my $app = sub {
     my $env = shift;
-
-    isa_ok($env->{'plack.session'}, 't::MyCustomSession');
-
-    my $counter = $env->{'plack.session'}->get('counter') || 0;
+    my $counter = $env->{'psgix.session'}->{'counter'} || 0;
 
     my $body = "Counter=$counter";
     $counter++;
-    $env->{'plack.session'}->set(counter => $counter);
+    $env->{'psgix.session'}->{counter} = $counter;
 
     return [ 200, [], [ $body ] ];
 };
 
-$app = Plack::Middleware::Session->wrap(
-    $app,
-    session_class => 't::MyCustomSession'
-);
+$app = Plack::Middleware::Session->wrap($app);
 
 test_psgi $app, sub {
     my $cb = shift;
diff --git a/t/014_cookiestore_again.t b/t/014_cookiestore_again.t
new file mode 100644 (file)
index 0000000..65d2ac2
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use Plack::Request;
+use Plack::Middleware::Session::Cookie;
+
+use t::lib::TestSessionHash;
+
+t::lib::TestSessionHash::run_all_tests(
+    middleware_create_cb => sub {
+        Plack::Middleware::Session::Cookie->wrap( $_[0], secret => "foobar" );
+    },
+    env_cb => sub {
+        my $cookies = shift;
+        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',
+            HTTP_COOKIE       => join "; " => map { $_ . "=" . $cookies->{ $_ } } keys %$cookies,
+        };
+    },
+    response_test   => sub {
+        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/, '... cookie value is as suspected');
+        if ($check_expired) {
+            like($cookie, qr/expires=/, '... cookie is expriring as suspected');
+        }
+    }
+);
+
+done_testing;
diff --git a/t/lib/TestSessionHash.pm b/t/lib/TestSessionHash.pm
new file mode 100644 (file)
index 0000000..4573134
--- /dev/null
@@ -0,0 +1,210 @@
+package t::lib::TestSessionHash;
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+use Plack::Middleware::Session;
+
+sub create_session {
+    my($mw, $env) = @_;
+
+    my ($session, $session_options);
+    my $app = sub {
+        my $env = shift;
+        $session         = $env->{'psgix.session'};
+        $session_options = $env->{'psgix.session.options'};
+        return sub {
+            my $responder = shift;
+            $responder->([ 200, [], [] ]);
+        };
+    };
+
+    my $res = $mw->($app)->($env);
+
+    return ($session, $session_options, $res);
+}
+
+sub run_all_tests {
+    my %params = @_;
+
+    my (
+        $env_cb,
+        $state,
+        $storage,
+        $response_test,
+        $middleware_create_cb
+    ) = @params{qw[
+        env_cb
+        state
+        store
+        response_test
+        middleware_create_cb
+    ]};
+
+    my $m = $middleware_create_cb
+          || 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($s, $opts, $res) = create_session($m, $env_cb->());
+
+        push @sids, $opts->{id};
+
+        ok(!$s->{'foo'}, '... no value stored in foo for session');
+
+        lives_ok {
+            $s->{foo} = 'bar';
+        } '... set the value successfully in session';
+
+        is($s->{'foo'}, 'bar', '... got the foo value back successfully from session');
+
+        ok(!$s->{'bar'}, '... no value stored in foo for session');
+
+        lives_ok {
+            $s->{bar} = 'baz';
+        } '... set the value successfully in session';
+
+        is($s->{'bar'}, 'baz', '... got the foo value back successfully from session');
+
+        is_deeply( $s, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
+
+        $response_test->($res, $sids[0]);
+    }
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->());
+
+        push @sids, $opts->{id};
+
+        isnt($sids[0], $sids[1], "no same Session ID");
+        ok(!$s->{'foo'}, '... no value stored for foo in session');
+
+        lives_ok {
+            $s->{foo} = 'baz';
+        } '... set the value successfully';
+
+        is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
+
+        is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
+
+        $response_test->($res, $sids[1]);
+    }
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
+        is($opts->{id}, $sids[0], '... got a basic session id');
+
+        is($s->{'foo'}, 'bar', '... got the value for foo back successfully from session');
+
+
+        lives_ok {
+            delete $s->{'foo'};
+        } '... removed the foo value successfully from session';
+
+        ok(!$s->{'foo'}, '... no value stored for foo in session');
+
+        is_deeply( $s, { bar => 'baz' }, '... got the session dump we expected');
+
+        $response_test->( $res, $sids[0] );
+    }
+
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
+
+        is($opts->{id}, $sids[1], '... got a basic session id');
+
+        is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
+
+        is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
+
+        $response_test->( $res, $sids[1] );
+    }
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
+
+        is($opts->{id}, $sids[0], '... got a basic session id');
+
+        ok(!$s->{'foo'}, '... no value stored for foo in session');
+
+        lives_ok {
+            $s->{baz} = 'gorch';
+        } '... set the bar value successfully in session';
+
+        is_deeply( $s, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
+
+        $response_test->( $res, $sids[0] );
+    }
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
+
+        is($s->{'bar'}, 'baz', '... got the bar value back successfully from session');
+
+        lives_ok {
+            $opts->{expire} = 1;
+        } '... expired session successfully';
+
+        $response_test->( $res, $sids[0], 1 );
+
+        # XXX
+        # this will not pass, because
+        # it is just a hash ref and we are
+        # not clearing it. Should we be?
+        # - SL
+        # is_deeply( $s, {}, '... got the session dump we expected');
+    }
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
+
+        push @sids, $opts->{id};
+        isnt($opts->{id}, $sids[0], 'expired ... got a new session id');
+
+        ok(!$s->{'bar'}, '... no bar value stored');
+
+        is_deeply( $s, {}, '... got the session dump we expected');
+
+        $response_test->( $res, $sids[2] );
+    }
+
+    {
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
+
+        is($opts->{id}, $sids[1], '... got a basic session id');
+
+        is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
+
+        is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
+
+        $response_test->( $res, $sids[1] );
+    }
+
+    {
+        # wrong format session_id
+        my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));
+
+        isnt('../wrong' => $opts->{id}, '... regenerate session id');
+
+        ok(!$s->{'foo'}, '... no value stored for foo in session');
+
+        lives_ok {
+            $s->{foo} = 'baz';
+        } '... set the value successfully';
+
+        is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
+
+        is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
+
+        $response_test->( $res, $opts->{id} );
+    }
+}
+
+1;