$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 {
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 );
}
--- /dev/null
+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<Plack::Session> requires a C<store> 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 E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
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;
--- /dev/null
+#!/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;