finalize session before sending HTTP headers
[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 => 8;
7 use Test::MockObject::Extends;
8 use Test::Exception;
9
10 my $m;
11 BEGIN { use_ok( $m = "Catalyst::Plugin::Session" ) }
12
13 my $c = Test::MockObject::Extends->new($m);
14
15 my $flash = {};
16 $c->mock(
17     get_session_data => sub {
18         my ( $c, $key ) = @_;
19         return $key =~ /expire/ ? time() + 1000 : $flash;
20     },
21 );
22 $c->mock("store_session_data" => sub { $flash = $_[2] });
23 $c->mock("delete_session_data" => sub { $flash = {} });
24 $c->set_always( _sessionid => "deadbeef" );
25 $c->set_always( config     => { session => { expires => 1000 } } );
26 $c->set_always( stash      => {} );
27
28 is_deeply( $c->flash, {}, "nothing in flash" );
29
30 $c->flash->{foo} = "moose";
31
32 $c->finalize_body;
33
34 is_deeply( $c->flash, { foo => "moose" }, "one key in flash" );
35
36 $c->flash(bar => "gorch");
37
38 is_deeply( $c->flash, { foo => "moose", bar => "gorch" }, "two keys in flash" );
39
40 $c->finalize_body;
41
42 is_deeply( $c->flash, { bar => "gorch" }, "one key in flash" );
43
44 $c->finalize_body;
45
46 $c->flash->{test} = 'clear_flash';
47
48 $c->finalize_body;
49
50 $c->clear_flash();
51
52 is_deeply( $c->flash, {}, "nothing in flash after clear_flash" );
53
54 $c->finalize_body;
55
56 is_deeply( $c->flash, {}, "nothing in flash after finalize after clear_flash" );
57
58 $c->flash->{bar} = "gorch";
59
60 $c->config->{session}{flash_to_stash} = 1;
61
62 $c->finalize_body;
63 $c->prepare_action;
64
65 is_deeply( $c->stash, { bar => "gorch" }, "flash copied to stash" );