Added high performance version of the Sessions code as a branch
[catagits/Catalyst-Plugin-Session.git] / t / 03_flash.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 12;
7 use Test::MockObject::Extends;
8 use Test::Exception;
9 use Test::Deep;
10
11 my $m;
12 BEGIN { use_ok( $m = "Catalyst::Plugin::SessionHP" ) }
13
14 my $c = Test::MockObject::Extends->new($m);
15
16 my $flash = {};
17 $c->mock(
18     get_session_data => sub {
19         my ( $c, $key ) = @_;
20         return $key =~ /expire/ ? time() + 1000 : $flash;
21     },
22 );
23 $c->mock( "debug"               => sub {0} );
24 $c->mock( "store_session_data"  => sub { $flash = $_[2] } );
25 $c->mock( "delete_session_data" => sub { $flash = {} } );
26 $c->set_always( _session_id => "deadbeef" );
27 $c->set_always(
28     config => { session => { max_lifetime => 1000, min_lifetime => 500 } } );
29 $c->set_always( stash => {} );
30
31 # check that start state is as expected
32 is_deeply( $c->session, {}, "nothing in session" );
33 is_deeply( $c->flash,   {}, "nothing in flash" );
34
35 # set a value in the flash and check it gets to the flash
36 pass "--- add one value to the flash ---";
37 $c->flash->{foo} = "moose";
38 is_deeply( $c->flash, { foo => "moose" }, "one key in flash" );
39 $c->finalize_headers;
40
41
42 cmp_deeply(
43     $c->session,
44     {   __updated => re('^\d+$'),
45         __created => re('^\d+$'),
46         __flash   => { foo => "moose" },
47     },
48     "session  has __flash with flash data"
49 );
50
51 pass "--- add second value to flash ---";
52 $c->flash->{bar} = "gorch";
53 is_deeply(
54     $c->flash,
55     { foo => "moose", bar => "gorch" },
56     "two keys in flash"
57 );
58
59 $c->finalize_headers;
60
61 is_deeply( $c->flash, { bar => "gorch" }, "one key in flash" );
62
63 $c->finalize_headers;
64
65 $c->flash->{test} = 'clear_flash';
66
67 $c->finalize_headers;
68
69 $c->clear_flash();
70
71 is_deeply( $c->flash, {}, "nothing in flash after clear_flash" );
72
73 $c->finalize_headers;
74
75 is_deeply( $c->flash, {},
76     "nothing in flash after finalize after clear_flash" );
77
78 cmp_deeply(
79     $c->session,
80     { __updated => re('^\d+$'), __created => re('^\d+$'), },
81     "session has empty __flash after clear_flash + finalize"
82 );
83
84 $c->flash->{bar} = "gorch";