Added high performance version of the Sessions code as a branch
[catagits/Catalyst-Plugin-Session.git] / t / 03_flash.t
CommitLineData
95c6f6e8 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 12;
7use Test::MockObject::Extends;
8use Test::Exception;
9use Test::Deep;
10
11my $m;
12BEGIN { use_ok( $m = "Catalyst::Plugin::SessionHP" ) }
13
14my $c = Test::MockObject::Extends->new($m);
15
16my $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
32is_deeply( $c->session, {}, "nothing in session" );
33is_deeply( $c->flash, {}, "nothing in flash" );
34
35# set a value in the flash and check it gets to the flash
36pass "--- add one value to the flash ---";
37$c->flash->{foo} = "moose";
38is_deeply( $c->flash, { foo => "moose" }, "one key in flash" );
39$c->finalize_headers;
40
41
42cmp_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
51pass "--- add second value to flash ---";
52$c->flash->{bar} = "gorch";
53is_deeply(
54 $c->flash,
55 { foo => "moose", bar => "gorch" },
56 "two keys in flash"
57);
58
59$c->finalize_headers;
60
61is_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
71is_deeply( $c->flash, {}, "nothing in flash after clear_flash" );
72
73$c->finalize_headers;
74
75is_deeply( $c->flash, {},
76 "nothing in flash after finalize after clear_flash" );
77
78cmp_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";