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