releng for C::P::Session 0.012 - minor refactorings for Store::Delegate
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session / Test / Store.pm
CommitLineData
15ee2fd3 1\feff#!/usr/bin/perl
69da5f6e 2
3package Catalyst::Plugin::Session::Test::Store;
4
5use strict;
6use warnings;
7
ec299c02 8use utf8;
9
8f236527 10use Test::More;
69da5f6e 11use File::Temp;
12use File::Spec;
13
14use Catalyst ();
15
16sub import {
45c0711b 17 shift;
18 my %args = @_;
69da5f6e 19
8f236527 20 plan tests => 19 + ($args{extra_tests} || 0);
21
45c0711b 22 my $backend = $args{backend};
23 my $cfg = $args{config};
69da5f6e 24
45c0711b 25 my $p = "Session::Store::$backend";
26 use_ok( my $m = "Catalyst::Plugin::$p" );
69da5f6e 27
45c0711b 28 isa_ok( bless( {}, $m ), "Catalyst::Plugin::Session::Store" );
69da5f6e 29
ec299c02 30 {
31 package Catalyst::Plugin::SessionStateTest;
32 use base qw/Catalyst::Plugin::Session::State/;
33
34 no strict 'refs';
35
36 sub get_session_id {
37 my $c = shift;
38 ${ ref($c) . "::session_id" };
39 }
40
41 sub set_session_id {
42 my ( $c, $sid ) = @_;
43 ${ ref($c) . "::session_id" } = $sid;
44 }
45
46 sub delete_session_id {
47 my $c = shift;
48 undef ${ ref($c) . "::session_id" };
49 }
50 }
69da5f6e 51
45c0711b 52 {
69da5f6e 53
45c0711b 54 package SessionStoreTest;
ec299c02 55 use Catalyst qw/Session SessionStateTest/;
45c0711b 56 push our (@ISA), $m;
69da5f6e 57
ec299c02 58 use strict;
59 use warnings;
69da5f6e 60
45c0711b 61 use Test::More;
69da5f6e 62
45c0711b 63 sub create_session : Global {
64 my ( $self, $c ) = @_;
ec299c02 65 ok( !$c->session_is_valid, "no session id yet" );
66 ok( $c->session, "session created" );
67 ok( $c->session_is_valid, "with a session id" );
69da5f6e 68
45c0711b 69 $c->session->{magic} = "møøse";
70 }
69da5f6e 71
45c0711b 72 sub recover_session : Global {
73 my ( $self, $c ) = @_;
ec299c02 74 ok( $c->session_is_valid, "session id exists" );
75 is( $c->sessionid, our $session_id,
45c0711b 76 "and is the one we saved in the last action" );
77 ok( $c->session, "a session exists" );
78 is( $c->session->{magic},
79 "møøse",
80 "and it contains what we put in on the last attempt" );
81 $c->delete_session("user logout");
45c0711b 82 }
69da5f6e 83
45c0711b 84 sub after_session : Global {
85 my ( $self, $c ) = @_;
ec299c02 86 ok( !$c->session_is_valid, "no session id" );
45c0711b 87 ok( !$c->session->{magic}, "session data not restored" );
88 ok( !$c->session_delete_reason, "no reason for deletion" );
89 }
69da5f6e 90
45c0711b 91 @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
69da5f6e 92
45c0711b 93 __PACKAGE__->setup;
94 }
69da5f6e 95
45c0711b 96 {
69da5f6e 97
45c0711b 98 package SessionStoreTest2;
ec299c02 99 use Catalyst qw/Session SessionStateTest/;
45c0711b 100 push our (@ISA), $m;
69da5f6e 101
45c0711b 102 our $VERSION = "123";
69da5f6e 103
45c0711b 104 use Test::More;
69da5f6e 105
45c0711b 106 sub create_session : Global {
107 my ( $self, $c ) = @_;
69da5f6e 108
45c0711b 109 $c->session->{magic} = "møøse";
45c0711b 110 }
69da5f6e 111
45c0711b 112 sub recover_session : Global {
113 my ( $self, $c ) = @_;
69da5f6e 114
ec299c02 115 ok( !$c->session_is_valid, "session is gone" );
69da5f6e 116
45c0711b 117 is(
118 $c->session_delete_reason,
119 "session expired",
120 "reason is that the session expired"
121 );
69da5f6e 122
45c0711b 123 ok( !$c->session->{magic}, "no saved data" );
124 }
69da5f6e 125
45c0711b 126 __PACKAGE__->config->{session}{expires} = 0;
c80e9f04 127
45c0711b 128 @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
c80e9f04 129
45c0711b 130 __PACKAGE__->setup;
131 }
69da5f6e 132
45c0711b 133 use Test::More;
69da5f6e 134
45c0711b 135 can_ok( $m, "get_session_data" );
136 can_ok( $m, "store_session_data" );
137 can_ok( $m, "delete_session_data" );
138 can_ok( $m, "delete_expired_sessions" );
69da5f6e 139
45c0711b 140 {
141
142 package t1;
143 use Catalyst::Test "SessionStoreTest";
144
124488fb 145 # idiotic void context warning workaround
146
147 my $x = get("/create_session");
148 $x = get("/recover_session");
149 $x = get("/after_session");
45c0711b 150 }
151
152 {
153
154 package t2;
155 use Catalyst::Test "SessionStoreTest2";
156
124488fb 157 my $x = get("/create_session");
45c0711b 158 sleep 1; # let the session expire
124488fb 159 $x = get("/recover_session");
45c0711b 160 }
69da5f6e 161}
162
163__PACKAGE__;
164
165__END__
166
167=pod
168
169=head1 NAME
170
171Catalyst::Plugin::Session::Test::Store - Reusable sanity for session storage
172engines.
173
174=head1 SYNOPSIS
175
45c0711b 176 #!/usr/bin/perl
69da5f6e 177
45c0711b 178 use Catalyst::Plugin::Session::Test::Store (
179 backend => "FastMmap",
180 config => {
181 storage => "/tmp/foo",
182 },
183 );
69da5f6e 184
185=head1 DESCRIPTION
186
187=cut
188
189