From: Stevan Little Date: Sun, 13 Dec 2009 01:11:42 +0000 (-0500) Subject: improved Store::File based on stuff in Store::CHI, ... added Store::Null and tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Session.git;a=commitdiff_plain;h=9bb2075077bc58387a991b29c9cae4066384e00d improved Store::File based on stuff in Store::CHI, ... added Store::Null and tests --- diff --git a/lib/Plack/Session/Store/File.pm b/lib/Plack/Session/Store/File.pm index 7741c74..c2926da 100644 --- a/lib/Plack/Session/Store/File.pm +++ b/lib/Plack/Session/Store/File.pm @@ -23,12 +23,14 @@ sub new { $params{'serializer'} ||= sub { Storable::nstore( @_ ) }; $params{'deserializer'} ||= sub { Storable::retrieve( @_ ) }; - $class->SUPER::new( %params ); + bless { %params } => $class; } sub fetch { my ($self, $session_id, $key) = @_; - $self->_deserialize( $session_id )->{ $key }; + my $store = $self->_deserialize( $session_id ); + return unless exists $store->{ $key }; + return $store->{ $key }; } sub store { @@ -41,6 +43,7 @@ sub store { sub delete { my ($self, $session_id, $key) = @_; my $store = $self->_deserialize( $session_id ); + return unless exists $store->{ $key }; delete $store->{ $key }; $self->_serialize( $session_id, $store ); } diff --git a/lib/Plack/Session/Store/Null.pm b/lib/Plack/Session/Store/Null.pm new file mode 100644 index 0000000..3ab6ccb --- /dev/null +++ b/lib/Plack/Session/Store/Null.pm @@ -0,0 +1,48 @@ +package Plack::Session::Store::Null; +use strict; +use warnings; + +sub new { bless {} => shift } +sub fetch {} +sub store {} +sub delete {} +sub cleanup {} +sub persist {} + +1; + +__END__ + +=pod + +=head1 NAME + +Plack::Session::Store::Null - Null store + +=head1 DESCRIPTION + +Sometimes you don't want to store anything in your sessions, but +L requires a C instance, so you can use this +one and all methods will return null. + +=head1 BUGS + +All complex software has bugs lurking in it, and this module is no +exception. If you find a bug please either email me, or add the bug +to cpan-RT. + +=head1 AUTHOR + +Stevan Little Estevan.little@iinteractive.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2009 Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + diff --git a/t/004_basic_file_w_customs.t b/t/004_basic_file_w_customs.t index 0683a79..de79650 100644 --- a/t/004_basic_file_w_customs.t +++ b/t/004_basic_file_w_customs.t @@ -3,14 +3,10 @@ use strict; use warnings; use File::Spec; +use Test::Requires 'YAML'; use Test::More; -BEGIN { - eval "use YAML"; - plan skip_all => "This test requires YAML" if $@; -} - use Plack::Request; use Plack::Session; use Plack::Session::State::Cookie; diff --git a/t/006_basic_w_null_store.t b/t/006_basic_w_null_store.t new file mode 100644 index 0000000..2efba7a --- /dev/null +++ b/t/006_basic_w_null_store.t @@ -0,0 +1,66 @@ +#!/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; + +my $storage = Plack::Session::Store::Null->new; +my $state = Plack::Session::State->new; +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->new( + state => $state, + store => $storage, + request => $r, + ); + + 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'; + + ok(!$s->get('foo'), '... still no value stored in foo for session (null store)'); + + 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( $resp ); + } '... finalized session successfully'; +} + + +done_testing;